• OS | 第5章 插叙:进程API


    OS | 第5章 插叙:进程API


    UNXI 系统采用了一种非常有趣的创建新进程的方式,通过一对系统调用: fork()exec(),还可以通过第3个系统调用 wait(),来等待其创建的子进程执行完成。

    5.1 fork()系统调用

    系统调用 fork()用于创建新进程。

    #include 
    #include 
    #include 
    
    int main(int argc, char *argv[]) {
      printf("hello Randy (pid:%d) \n", (int)getpid());
      int rc = fork();
      if (rc < 0)  // fork failed; exit
      {
        fprintf(stderr, "fork failed!\n");
        exit(1);
      } else if (rc == 0) {  // child (new process)
        printf("hello, I am child (pid:%d)\n", (int)getpid());
      } else {  // parent goes down this path (main)
        printf("hello, I am parent of %d (pid:%d)\n", rc, (int)getpid());
      }
      return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    运行结果:

    hello Randy (pid:12242) 
    hello, I am parent of 12243 (pid:12242)
    hello, I am child (pid:12243)
    
    • 1
    • 2
    • 3

    代码过程分析

    1. 程序开始运行时, 进程输出一条 “hello Randy”信息,以及自己的进程描述符(process identifier, PID),该进程PID为12242;
    2. 进程调用了fork()系统调用,创建了1个新的线程。新创建的线程与调用线程几乎完全一样,对操作系统来说,有2个完全一样的程序在运行,并从fork()系统调用中返回;
    3. 新创建的进程称为子进程(child),原来的线程为父进程(parent)。子进程直接从fork()系统调用返回,好像自己调用了fork();
    4. 父进程和子进程接着分别运行剩余代码。

    子进程并不是完全拷贝父进程:虽然子进程有自己的地址空间(拥有自己的私有内存)、寄存器、程序计数器等,但是它从fork()返回的值是不同的。

    父进程获得的返回值是子进程的PID,子进程获得的返回值是0。

    CPU调度程序(scheduler)决定了某个时刻哪个进程被执行,所以父进程和子进程谁先打印不确定。

    5.2 wait()系统调用

    有时候父进程需要等待子进程执行完毕,由wait()系统调用完成

    #include 
    #include 
    #include 
    #include 
    
    int main(int argc, char *argv[]) {
      printf("hello Randy (pid:%d) \n", (int)getpid());
      int rc = fork();
      if (rc < 0)  // fork failed; exit
      {
        fprintf(stderr, "fork failed!\n");
        exit(1);
      } else if (rc == 0) {  // child (new process)
        printf("hello, I am child (pid:%d)\n", (int)getpid());
      } else {  // parent goes down this path (main)
        int wc = wait(NULL);
        printf("hello, I am parent of %d (wc:%d) (pid:%d)\n", rc, wc,  (int)getpid());
      }
      return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    执行结果:

    hello Randy (pid:14580) 
    hello, I am child (pid:14581)
    hello, I am parent of 14581 (wc:14581) (pid:14580)
    
    • 1
    • 2
    • 3

    父进程调用wait(),延迟自己的执行,直到子进程执行完毕,wait()才返回父进程。

    5.3 exec() 系统调用

    exec() 有几种变体:execl()、execle()、execlp()、execv()、execvp()。

    这个系统调用可以让子进程执行与父进程不同的程序。

    #include 
    #include 
    #include 
    #include 
    
    #include 
    
    int main(int argc, char *argv[]) {
      printf("hello Randy (pid:%d) \n", (int)getpid());
      int rc = fork();
      if (rc < 0)  // fork failed; exit
      {
        fprintf(stderr, "fork failed!\n");
        exit(1);
      } else if (rc == 0) {  // child (new process)
        printf("hello, I am child (pid:%d)\n", (int)getpid());
        char *myargs[3];
        myargs[0] = strdup("wc");        // program: "wc"(word count)
        myargs[1] = strdup("fork.cpp");  // program: "wc"(word count)
        myargs[2] = NULL;                // program: "wc"(word count)
        execvp(myargs[0], myargs);       // runs word count
        printf("this shouldn't print out");
      } else {                     // parent goes down this path (main)
        int wc = wait(NULL);
        printf("hello, I am parent of %d (wc:%d) (pid:%d)\n", rc, wc,  (int)getpid());
      }
      return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    执行结果:

    ./randy 
    hello Randy (pid:15334) 
    hello, I am child (pid:15335)
     27 117 916 fork.cpp
    hello, I am parent of 15335 (wc:15335) (pid:15334)
    
    • 1
    • 2
    • 3
    • 4
    • 5

    子进程调用execvp()运行字符计数程序wc。

    执行过程

    1. exec() 给定可执行程序的名称(如wc)及需要的参数后,会从可执行程序中加载代码和静态数据,并用它覆写自己的代码段(以及静态数据),堆、栈及其他内存空间也会被重新初始化。

    2. 操作系统执行该程序,将参数通过argv 传递给该进程。

    它并没有创建新进程,而是直接将当前运行的程序替换为不同的运行程序(wc)。

    子进程执行exec()之后,几乎就像源程序从未运行过一样,对exec() 的成功调用永远不会返回。

    为什么这样设计API?

    这种分离的fork()和exec()的做法在构建UNIX shell 的时候非常有用,这给了shell 在fork之后 exec 之前运行代码的机会,这些代码可以在运行新程序之前改变环境,从而让其他功能易于实现。

    比如:

    wc fork.cpp > count_fork_cpp.txt
    
    • 1

    shell执行过程

    shell 是一个用户程序(包括 tcsh、bash、zsh等)

    1. 首先显示一个提示符(prompt)
    2. 等待用户输入。输入命令或参数
    3. shell在文件系统中找到这个可执行程序,调用fork()创建新进程;
    4. 调用exec()的某个变体执行这个可执行程序
    5. 调用wait()等待该命令完成

    shell从wait()返回并再次输出一个提示符,等待用户输入下一条命令。

    shell 重定向

    上面的例子,wc的结果被重定向到count_fork_cpp.txt中。

    重定向的工作原理,是基于操作系统管理文件描述符方式的假设。

    UNIX系统从0开始寻找可以使用的文件描述符。

    下面的例子中STDOUT_FILENO将成为第一个可用的文件描述符,因此在open()被调用时,得到赋值。然后子进程向标准输出文件描述符的写入(如printf()),都会被透明地转向新打开的文件,若不是屏幕。

    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    int main(int argc, char *argv[]) {
      int rc = fork();
      if (rc < 0)  // fork failed; exit
      {
        fprintf(stderr, "fork failed!\n");
        exit(1);
      } else if (rc == 0) {  // child : redict standard  output to a file
        close(STDOUT_FILENO);
        open("./redirect.output", O_CREAT | O_WRONLY | O_TRUNC, S_IRWXU);
        
        // now exec "wc"
        printf("hello, I am child (pid:%d)\n", (int)getpid());
        char *myargs[3];
        myargs[0] = strdup("wc");        // program: "wc"(word count)
        myargs[1] = strdup("fork.cpp");  // program: "wc"(word count)
        myargs[2] = NULL;                // program: "wc"(word count)
        execvp(myargs[0], myargs);       // runs word count
        printf("this shouldn't print out");
      } else {                     // parent goes down this path (main)
        // printf("hello, I am parent of %d (pid:%d)\n", rc, (int)getpid());
        int wc = wait(NULL);
        printf("hello, I am parent of %d (wc:%d) (pid:%d)\n", rc, wc,  (int)getpid());
      }
      return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    执行结果:

    Randy@Jeff:~$ ./randy 
    hello, I am parent of 17118 (wc:17118) (pid:17117)
    Randy@Jeff:~$ cat redirect.output 
      46  227 1592 fork.cpp
    
    • 1
    • 2
    • 3
    • 4

    pipe()

    UNIX管道也是用类似的方式实现的,用的是pipe()系统调用。

    一个进程的输出被连接到一个内核管道(pipe)上(队列),另一个进程的输入也被连接到了同一个管道上。

    因此前一个进程的输出无缝地作为后一个进程的输入,许多命令可以用这种方式串联在一个,共同完成某项任务。如从一个文件中查找某个词,并统计其出现的次数:

    grep -o randy file | wc -l
    
    • 1

    >>>>> 欢迎关注公众号【三戒纪元】 <<<<<

  • 相关阅读:
    1-前端基本知识-HTML
    【Linux】页表讲解(一级、二级) 和 vm_area_struct ## 对于我前面博客内容的补充
    百面深度学习-循环神经网络
    SpringBoot 集成 RabbitMq 实现五种常用消息模型
    Windows应急响应信息采集工具
    动力电池:车企们的新角斗场
    CNN和RNN结合与对比,实例讲解
    python的/ 和// 学习
    22 年国内最牛的 Java 面试八股文合集(全彩版),不接受反驳
    实现流程自动化的警务反诈RPA:通过智能化技术提升警方反诈骗能力
  • 原文地址:https://blog.csdn.net/moneymyone/article/details/132687566