Chinaunix首页 | 论坛 | 博客
  • 博客访问: 898404
  • 博文数量: 194
  • 博客积分: 7991
  • 博客等级: 少将
  • 技术积分: 2067
  • 用 户 组: 普通用户
  • 注册时间: 2007-12-09 22:03
文章分类

全部博文(194)

文章存档

2010年(8)

2009年(71)

2008年(109)

2007年(6)

我的朋友

分类: LINUX

2008-11-24 09:47:09

Linux® kernel 2.6.25 introduced a new process state for putting processes to sleep called TASK_KILLABLE, which offers an alternative to the efficient but potentially unkillable TASK_UNINTERRUPTIBLE and the easy-to-awaken but safer TASK_INTERRUPTIBLE.
 
Here is the list of process  states:
TASK_RUNNING:
The process is either running on CPU or waiting in a run queue to get scheduled.
TASK_INTERRUPTIBLE:
The process is sleeping, waiting for some event to occur. The process is open to be interrupted by signals. Once signalled or awoken by explicit wake-up call, the process transitions to TASK_RUNNING.
TASK_UNINTERRUPTIBLE:
The process state is similar to TASK_INTERRUPTIBLE except that in this state it does not process signals. It may not be desirable even to interrupt the process while in this state since it may be in the middle of completing some important task. When the event occurs that it is waiting for, the process is awaken by the explicit wake-up call.
TASK_KILLABLE:
If a process is sleeping killably in this new state, it works like TASK_UNINTERRUPTIBLE with the bonus that it can respond to fatal signals.
TASK_STOPPED:
The process execution is stopped, it's not running, and not able to run. On receipt of signals like SIGSTOP, SIGTSTP, and so on, the process arrives at this state. The process would be runnable again on receipt of signal SIGCONT.
TASK_TRACED:
A process arrives at this state while it is being monitored by other processes such as debuggers.
EXIT_ZOMBIE:
The process has terminated. It is lingering around just for its parent to collect some statistical information about it.
EXIT_DEAD:
The final state (just like it sounds). The process reaches this state when it is being removed from the system since its parent has just collected all statistical information by issuing the wait4() or waitpid() system call.
 
#define TASK_RUNNING            0
#define TASK_INTERRUPTIBLE      1
#define TASK_UNINTERRUPTIBLE    2
#define __TASK_STOPPED          4
#define __TASK_TRACED           8
/* in tsk->exit_state */
#define EXIT_ZOMBIE             16
#define EXIT_DEAD               32
/* in tsk->state again */
#define TASK_DEAD               64
#define TASK_WAKEKILL           128
TASK_WAKEKILL is designed to wake the process on receipt of fatal signals.

#define TASK_KILLABLE   (TASK_WAKEKILL | TASK_UNINTERRUPTIBLE)
#define TASK_STOPPED    (TASK_WAKEKILL | __TASK_STOPPED)
#define TASK_TRACED     (TASK_WAKEKILL | __TASK_TRACED)
 
New kernel APIs using TASK_KILLABLE:
int wait_event_killable(wait_queue_t queue, condition);
This function is defined in include/linux/wait.h; it puts the calling process to sleep killably in queue until the condition evaluates to true.

long schedule_timeout_killable(signed long timeout);
This is defined in kernel/timer.c; this routine basically sets the current task's state to TASK_KILLABLE and calls schedule_timeout(), which makes the calling task sleep for timeout number of jiffies.

int wait_for_completion_killable(struct completion *comp);
Defined in kernel/sched.c, this routine is used to wait killably for the completion of an event. This function calls schedule_timeout() for MAX_SCHEDULE_TIMEOUT (defined to be equal to LONG_MAX) jiffies if there are no fatal signals pending.

int mutex_lock_killable(struct mutex *lock);
Defined in kernel/mutex.c, this routine is used to acquire mutex lock. However, if the lock is not available and the task is waiting to get the lock, and in the meantime it gets a fatal signal, the task would be removed from the list of waiters waiting for the mutex lock to process the signal.

int down_killable(struct semaphore *sem);
Defined in kernel/semaphore.c, it is used to acquire the semaphore sem. If the semaphore is not available, it's put to sleep; if a fatal signal is delivered to it, it would be removed from the waiters' list and would have to respond to the signal. The other two methods of acquiring a semaphore are by using the routines down() or down_interruptible(). The function down() is deprecated now; you should use either down_killable() or down_interruptible().
 
 
阅读(1625) | 评论(0) | 转发(0) |
0

上一篇:some saying about love

下一篇:Get to know GCC 4

给主人留下些什么吧!~~