进程原理及其系统调用(下)

简介: 进程原理及其系统调用

进程优先级

并非所有进程都具有相同的重要性。除了大多数我们所熟悉的进程优先级之外,进程还有不同的关键度类别,以满足不同需求。首先进行比较粗糙的划分,进程可以分为实时进程(0~99)和非实时进程(普通进程100-139):

进程系统调用

讨论fork和exec系列系统调用的实现。通常这些调用不是由应用程序直接发出的,而是通过一个中间层调用,即负责与内核通信的C标库。从用户状态切换到核心态的方法,依不同的体系结构而各有不同:

进程复制

传统的入UNIX中用·于复制进程的系统调用是fork。但它并不是Linux为此实现的唯一调用,实际Linux实现了3个。

  1. fork是重量级调用,因为它建立了父进程的.个完整副本,然后作为了进程执行。为减少与该调用相关的工作量,Linux使用了写时复制(copy-on-write)技术。
  2. vfork类似于k,不并不创建父进程数据的副本。相反,父了进程之间共享数据。
    这节省了大量CPU时间(如果一个进程操纵共享数据,则另一个会自动注意到)。
  3. clone产生线程,可以对父了进程之间的共享、复制进行精确控制。

写时复制(copy-on-write)

内核使用了写时复制(Copy-0n-Wnte,COW)技术,以防十在fork执行时将父进程的所有数据复制到了进程。在调用fork时,内核通常对父进程的每个内存页,都为了进程创建一个相同的副本。

问题?如果主进程修改其中页z的数据?此时就会发生父子进程在内存分离。

只有在不得不复制数据内容时才去复制数据内容,这是是写时复制核心思想,可以看到因为修改页z导致子进程不得不去复制原页z来保证父子进程互干扰。

内核只为新生成的子进程创建虚拟空间结构,它们来复制于父进程的虚拟结构,但是不为这些段分配物理内存,它们共享父进程的物理空间,当父进程中有更改相应段的行为发生时,再为子进程相应段分配物理空间。

执行系统调用

fork、vfork和clone系统调用的入口点分别是sys_fork、sys_vfork和sys_clone函数。其定又依赖于具体的体系结构,因为在用户窄间和内核空间之间传递参数的方法因体系结构而异。

do_fork实现

所有3个fork机制最终都调用kernel/fork中的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)
{//生成一个子进程,然后把它加入到CPU就绪队列,等待CPU调度,然后系统调用就返回了。
  struct task_struct *p;
  int trace = 0;
  long nr;
/*
   * Determine whether and which event to report to ptracer.  When
   * called from kernel_thread or CLONE_UNTRACED is explicitly
   * requested, no event is reported; otherwise, report if the event
   * for the type of forking is enabled.
   */
  if (!(clone_flags & CLONE_UNTRACED)) {
    if (clone_flags & CLONE_VFORK)
      trace = PTRACE_EVENT_VFORK;
    else if ((clone_flags & CSIGNAL) != SIGCHLD)
      trace = PTRACE_EVENT_CLONE;
    else
      trace = PTRACE_EVENT_FORK;
    if (likely(!ptrace_event_enabled(current, trace)))
      trace = 0;
  }
  p = copy_process(clone_flags, stack_start, stack_size,
       child_tidptr, NULL, trace);//将父进程的相关资源复制到子进程,执行生成子进程的工作;
  /*
   * Do this prior waking up the new thread - the thread pointer
   * might get invalid after that point, if the thread exits quickly.
   */
  if (!IS_ERR(p)) {
    struct completion vfork;
    struct pid *pid;
    trace_sched_process_fork(current, p);
    pid = get_task_pid(p, PIDTYPE_PID);
    nr = pid_vnr(pid);
    if (clone_flags & CLONE_PARENT_SETTID)
      put_user(nr, parent_tidptr);
    if (clone_flags & CLONE_VFORK) {
      p->vfork_done = &vfork;
      init_completion(&vfork);
      get_task_struct(p);
    }
    wake_up_new_task(p);//将子进程加入到CPU就绪队列。
    /* forking complete and child started to run, tell ptracer */
    if (unlikely(trace))
      ptrace_event_pid(trace, pid);
    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;
}

内核线程

内核线程是直接由内核本身启动的进程。内核线程实际上是将内核函数委托给独立的进程,与系统中其他进程“并行"执行(实际上,也并行于内核自身的执0)。内核线程经常称之为(内核)守护进程。它们用于执行下列任务。

  • 周期性地将修改的内存页与页来源块设各同步(例如,使用mmap的文件映射)。
  • 如果内存页很少使用,则写人交换区。
  • 管理延时动作(deferredaction)
  • 实现文件系统的事务日志。

退出线程

进程必须用exrit系统调用终止。这使得内核有机会将该进

程使用的资源释放回系统。见kernel/exit.c----->do_exit。简而言之,

该函数的实现就是将各个引用汁数器减1,如果引用计数器]0而

没有进程再使用对应的结构,那么将相应的内存区域返还给内存

管理模块。

