Linux Kernel Development -- 设置当前进程的状态

简介:

首先查看内核源码中是如何定义的

复制代码
#define __set_task_state(tsk, state_value) \ 
do { (tsk)->state = (state_value); } while (0) 
#define set_task_state(tsk, state_value) \ 
set_mb((tsk)->state, (state_value)) 
/* 
* set_current_state() includes a barrier so that the write of current->state 
* is correctly serialised wrt the caller's subsequent test of whether to 
* actually sleep: 
* 
* set_current_state(TASK_UNINTERRUPTIBLE); 
* if (do_i_need_to_sleep()) 
* schedule(); 
* 
* If the caller does not need such serialisation then use __set_current_state()
*/ 
#define __set_current_state(state_value) \ 
do { current->state = (state_value); } while (0) 
#define set_current_state(state_value) \ 
set_mb(current->state, (state_value)) 
复制代码

 

如果是单处理器,则 set_mb 的定义为(asm-x86/system_32.h):

#define set_mb(var, value) do { var = value; barrier(); } while (0)

在对 current->state 复制后,简单的用 barrier() 刷新一下内存和寄存器之间的关系。

如果是多处理器 SMP,则 set_mb 的定义为:

#define set_mb(var, value) do { (void) xchg(&var, value); } while (0)

xchg(&var, value) 定义为(asm-x86/cmpxchg_32.h):
#define xchg(ptr,v) ((__typeof__(*(ptr)))__xchg((unsigned long)(v),(ptr),sizeof(*(ptr))))

上面,(__typeof__(*(ptr))) 获得 *ptr 的类型,因为 *ptr 就是 var,而 var 的类型为 long 型,所以这里也是对 __xchg() 返回的类型强制转换为 long 型。

__xchg() 被定义为:

复制代码
/*
* Note: no "lock" prefix even on SMP: xchg always implies lock anyway
* Note 2: xchg has side effect, so that attribute volatile is necessary,
* but generally the primitive is invalid, *ptr is output argument. --ANK
*/
static inline unsigned long __xchg(unsigned long x, volatile void * ptr, int size)
{
switch (size) {
case 1:
__asm__ __volatile__("xchgb %b0,%1"
:"=q" (x)
:"m" (*__xg(ptr)), "0" (x)
:"memory");
break;
case 2:
__asm__ __volatile__("xchgw %w0,%1"
:"=r" (x)
:"m" (*__xg(ptr)), "0" (x)
:"memory");
break;
case 4:
__asm__ __volatile__("xchgl %0,%1"
:"=r" (x)
:"m" (*__xg(ptr)), "0" (x)
:"memory");
break;
}
return x;
}
复制代码

上面,根据 __xchg 第 3 个表示长度的参数来使用 xchg 指令将要设置的新 state 的值交换到 current->state 中。


set_current_state()与set_task_state()


set_current_state(state)等价于set_task_state(current, state)

__set_current_state()与set_current_state()

set_task_state()带有一个memory barrier,__set_task_state()则没有,当状态state是RUNNING时,因为scheduler可能访问这个state,因 此此时要变成其他状态(如INTERRUPTIBLE),就要用set_task_state();
而当state不是RUNNING时,因为没有其他人会 访问这个state,因此可以用__set_task_state(). 
所以用set_task_state()肯定是安全的,但 __set_task_state()可能会快些。


set_current_state() 的使用


在驱动程序中,进程睡眠往往通过 3 个步骤进行:
1. 将进程加入等待队列中。
2. 然后使用 set_current_state() 来设置进程的状态,设置的状态为 TASK_INTERRUPTIBLE 或 TASK_UNINTERRUTIBLE 。
3. 上面的设置完后,我们就要放弃处理器了。但在放弃处理器之前,还有一件重要的事情需要做:检查睡眠等待的条件。如果不检查,如果此时条件正好变为真,那么就漏掉了继续运行的机会,从而会睡眠更长的时间。

所以,一般在睡前需要类似的动作:

set_current_state(TASK_UNINTERRUPTIBLE);
if (do_i_need_to_sleep())
schedule();

本文转自feisky博客园博客,原文链接:http://www.cnblogs.com/feisky/archive/2013/03/04/2943250.html,如需转载请自行联系原作者

相关文章
|
4天前
|
NoSQL Linux 程序员
【linux进程信号(一)】信号的概念以及产生信号的方式
【linux进程信号(一)】信号的概念以及产生信号的方式
|
4天前
|
Linux
【linux进程间通信(一)】匿名管道和命名管道
【linux进程间通信(一)】匿名管道和命名管道
|
4天前
|
Java Shell Linux
【linux进程控制(三)】进程程序替换--如何自己实现一个bash解释器?
【linux进程控制(三)】进程程序替换--如何自己实现一个bash解释器?
|
4天前
|
算法 Linux Shell
【linux进程(二)】如何创建子进程?--fork函数深度剖析
【linux进程(二)】如何创建子进程?--fork函数深度剖析
|
4天前
|
存储 Linux Shell
【linux进程(一)】深入理解进程概念--什么是进程?PCB的底层是什么?
【linux进程(一)】深入理解进程概念--什么是进程?PCB的底层是什么?
|
4天前
|
存储 Linux Android开发
RK3568 Android/Linux 系统动态更换 U-Boot/Kernel Logo
RK3568 Android/Linux 系统动态更换 U-Boot/Kernel Logo
20 0
|
4天前
|
Ubuntu Linux
Linux(22) Linux设置网络优先级顺序
Linux(22) Linux设置网络优先级顺序
6 0
|
5天前
|
消息中间件 Unix Linux
Linux的学习之路:17、进程间通信(1)
Linux的学习之路:17、进程间通信(1)
20 1
|
5天前
|
存储 安全 Linux
Linux的学习之路:9、冯诺依曼与进程(1)
Linux的学习之路:9、冯诺依曼与进程(1)
18 0
|
1月前
|
消息中间件 Linux 调度
【Linux 进程/线程状态 】深入理解Linux C++中的进程/线程状态:阻塞,休眠,僵死
【Linux 进程/线程状态 】深入理解Linux C++中的进程/线程状态:阻塞,休眠,僵死
67 0