Is it possible to block a task from kernel space?

雨燕双飞 提交于 2019-12-06 05:38:50

In UP, this is quite simple: set the state of the task to TASK_INTERRUPTIBLE and call schedule(). You can "resume" it later by setting its state to TASK_RUNNING.

In SMP, you have to make sure that the task is not running on another CPU.

See this:

http://lxr.linux.no/linux+v3.0.4/include/linux/sched.h#L242

 250/*
 251 * This serializes "schedule()" and also protects
 252 * the run-queue from deletions/modifications (but
 253 * _adding_ to the beginning of the run-queue has
 254 * a separate lock).
 255 */
 256extern rwlock_t tasklist_lock;
 257extern spinlock_t mmlist_lock;
 258

So we we know this lock is for synchronizing access to update the scheduling structure. To change the tasking running status, look for an example:

http://lxr.linux.no/linux+v3.0.4/kernel/signal.c#L1812

1769                read_lock(&tasklist_lock); 
1809               __set_current_state(TASK_RUNNING); 
1810                if (clear_code) 
1811                        current->exit_code = 0; 
1812               read_unlock(&tasklist_lock

U just need to lock/unlock the tasklist_lock, and set the status.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!