设为首页 加入收藏

TOP

内核进程的复制(十一)
2012-11-01 15:47:27 来源: 作者: 【 】 浏览:3016
Tags:内核 进程 复制

  
  九,初始化父子关系
  
  [cpp]
  
  /* CLONE_PARENT re-uses the old parent */
  
  if (clone_flags & (CLONE_PARENT|CLONE_THREAD))
  
  p->real_parent = current->real_parent;
  
  else
  
  p->real_parent = current;
  
  p->parent = p->real_parent;
  
  spin_lock(&current->sighand->siglock);
  
  /*
  
  * Process group and session signals need to be delivered to just the
  
  * parent before the fork or both the parent and the child after the
  
  * fork. Restart if a signal comes in before we add the new process to
  
  * it's process group.
  
  * A fatal signal pending means that current will exit, so the new
  
  * thread can't slip out of an OOM kill (or normal SIGKILL)。
  
  */
  
  recalc_sigpending();
  
  if (signal_pending(current)) {
  
  spin_unlock(¤t->sighand->siglock);
  
  write_unlock_irq(&tasklist_lock);
  
  retval = -ERESTARTNOINTR;
  
  goto bad_fork_free_pid;
  
  }
  
  如果创建的是线程或者是PARENT选项,则其父进程为current的父进程。
  
  十,线程创建部分
  
  如果本次创建的是线程,一些另于进程创建的操作。
  
  [cpp]
  
  if (clone_flags & CLONE_THREAD) {
  
  p->group_leader = current->group_leader;//线程组长就是当前线程的组长
  
  list_add_tail_rcu(&p->thread_group, &p->group_leader->thread_group);
  
  if (!cputime_eq(current->signal->it_virt_expires,
  
  cputime_zero) ||
  
  !cputime_eq(current->signal->it_prof_expires,
  
  cputime_zero) ||
  
  current->signal->rlim[RLIMIT_CPU].rlim_cur != RLIM_INFINITY ||
  
  !list_empty(&current->signal->cpu_timers[0]) ||
  
  !list_empty(&current->signal->cpu_timers ) ||
  
  !list_empty(&current->signal->cpu_timers )) {
  
  /*
  
  * Have child wake up on its first tick to check
  
  * for process CPU timers.
  
  */
  
  p->it_prof_expires = jiffies_to_cputime(1);
  
  }
  
  }
  
  十一,新进程插入进程链表
  
  [cpp]
  
  if (likely(p->pid)) {
  
  add_parent(p);
  
  if (unlikely(p->ptrace & PT_PTRACED))
  
  __ptrace_link(p, current->parent);
  
  if (thread_group_leader(p)) {
  
  if (clone_flags & CLONE_NEWPID)
  
  p->nsproxy->pid_ns->child_reaper = p;如果创建新的命名空间,则将命名空间的child_reaper设为当前创建的进程,这个进程就是这个创建的命名空间的init进程。
  
  p->signal->tty = current->signal->tty;
  
  set_task_pgrp(p, task_pgrp_nr(current));
  
  set_task_session(p, task_session_nr(current));
  
  attach_pid(p, PIDTYPE_PGID, task_pgrp(current));
  
  attach_pid(p, PIDTYPE_SID, task_session(current));
  
  list_add_tail_rcu(&p->tasks, &init_task.tasks);
  
  __get_cpu_var(process_counts)++;
  
  }
  
  attach_pid(p, PIDTYPE_PID, pid);
  
  nr_threads++;
  
  }
  
  add_parent宏将进程的children链表与父进程连接,实现如下
  
  [cpp]
  
  #define add_parent(p)       list_add_tail(&(p)->sibling,&(p)->parent->children)

            

首页 上一页 8 9 10 11 12 13 14 下一页 尾页 11/16/16
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C/C++学习指针一些事 下一篇C/C++学习之++i 和 i++..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: