国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 服務(wù)器 > Linux服務(wù)器 > 正文

淺談Linux內(nèi)核創(chuàng)建新進程的全過程

2024-09-05 23:02:29
字體:
供稿:網(wǎng)友
這篇文章主要為大家深入淺出的介紹了Linux內(nèi)核創(chuàng)建新進程的全過程,感興趣的小伙伴們可以參考一下
 

進程描述

  • 進程描述符(task_struct)

用來描述進程的數(shù)據(jù)結(jié)構(gòu),可以理解為進程的屬性。比如進程的狀態(tài)、進程的標(biāo)識(PID)等,都被封裝在了進程描述符這個數(shù)據(jù)結(jié)構(gòu)中,該數(shù)據(jù)結(jié)構(gòu)被定義為task_struct

  • 進程控制塊(PCB)

是操作系統(tǒng)核心中一種數(shù)據(jù)結(jié)構(gòu),主要表示進程狀態(tài)。

  • 進程狀態(tài)

淺談Linux內(nèi)核創(chuàng)建新進程的全過程

  • fork()

fork()在父、子進程各返回一次。在父進程中返回子進程的 pid,在子進程中返回0。

fork一個子進程的代碼

 

#include <stdio.h>#include <stdlib.h>#include <unistd.h>int main(int argc, char * argv[]){int pid;/* fork another process */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");}}

進程創(chuàng)建

1、大致流程

fork 通過0x80中斷(系統(tǒng)調(diào)用)來陷入內(nèi)核,由系統(tǒng)提供的相應(yīng)系統(tǒng)調(diào)用來完成進程的創(chuàng)建。

fork.c//fork#ifdef __ARCH_WANT_SYS_FORKSYSCALL_DEFINE0(fork){#ifdef CONFIG_MMU  return do_fork(SIGCHLD, 0, 0, NULL, NULL);#else  /* can not support in nommu mode */  return -EINVAL;#endif}#endif//vfork#ifdef __ARCH_WANT_SYS_VFORKSYSCALL_DEFINE0(vfork){  return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, 0,      0, NULL, NULL);}#endif//clone#ifdef __ARCH_WANT_SYS_CLONE#ifdef CONFIG_CLONE_BACKWARDSSYSCALL_DEFINE5(clone, unsigned long, clone_flags, unsigned long, newsp,     int __user *, parent_tidptr,     int, tls_val,     int __user *, child_tidptr)#elif defined(CONFIG_CLONE_BACKWARDS2)SYSCALL_DEFINE5(clone, unsigned long, newsp, unsigned long, clone_flags,     int __user *, parent_tidptr,     int __user *, child_tidptr,     int, tls_val)#elif defined(CONFIG_CLONE_BACKWARDS3)SYSCALL_DEFINE6(clone, unsigned long, clone_flags, unsigned long, newsp,    int, stack_size,    int __user *, parent_tidptr,    int __user *, child_tidptr,    int, tls_val)#elseSYSCALL_DEFINE5(clone, unsigned long, clone_flags, unsigned long, newsp,     int __user *, parent_tidptr,     int __user *, child_tidptr,     int, tls_val)#endif{  return do_fork(clone_flags, newsp, 0, parent_tidptr, child_tidptr);}#endif

通過看上邊的代碼,我們可以清楚的看到,不論是使用 fork 還是 vfork 來創(chuàng)建進程,最終都是通過 do_fork() 方法來實現(xiàn)的。接下來我們可以追蹤到 do_fork()的代碼:

