码迷,mamicode.com
首页 > 系统相关 > 详细

进程的描述与进程的创建

时间:2016-04-03 20:21:08      阅读:294      评论:0      收藏:0      [点我收藏+]

标签:

阅读理解task_struct数据结构,它包含了

进程状态、运行时间、调度信息、进程的通讯状况、task_struct型链表连接指针

标号,决定改进程归属、可以读写打开的一些文件信息、进程上下文和内核上下文、处理器上下文、内存信息等

 

  1. struct task_struct { volatile long state; 状态信息
  2. unsigned long flags; //进程号,在调用fork()时给出 
  3. int sigpending; //进程上是否有待处理的信号 
  4. mm_segment_t addr_limit; //进程地址空间,区分内核进程与普通进程在内存存放的不同位置 
  5. //0-0xBFFFFFFF for user-thead 
  6. //0-0xFFFFFFFF for kernel-thread //调度标志,表示该进程是否需要重新调度,若非0,则当从内核态返回到用户态,会发生调度 
  7. volatile long need_resched; 
  8. int lock_depth; //锁深度 
  9. long nice; //进程的基本时间片 
  10. //进程的调度策略,有三种,实时进程:SCHED_FIFO,SCHED_RR, 分时进程:SCHED_OTHER unsigned long policy; 
  11. struct mm_struct *mm; //进程内存管理信息 
  12. int processor; //若进程不在任何CPU上运行, cpus_runnable 的值是0,否则是1 这个值在运行队列被锁时更新 
  13. unsigned long cpus_runnable, cpus_allowed; 
  14. struct list_head run_list; //指向运行队列的指针 
  15. unsigned long sleep_time; //进程的睡眠时间 
  16. //用于将系统中所有的进程连成一个双向循环链表, 其根是init_task 
  17. struct task_struct *next_task, *prev_task; 
  18. struct mm_struct *active_mm; 
  19. struct list_head local_pages; //指向本地页面 
  20. unsigned int allocation_order, nr_local_pages; struct linux_binfmt *binfmt; //进程所运行的可执行文件的格式 
  21. int exit_code, exit_signal; 
  22. int pdeath_signal; //父进程终止是向子进程发送的信号 
  23. unsigned long personality; 
  24. int did_exec:1; pid_t pid; //进程标识符,用来代表一个进程 
  25. pid_t pgrp; //进程组标识,表示进程所属的进程组 
  26. pid_t tty_old_pgrp; //进程控制终端所在的组标识 
  27. pid_t session; //进程的会话标识
  28. pid_t tgid; 
  29. int leader; //表示进程是否为会话主管 
  30. struct task_struct *p_opptr,*p_pptr,*p_cptr,*p_ysptr,*p_osptr; struct list_head thread_group; //线程链表 
  31. struct task_struct *pidhash_next; //用于将进程链入HASH表 
  32. struct task_struct **pidhash_pprev; 
  33. wait_queue_head_t wait_chldexit; //供wait4()使用 
  34. struct completion *vfork_done; //供vfork() 使用 
  35. unsigned long rt_priority; //实时优先级,用它计算实时进程调度时的weight值 …… 
  36. };
  37. 进程创建分析 
  38. fork函数到底如何进行对应的内核处理过程sys_clone。
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include <unistd.h>
  42. int main(int argc, char * argv[])
  43. { int pid;    /* fork another process */
  44.     pid = fork();
        if (pid < 0) 
        { 
            /* error occurred */
            fprintf(stderr,"Fork Failed!");
            exit(-1);
        } 
        else if (pid == 0) 
        {
            /* child process */
            printf("This is Child Process!\n");
        } 
        else
        {  
            /* parent process  */
            printf("This is Parent Process!\n");
            /* parent will wait for the child to complete*/
            wait(NULL);
            printf("Child Complete!\n");
        }
    }

  45. ti = alloc_thread_info_node(tsk, node);
  46. tsk->stack = ti; 
  47. setup_thread_stack(tsk, orig); //这里只是复制thread_info,而非复制内核堆栈
  48. *childregs = *current_pt_regs(); //复制内核堆栈 
  49. childregs->ax = 0; //为什么子进程的fork返回0,这里就是原因! 
  50. p->thread.sp = (unsigned long) childregs; //调度到子进程时的内核栈顶 
  51. p->thread.ip = (unsigned long) ret_from_fork; //调度到子进程时的第一条指令地址
  52. static struct task_struct *dup_task_struct(struct task_struct *orig)
  53. {
    1.   struct task_struct *tsk;
    2. struct thread_info *ti;
    3. int node = tsk_fork_get_node(orig);
    4. int err;
    5. tsk = alloc_task_struct_node(node);
    6. if (!tsk)
    7. return NULL;
    8. ti = alloc_thread_info_node(tsk, node);
    9. if (!ti)
    10.  goto free_tsk;
    11. err = arch_dup_task_struct(tsk, orig);
    12. if (err)
    13.  goto free_ti;
    14. tsk->stack = ti;
    15. # ifdef CONFIG_SECCOMP
    16. tsk->seccomp.filter = NULL;
    17. # endif
    18. setup_thread_stack(tsk, orig);
    19. clear_user_return_notifier(tsk);
    20. clear_tsk_need_resched(tsk);
    21. set_task_stack_end_magic(tsk);
    22. # ifdef CONFIG_CC_STACKPROTECTOR
    23. tsk->stack_canary = get_random_int();
    24. # endif
    25. atomic_set(&tsk->usage, 2);
    26. # ifdef CONFIG_BLK_DEV_IO_TRACE
    27. tsk->btrace_seq = 0;
    28. # endif
    29. tsk->splice_pipe = NULL;
    30. tsk->task_frag.page = NULL;
    31. account_kernel_stack(ti, 1);
    32. return tsk;
    33. free_ti:
    34. free_thread_info(ti);
    35. free_tsk:
    36. free_task_struct(tsk);
    37. return NULL;
    38. }

int copy_thread(unsigned long clone_flags, unsigned long sp, unsigned long arg, struct task_struct *p)
{ ...
*childregs = *current_pt_regs();
childregs->ax = 0;
if (sp) childregs->sp = sp;
p->thread.ip = (unsigned long) ret_from_fork;
... }

/*ret from_fork*/
ENTRY(ret_from_fork)
    CFI_STARTPROC
    pushl_cfi %eax
    call schedule_tail
    GET_THREAD_INFO(%ebp)
    popl_cfi %eax
    pushl_cfi $0x0202       # Reset kernel eflags
    popfl_cfi
    jmp syscall_exit
    CFI_ENDPROC
END
小结:
(ret_from_fork)新进程是从在ret_from_fork之前,也就是在copy_thread()函数中*childregs = *current_pt_regs();该句将父进程的regs参数赋值到子进程的内核堆栈,,使子进程拥有了
SAVE ALL中压入栈的参数,故在ret from_fork时可以返回当前子进程的信息。 

进程的描述与进程的创建

标签:

原文地址:http://www.cnblogs.com/yktlll/p/5350379.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!