先上图

include/linux/sched.h描述进程的状态
/* Used in tsk->state: */
#define TASK_RUNNING 0x0000
#define TASK_INTERRUPTIBLE 0x0001
#define TASK_UNINTERRUPTIBLE 0x0002
#define __TASK_STOPPED 0x0004
#define __TASK_TRACED 0x0008
/* Used in tsk->exit_state: */
#define EXIT_DEAD 0x0010
#define EXIT_ZOMBIE 0x0020
#define EXIT_TRACE (EXIT_ZOMBIE | EXIT_DEAD)
/* Used in tsk->state again: */
#define TASK_PARKED 0x0040
#define TASK_DEAD 0x0080
#define TASK_WAKEKILL 0x0100
#define TASK_WAKING 0x0200
#define TASK_NOLOAD 0x0400
#define TASK_NEW 0x0800
#define TASK_STATE_MAX 0x1000
fs/proc/array.c描述进程的状态(跟上面对应)
static const char * const task_state_array[] = {
/* states in TASK_REPORT: */
"R (running)", /* 0x00 */
"S (sleeping)", /* 0x01 */
"D (disk sleep)", /* 0x02 */
"T (stopped)", /* 0x04 */
"t (tracing stop)", /* 0x08 */
"X (dead)", /* 0x10 */
"Z (zombie)", /* 0x20 */
"P (parked)", /* 0x40 */
/* states beyond TASK_REPORT: */
"I (idle)", /* 0x80 */
};
写驱动模拟下
#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/wait.h>
#include <linux/sched.h>
#include <linux/uaccess.h>
#include <linux/errno.h>
#include <linux/device.h>
#include <linux/sched/signal.h>
#include <linux/delay.h>
#include <linux/miscdevice.h>
static unsigned int process_debug = 0;
module_param(process_debug, int, 0644);
MODULE_PARM_DESC(process_debug, "enable debug for process");
wait_queue_head_t test_queue;
int test_open(struct inode *node, struct file *filp)
{
return 0;
}
int test_close(struct inode *node, struct file *filp)
{
return 0;
}
ssize_t
test_read(struct file * filp, char __user * buf, size_t count, loff_t * offset)
{
if (process_debug == 0)
__set_current_state(TASK_RUNNING);
else if (process_debug == 1)
__set_current_state(TASK_INTERRUPTIBLE);
else if (process_debug == 2)
__set_current_state(TASK_UNINTERRUPTIBLE);
else if (process_debug == 3)
__set_current_state(__TASK_STOPPED);
else if (process_debug == 4)
__set_current_state(__TASK_TRACED);
else {
return 0;
}
pr_info("state=%d\n", process_debug);
mdelay(20000);
return 0;
}
struct file_operations test_fops = {
.open = test_open,
.release = test_close,
.read = test_read,
};
static struct miscdevice misc_dev = {
.minor = MISC_DYNAMIC_MINOR,
.name = "test",
.fops = &test_fops,
};
static int __init process_init(void) //模块初始化函数
{
int ret = 0;
init_waitqueue_head(&test_queue);
ret = misc_register(&misc_dev);
printk("process_init\n");
return ret;
}
static void __exit process_exit(void) //模块卸载函数
{
misc_deregister(&misc_dev);
printk("process_exit\n");
} module_init(process_init);
module_exit(process_exit);
MODULE_LICENSE("GPL");
测试app
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
int main(void)
{
char buf[20];
int fd;
int ret;
fd = open("/dev/test", O_RDWR);
if (fd < 0) {
perror("open");
return -1;
}
ret = read(fd, buf, 10);
if (ret == -1) {
printf("errno = %d\n", errno);
}
close(fd);
return 0;
}
本文深入解析了Linux内核中进程的各种状态定义及其转换机制,并通过一个具体的驱动程序示例展示了如何模拟不同状态的进程,有助于理解Linux调度机制。
453

被折叠的 条评论
为什么被折叠?



