Linux kernel Process Management ~Supplement(updating)

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

    • 0
      点赞
    • 0
      收藏
      觉得还不错? 一键收藏
    • 0
      评论
    评论
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

    当前余额3.43前往充值 >
    需支付:10.00
    成就一亿技术人!
    领取后你会自动成为博主和红包主的粉丝 规则
    hope_wisdom
    发出的红包
    实付
    使用余额支付
    点击重新获取
    扫码支付
    钱包余额 0

    抵扣说明:

    1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
    2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

    余额充值