void do_exit(long code)
{
  struct task_struct *tsk = current;
  int group_dead;
  TASKS_RCU(int tasks_rcu_i);
  profile_task_exit(tsk);//触发task_exit_nb通知链实例的处理函数
  WARN_ON(blk_needs_flush_plug(tsk));//检查进程的blk_plug是否为空
//保证task_struct中的plug字段是空的,或者plug字段指向的队列是空的。plug字段的意义是stack plugging
  if (unlikely(in_interrupt()))//OOPS消息,中断上下文不能执行do_exit函数, 也不能终止PID为0的进程
    panic("Aiee, killing interrupt handler!");
  if (unlikely(!tsk->pid))
    panic("Attempted to kill the idle task!");
  /*
   * If do_exit is called because this processes oopsed, it's possible
   * that get_fs() was left as KERNEL_DS, so reset it to USER_DS before
   * continuing. Amongst other possible reasons, this is to prevent
   * mm_release()->clear_child_tid() from writing to a user-controlled
   * kernel address.
   */
  set_fs(USER_DS);//设定进程可以使用的虚拟地址的上限(用户空间)
  ptrace_event(PTRACE_EVENT_EXIT, code);
  validate_creds_for_do_exit(tsk);
  /*
   * We're taking recursive faults here in do_exit. Safest is to just
   * leave this task alone and wait for reboot.
   */
   //检查进病设置进程程PF_EXITING
   /*/*current->flags的PF_EXITING标志表示进程正在被删除*/
  if (unlikely(tsk->flags & PF_EXITING)) {/*  检查PF_EXITING标志是否未被设置  */
    pr_alert("Fixing recursive fault but reboot is needed!\n");
    /*
     * We can do this unlocked here. The futex code uses
     * this flag just to verify whether the pi state
     * cleanup has been done or not. In the worst case it
     * loops once more. We pretend that the cleanup was
     * done as there is no way to return. Either the
     * OWNER_DIED bit is set by now or we push the blocked
     * task into the wait for ever nirwana as well.
     */
    tsk->flags |= PF_EXITPIDONE;/*  设置进程标识为PF_EXITPIDONE*/
    set_current_state(TASK_UNINTERRUPTIBLE);/*  设置进程状态为不可中断的等待状态 */
    schedule();/*  调度其它进程  */
  }
//如果此标识未被设置, 则通过exit_signals来设置
  exit_signals(tsk);  /* sets PF_EXITING */
  /*
   * tsk->flags are checked in the futex code to protect against
   * an exiting task cleaning up the robust pi futexes.
   */
  smp_mb();/*  内存屏障,用于确保在它之后的操作开始执行之前,它之前的操作已经完成*/
  raw_spin_unlock_wait(&tsk->pi_lock); /*  一直等待,直到获得current->pi_lock自旋锁*/
  if (unlikely(in_atomic()))
    pr_info("note: %s[%d] exited with preempt_count %d\n",
      current->comm, task_pid_nr(current),
      preempt_count());
  acct_update_integrals(tsk);//获取current->mm->rss_stat.count[member]计数
  /* sync mm's RSS info before statistics gathering */
  if (tsk->mm)//同步进程的mm的rss_stat
    sync_mm_rss(tsk->mm);
  group_dead = atomic_dec_and_test(&tsk->signal->live);//清除定时器
  if (group_dead) {
    hrtimer_cancel(&tsk->signal->real_timer);
    exit_itimers(tsk->signal);
    if (tsk->mm)
      setmax_mm_hiwater_rss(&tsk->signal->maxrss, tsk->mm);
  }
  acct_collect(code, group_dead);//收集进程会计信息
  if (group_dead)//审计
    tty_audit_exit();//记录审计事件
  audit_free(tsk);//  释放struct audit_context结构体
  tsk->exit_code = code;
  taskstats_exit(tsk, group_dead);
//释放进程占用的资源
  exit_mm(tsk);/*  释放存储空间
    放弃进程占用的mm,如果没有其他进程使用该mm,则释放它。
     */
  if (group_dead)//输出进程会计信息
    acct_process();
  trace_sched_process_exit(tsk);
  exit_sem(tsk);/*  释放用户空间的“信号量”  */
  exit_shm(tsk);/* 释放锁  */
  exit_files(tsk);/*  释放已经打开的文件   */
  exit_fs(tsk); /*  释放用于表示工作目录等结构*/
  if (group_dead)//脱离控制终端
    disassociate_ctty(1);
  exit_task_namespaces(tsk);//释放命名空间
  exit_task_work(tsk);
  exit_thread();//释放task_struct中的thread_struct结构
/*触发thread_notify_head链表中所有通知链实例的处理函数,用于处理struct thread_info结构体*/
  /*
   * Flush inherited counters to the parent - before the parent
   * gets woken up by child-exit notifications.
   *
   * because of cgroup mode, must be called before cgroup_exit()
   */
  perf_event_exit_task(tsk);//Performance Event功能相关资源的释放
  cgroup_exit(tsk);//Performance Event功能相关资源的释放
  /*
   * FIXME: do that only when needed, using sched_exit tracepoint
   */
  flush_ptrace_hw_breakpoint(tsk);//注销断点
  TASKS_RCU(tasks_rcu_i = __srcu_read_lock(&tasks_rcu_exit_srcu));
  exit_notify(tsk, group_dead);//更新所有子进程的父进程
  proc_exit_connector(tsk);//进程事件连接器(通过它来报告进程fork、exec、exit以及进程用户ID与组ID的变化
#ifdef CONFIG_NUMA//用于NUMA,当引用计数为0时,释放mempolicy结构体所占用的资源
  task_lock(tsk);
  mpol_put(tsk->mempolicy);
  tsk->mempolicy = NULL;
  task_unlock(tsk);
#endif
#ifdef CONFIG_FUTEX
  if (unlikely(current->pi_state_cache))
    kfree(current->pi_state_cache);
#endif
  /*
   * Make sure we are holding no locks:
   */
  debug_check_no_locks_held();
  /*
   * We can do this unlocked here. The futex code uses this flag
   * just to verify whether the pi state cleanup has been done
   * or not. In the worst case it loops once more.
   */
  tsk->flags |= PF_EXITPIDONE;
  if (tsk->io_context)//释放struct futex_pi_state结构体所占用的内存
    exit_io_context(tsk);
  if (tsk->splice_pipe)//释放与进程描述符splice_pipe字段相关的资源
    free_pipe_info(tsk->splice_pipe);
  if (tsk->task_frag.page)
    put_page(tsk->task_frag.page);
  validate_creds_for_do_exit(tsk);
  check_stack_usage();//检查有多少未使用的进程内核栈
  preempt_disable();
  if (tsk->nr_dirtied)
    __this_cpu_add(dirty_throttle_leaks, tsk->nr_dirtied);
  exit_rcu();
  TASKS_RCU(__srcu_read_unlock(&tasks_rcu_exit_srcu, tasks_rcu_i));
  /*
   * The setting of TASK_RUNNING by try_to_wake_up() may be delayed
   * when the following two conditions become true.
   *   - There is race condition of mmap_sem (It is acquired by
   *     exit_mm()), and
   *   - SMI occurs before setting TASK_RUNINNG.
   *     (or hypervisor of virtual machine switches to other guest)
   *  As a result, we may become TASK_RUNNING after becoming TASK_DEAD
   *
   * To avoid it, we have to wait for releasing tsk->pi_lock which
   * is held by try_to_wake_up()
   */
  smp_mb();
  raw_spin_unlock_wait(&tsk->pi_lock);
  /* causes final put_task_struct in finish_task_switch(). */
  tsk->state = TASK_DEAD;//调度其它进程
  tsk->flags |= PF_NOFREEZE;/*重新调度,因为该进程已经被设置成了僵死状态,因此永远都不会再把它调度回来运行了,也就实现了do_exit不会有返回的目标    */  /* tell freezer to ignore us */
  schedule();
  BUG();
  /* Avoid "noreturn function does return".  */
  for (;;)
    cpu_relax();  /* For when BUG is null */
}