long do_fork(unsigned long clone_flags,     unsigned long stack_start,     unsigned long stack_size,     int __user *parent_tidptr,     int __user *child_tidptr){    //創(chuàng)建進程描述符指針    struct task_struct *p;    //……    //復(fù)制進程描述符,copy_process()的返回值是一個 task_struct 指針。    p = copy_process(clone_flags, stack_start, stack_size,       child_tidptr, NULL, trace);    if (!IS_ERR(p)) {      struct completion vfork;      struct pid *pid;      trace_sched_process_fork(current, p);      //得到新創(chuàng)建的進程描述符中的pid      pid = get_task_pid(p, PIDTYPE_PID);      nr = pid_vnr(pid);      if (clone_flags & CLONE_PARENT_SETTID)        put_user(nr, parent_tidptr);      //如果調(diào)用的 vfork()方法,初始化 vfork 完成處理信息。      if (clone_flags & CLONE_VFORK) {        p->vfork_done = &vfork;        init_completion(&vfork);        get_task_struct(p);      }      //將子進程加入到調(diào)度器中,為其分配 CPU,準(zhǔn)備執(zhí)行      wake_up_new_task(p);      //fork 完成,子進程即將開始運行      if (unlikely(trace))        ptrace_event_pid(trace, pid);      //如果是 vfork,將父進程加入至等待隊列,等待子進程完成      if (clone_flags & CLONE_VFORK) {        if (!wait_for_vfork_done(p, &vfork))          ptrace_event_pid(PTRACE_EVENT_VFORK_DONE, pid);      }      put_pid(pid);    } else {      nr = PTR_ERR(p);    }    return nr;}

2、do_fork 流程

  • 調(diào)用 copy_process 為子進程復(fù)制出一份進程信息
  • 如果是 vfork 初始化完成處理信息
  • 調(diào)用 wake_up_new_task 將子進程加入調(diào)度器,為之分配 CPU
  • 如果是 vfork,父進程等待子進程完成 exec 替換自己的地址空間

3、copy_process 流程

追蹤copy_process 代碼(部分)

static struct task_struct *copy_process(unsigned long clone_flags,          unsigned long stack_start,          unsigned long stack_size,          int __user *child_tidptr,          struct pid *pid,          int trace){  int retval;  //創(chuàng)建進程描述符指針  struct task_struct *p;  //……  //復(fù)制當(dāng)前的 task_struct  p = dup_task_struct(current);  //……  //初始化互斥變量    rt_mutex_init_task(p);  //檢查進程數(shù)是否超過限制,由操作系統(tǒng)定義  if (atomic_read(&p->real_cred->user->processes) >=      task_rlimit(p, RLIMIT_NPROC)) {    if (p->real_cred->user != INIT_USER &&      !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN))      goto bad_fork_free;  }  //……  //檢查進程數(shù)是否超過 max_threads 由內(nèi)存大小決定  if (nr_threads >= max_threads)    goto bad_fork_cleanup_count;  //……  //初始化自旋鎖  spin_lock_init(&p->alloc_lock);  //初始化掛起信號  init_sigpending(&p->pending);  //初始化 CPU 定時器  posix_cpu_timers_init(p);  //……  //初始化進程數(shù)據(jù)結(jié)構(gòu),并把進程狀態(tài)設(shè)置為 TASK_RUNNING  retval = sched_fork(clone_flags, p);  //復(fù)制所有進程信息,包括文件系統(tǒng)、信號處理函數(shù)、信號、內(nèi)存管理等  if (retval)    goto bad_fork_cleanup_policy;  retval = perf_event_init_task(p);  if (retval)    goto bad_fork_cleanup_policy;  retval = audit_alloc(p);  if (retval)    goto bad_fork_cleanup_perf;  /* copy all the process information */  shm_init_task(p);  retval = copy_semundo(clone_flags, p);  if (retval)    goto bad_fork_cleanup_audit;  retval = copy_files(clone_flags, p);  if (retval)    goto bad_fork_cleanup_semundo;  retval = copy_fs(clone_flags, p);  if (retval)    goto bad_fork_cleanup_files;  retval = copy_sighand(clone_flags, p);  if (retval)    goto bad_fork_cleanup_fs;  retval = copy_signal(clone_flags, p);  if (retval)    goto bad_fork_cleanup_sighand;  retval = copy_mm(clone_flags, p);  if (retval)    goto bad_fork_cleanup_signal;  retval = copy_namespaces(clone_flags, p);  if (retval)    goto bad_fork_cleanup_mm;  retval = copy_io(clone_flags, p);  //初始化子進程內(nèi)核棧  retval = copy_thread(clone_flags, stack_start, stack_size, p);  //為新進程分配新的 pid  if (pid != &init_struct_pid) {    retval = -ENOMEM;    pid = alloc_pid(p->nsproxy->pid_ns_for_children);    if (!pid)      goto bad_fork_cleanup_io;  }  //設(shè)置子進程 pid   p->pid = pid_nr(pid);  //……  //返回結(jié)構(gòu)體 p  return p;
  • 調(diào)用 dup_task_struct 復(fù)制當(dāng)前的 task_struct
  • 檢查進程數(shù)是否超過限制
  • 初始化自旋鎖、掛起信號、CPU 定時器等
  • 調(diào)用 sched_fork 初始化進程數(shù)據(jù)結(jié)構(gòu),并把進程狀態(tài)設(shè)置為 TASK_RUNNING
  • 復(fù)制所有進程信息,包括文件系統(tǒng)、信號處理函數(shù)、信號、內(nèi)存管理等
  • 調(diào)用 copy_thread 初始化子進程內(nèi)核棧
  • 為新進程分配并設(shè)置新的 pid

4、dup_task_struct 流程

static struct task_struct *dup_task_struct(struct task_struct *orig){  struct task_struct *tsk;  struct thread_info *ti;  int node = tsk_fork_get_node(orig);  int err;  //分配一個 task_struct 節(jié)點  tsk = alloc_task_struct_node(node);  if (!tsk)    return NULL;  //分配一個 thread_info 節(jié)點,包含進程的內(nèi)核棧,ti 為棧底  ti = alloc_thread_info_node(tsk, node);  if (!ti)    goto free_tsk;  //將棧底的值賦給新節(jié)點的棧  tsk->stack = ti;  //……  return tsk;}

調(diào)用alloc_task_struct_node分配一個 task_struct 節(jié)點
調(diào)用alloc_thread_info_node分配一個 thread_info 節(jié)點,其實是分配了一個thread_union聯(lián)合體,將棧底返回給 ti

union thread_union {  struct thread_info thread_info; unsigned long stack[THREAD_SIZE/sizeof(long)];};

最后將棧底的值 ti 賦值給新節(jié)點的棧
最終執(zhí)行完dup_task_struct之后,子進程除了tsk->stack指針不同之外,全部都一樣!
5、sched_fork 流程

core.c

int sched_fork(unsigned long clone_flags, struct task_struct *p){  unsigned long flags;  int cpu = get_cpu();  __sched_fork(clone_flags, p);  //將子進程狀態(tài)設(shè)置為 TASK_RUNNING  p->state = TASK_RUNNING;  //……  //為子進程分配 CPU  set_task_cpu(p, cpu);  put_cpu();  return 0;}

我們可以看到sched_fork大致完成了兩項重要工作,一是將子進程狀態(tài)設(shè)置為 TASK_RUNNING,二是為其分配 CPU
6、copy_thread 流程

int copy_thread(unsigned long clone_flags, unsigned long sp,  unsigned long arg, struct task_struct *p){  //獲取寄存器信息  struct pt_regs *childregs = task_pt_regs(p);  struct task_struct *tsk;  int err;  p->thread.sp = (unsigned long) childregs;  p->thread.sp0 = (unsigned long) (childregs+1);  memset(p->thread.ptrace_bps, 0, sizeof(p->thread.ptrace_bps));  if (unlikely(p->flags & PF_KTHREAD)) {    //內(nèi)核線程    memset(childregs, 0, sizeof(struct pt_regs));    p->thread.ip = (unsigned long) ret_from_kernel_thread;    task_user_gs(p) = __KERNEL_STACK_CANARY;    childregs->ds = __USER_DS;    childregs->es = __USER_DS;    childregs->fs = __KERNEL_PERCPU;    childregs->bx = sp; /* function */    childregs->bp = arg;    childregs->orig_ax = -1;    childregs->cs = __KERNEL_CS | get_kernel_rpl();    childregs->flags = X86_EFLAGS_IF | X86_EFLAGS_FIXED;    p->thread.io_bitmap_ptr = NULL;    return 0;  }  //將當(dāng)前寄存器信息復(fù)制給子進程  *childregs = *current_pt_regs();  //子進程 eax 置 0,因此fork 在子進程返回0  childregs->ax = 0;  if (sp)    childregs->sp = sp;  //子進程ip 設(shè)置為ret_from_fork,因此子進程從ret_from_fork開始執(zhí)行  p->thread.ip = (unsigned long) ret_from_fork;  //……  return err;}

copy_thread 這段代碼為我們解釋了兩個相當(dāng)重要的問題!
一是,為什么 fork 在子進程中返回0,原因是childregs->ax = 0;這段代碼將子進程的 eax 賦值為0
二是,p->thread.ip = (unsigned long) ret_from_fork;將子進程的 ip 設(shè)置為 ret_form_fork 的首地址,因此子進程是從 ret_from_fork 開始執(zhí)行的
總結(jié)

新進程的執(zhí)行源于以下前提:

  • dup_task_struct中為其分配了新的堆棧
  • 調(diào)用了sched_fork,將其置為TASK_RUNNING
  • copy_thread中將父進程的寄存器上下文復(fù)制給子進程,保證了父子進程的堆棧信息是一致的
  • 將ret_from_fork的地址設(shè)置為eip寄存器的值

最終子進程從ret_from_fork開始執(zhí)行。

以上就是針對Linux內(nèi)核創(chuàng)建一個新進程的過程的詳細(xì)分析,希望對大家的學(xué)習(xí)有所幫助。



發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 新巴尔虎右旗| 沐川县| 宁夏| 昭平县| 长沙县| 刚察县| 英吉沙县| 四会市| 五台县| 金阳县| 元朗区| 浦江县| 凯里市| 自治县| 长白| 玉溪市| 新昌县| 外汇| 河源市| 疏附县| 洛宁县| 东乌珠穆沁旗| 卢氏县| 环江| 永城市| 平远县| 东兰县| 荔波县| 温州市| 靖宇县| 虎林市| 宜兰市| 资兴市| 宁阳县| 宝丰县| 宝丰县| 莱州市| 礼泉县| 历史| 镇安县| 彭水|