Linux下的进程状态切换

本文深入解析了Linux内核中进程的各种状态定义及其转换机制,并通过一个具体的驱动程序示例展示了如何模拟不同状态的进程,有助于理解Linux调度机制。

先上图

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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值