目录
相关文章
|
4月前
|
并行计算 Java 应用服务中间件
剑指JUC原理-1.进程与线程
剑指JUC原理-1.进程与线程
25 0
|
5月前
|
负载均衡 JavaScript 算法
Node.js 多进程的概念、原理、优势以及如何使用多进程来提高应用程序的性能和可伸缩性
Node.js 多进程的概念、原理、优势以及如何使用多进程来提高应用程序的性能和可伸缩性
44 1
|
1天前
|
存储 Linux 程序员
【Linux-14】进程地址空间&虚拟空间&页表——原理&知识点详解
【Linux-14】进程地址空间&虚拟空间&页表——原理&知识点详解
|
1天前
|
Unix Linux
【Linux】一文了解【进程优先级相关知识点】&【PRI / NI值】背后的修正原理(13)
【Linux】一文了解【进程优先级相关知识点】&【PRI / NI值】背后的修正原理(13)
|
1天前
|
Linux Shell 调度
【Linux系列】fork( )函数原理与应用详解——了解【父子进程及其特性】(代码演示,画图帮助理解,思维导图,精简)(11)
【Linux系列】fork( )函数原理与应用详解——了解【父子进程及其特性】(代码演示,画图帮助理解,思维导图,精简)(11)
|
3天前
|
存储 算法 Linux
【计算机操作系统】深入探究CPU,PCB和进程工作原理
【计算机操作系统】深入探究CPU,PCB和进程工作原理
|
2月前
|
存储 安全 Linux
深入Linux进程内核:揭开进程工作原理的神秘面纱
深入Linux进程内核:揭开进程工作原理的神秘面纱
55 0
|
4月前
|
算法 Unix Linux
进程原理及系统调用
进程原理及系统调用
|
4月前
|
算法 Linux 调度
内核:linux进程原理
内核:linux进程原理
28 0
|
5月前
|
Linux 调度 Windows
【Linux系统化学习】探索进程的奥秘 | 第一个系统调用
【Linux系统化学习】探索进程的奥秘 | 第一个系统调用