Author: Harold Wang
http://blog.csdn.net/hero7935
1. Task state:
struct task_struct* task;
/*
* This is OK even in SMP system, because it can involve Memory Barrier if necessary.
*/
set_task_state(task,state);
set_task_state(current,state)=set_current_state(state)
/*
* We can simply use this instead if not in SMP environment;
*/
task->state=state;
2.Iterate all processes:
struct task_struct* task;
for_each_process(task)
{
printk("%s[%d]/n",task->comm,task->pid);
}
// if we want get the Init process(PID=1)
for(task=current;task!=init_task;task=task->parent);
//The relationship between process family:
3. Process Creation:
Modern Unix kernels involve three mechnisms to create process with less overhead, at least, do their best.
- Copy On Write Tech
- Lightweight process
- vfork() system call(deprecated, see Advanced Unix Programming 2rd edition)
IN LINUX:
fork() :
--copy all the resources the parent has-->generate process
--clone(SIGCHLD,0)
vfork():
--copy works as fork() except the task_struct and kernel stack-->generate thread.
--clone(CLONE_VFORK|CLONE-VM|SIGCHLD,0)
NOTE: we can specify the resources that we want the "clone" to share with its parent.
like : CLONE_FS CLONE_FILES CLONE_VM CLONE_SIGHAND et al.
//In fact, fork/vfork/clone, they all call do_fork at last to achieve their goals.
Author: Harold Wang
http://blog.csdn.net/hero7935
//if this is still not clear, please look at this picture to see if any better:
4. Process Switch see x86 or amd64
5. Process Termination:
two methods to teminate process---->
exit_group():corresponding main kernel function is do_group_exit()
_exit():corresponding main kernel function is do_exit()
note: do_exit() only mean that the thread is in the state of EXIT_ZOMBIE, when all objects associated with the task are freed, the task is not runnable(no longer has an address space in which to run) . The only memory it occupies is its kernel stack, the thread_info structure,and the task_struct structure. YES, the final work will be fixed by its parent who call the wait() function. YES, may be his original parent has left him, so forget_orginal_parent() will give a hand. YES, it nobody like this orphan, init thread will do.
Author: Harold Wang
http://blog.csdn.net/hero7935
when we can do_exit