一, linux进程状态
TASK_RUNNING 态 : 可运行态,具备运行的条件,处于运行调度对列,等待调度,运行队列和调度策略与内核调度器类型有关,O(1)调度器2.6.23版本之后出现及CFG调度器后续分析。
TASK_INTERRUPTIBLE 态: 可中断的等待状态,进程正在等待某个事件或资源,处于wait_queue等待队列, 可以通过信号唤醒。
TASK_UNINTERRUPTIBLE态: 不可中断的等待状态,进程正在等待某个事件或资源,处于wait_queue等待队列,强制唤醒。
TASK_INTERRUPTIBLE 态,TASK_UNINTERRUPTIBLE态相关API:
等待队列结构:
struct __wait_queue_head {
wq_lock_t lock;
struct list_head task_list;
};
typedef struct __wait_queue_head wait_queue_head_t;
初始化方法:
静态方法:
DECLARE_WAIT_QUEUE_HEAD(name)
动态方法:
wait_queue_head_t my_queue;
init_waitqueue_head(&my_queue);
init_waitqueue_entry(wait_queue_head_t *head, task_struct * p);
void add_wait_queue(wait_queue_head_t *head, wait_queue_t *wait);
休眠方式:
wait_event(queue, condition) /*进程将被置于非中断休眠 */
wait_event_interruptible(queue, condition)
/*进程可被信号中断休眠,返回非0值表示休眠被信号中断*/
wait_event_timeout(queue, condition,timeout)
wait_event_interruptible_timeout(queue, condition, timeout)
呼醒方式:
void wake_up(wait_queue_head_t *queue);
/* 呼醒queue队列等待的进程 */
void wake_up_interruptible(wait_queue_head_t *queue);
wait_event的实现:
#define wait_event(wq, condition) \
do { \
if (condition) \
break; \
__wait_event(wq, condition); \
} while (0)
#define __wait_event(wq, condition) \
do { \
wait_queue_t __wait; \
init_waitqueue_entry(&__wait, current); \
add_wait_queue(&wq, &__wait); \
for (;;) { \
set_current_state(TASK_UNINTERRUPTIBLE); \
if (condition) \
break; \
schedule(); \
} \
current->state = TASK_RUNNING; \
remove_wait_queue(&wq, &__wait); \
} while (0)
wake_up的实现:
__wake_up((x),
TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE, 1)
void __wake_up(wait_queue_head_t *q, unsigned int mode, int nr)
{
if (q) {
unsigned long flags;
wq_read_lock_irqsave(&q->lock, flags);
__wake_up_common(q, mode, nr, 0);
wq_read_unlock_irqrestore(&q->lock, flags);
}
}
void __wake_up_common (wait_queue_head_t *q, unsigned int mode,
int nr_exclusive, const int sync)
{
struct list_head *tmp;
struct task_struct *p;
CHECK_MAGIC_WQHEAD(q);
WQ_CHECK_LIST_HEAD(&q->task_list);
list_for_each(tmp,&q->task_list) {
unsigned int state;
wait_queue_t *curr = list_entry(tmp, wait_queue_t, task_list);
CHECK_MAGIC(curr->__magic);
p = curr->task;
state = p->state;
if (state & mode) {
WQ_NOTE_WAKER(curr);
if (try_to_wake_up(p, sync) &&
(curr->flags&WQ_FLAG_EXCLUSIVE) && !--nr_exclusive)
/*nr_exclusive == 1呼醒第一个等待进程*/
/* nr_exclusive == 0呼醒所有等待进程*/
break;
}
}
}
void sleep_on(wait_queue_head_t *q)
long sleep_on_timeout(wait_queue_head_t *q, signed long timeout) 使得当前进程处于睡眠状态
使其处于UNINTERRUPTIBLE状态
由wake_up函数呼醒
void interruptible_sleep_on(wait_queue_head_t *q)
long interruptible_sleep_on_timeout(wait_queue_head_t *q, signed long timeout)
使得当前进程处于睡眠状态
使其处于INTERRUPTIBLE状态
由wake_up_interruptible函数呼醒
struct completion {
unsigned int done;
wait_queue_head_t wait;
};
void init_completion(struct completion *x)
void complete(struct completion *x)
void wait_for_completion(struct completion *x)
这组函数也是封装了_wake_up_common及__add_wait_queue_tail实现
实现如下:
void complete(struct completion *x)
{
unsigned long flags;
spin_lock_irqsave(&x->wait.lock, flags);
x->done++;
__wake_up_common(&x->wait, TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE, 1, 0);
spin_unlock_irqrestore(&x->wait.lock, flags);
}
void wait_for_completion(struct completion *x)
{
spin_lock_irq(&x->wait.lock);
if (!x->done) {
DECLARE_WAITQUEUE(wait, current);
wait.flags |= WQ_FLAG_EXCLUSIVE;
__add_wait_queue_tail(&x->wait, &wait);
do {
__set_current_state(TASK_UNINTERRUPTIBLE);
spin_unlock_irq(&x->wait.lock);
schedule();
spin_lock_irq(&x->wait.lock);
} while (!x->done);
__remove_wait_queue(&x->wait, &wait);
}
x->done--;
spin_unlock_irq(&x->wait.lock);
}
TASK_ZOMBIE状态: 是指进程结束但是尚未消亡的一种状态,除进程控制块外进程的所有资源都已经释放
进程处于僵死状态,待wait4()释放资源
TASK_STOP状态: 进程暂停态,通过其它进程的信号才能呼醒该进程