c中fork()的输出 [英] Output from fork() in c

查看:116
本文介绍了c中fork()的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近遇到了这段代码,但并没有完全理解它.

I recently ran into this piece of code, and don't fully understand it.

  1. 什么情况下会导致pid == 0?
  2. 为什么wait(NULL)会使程序进入if(pid == 0)

基本上,我不完全理解下面的输出.任何帮助,将不胜感激.谢谢.

Basically I don't fully understand the output below. Any help would be appreciated. Thank you.

代码:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> // standard POSIX header file
#include <sys/wait.h> // POSIX header file for 'wait' function
int main(void)
{
    int i = -1;
    int pid;
    pid = getpid();
    fprintf(stdout, "parent pid = %d\n", pid);
    pid = fork();

    if (pid == 0)
    {
        for (i = 0; i < 10; ++i)
        {
            fprintf(stdout, "child process: %d\n", i);
            sleep(1);
        }
        exit(0);
    }
    else
    {
        fprintf(stdout, "child pid = %d\n", pid);
        fprintf(stdout, "waiting for child\n");
        wait(NULL);
        fprintf(stdout, "child terminated\n");
    }
    fprintf(stdout, "parent terminating\n");
    return 0;
}

输出:

parent pid = 2896
child pid = 5840
waiting for child
child process: 0
child process: 1
child process: 2
child process: 3
child process: 4
child process: 5
child process: 6
child process: 7
child process: 8
child process: 9
child terminated
parent terminating

推荐答案

  1. fork(2)系统调用将创建第二个子进程,因此,该调用在父级中被调用一次,但在父级和子级中均返回.

  1. The fork(2) system call creates a second, child process and as a consequence, the call is called once in the parent, but returns in both, the parent and the child.

由于这一点,调用后的代码需要一些指示来知道我们是在父级还是子级中执行代码,因为这允许父级和子级从此刻开始偏离通用代码.

Due to this, the code following the call needs some indication to know if we are executing code in the parent or the child, because this allows parent and child to divert from the common code from this point on.

fork(2)的定义(在手册页中有说明):

The definition of fork(2) and it is stated in the manual page is:

  • 返回-1并将errno设置为指示fork(2)调用失败原因的值.
  • 为子子流程返回0.
  • pid_t子进程ID返回给父进程(一个正数),以便它可以知道刚刚启动的新子进程的pid.
  • to return -1 and set errno to a value indicating why the fork(2) call failed.
  • to return 0 for the child subprocess.
  • to return the pid_t child process id to the parent process (a positive number) so it can know the pid of the new subprocess just started.

wait(NULL);不在if (pid == 0)中,而是在else部分中,因此它确实在if (pid != 0)或父进程中.出于以下几个原因,父进程必须知道子进程是否已完成以及如何完成同步,并且必须与之同步:

the wait(NULL); is not in the if (pid == 0), but in the else part, so it is indeed in the if (pid != 0) or in the parent process. The parent process must know if the child has finished and how, and must synchronise with it, for several reasons:

  • 孩子可以在父母之前exit(2),但是根据孩子的工作量,孩子可以在父母完成之后做.如果父母在孩子之前结束,您将再次得到shell提示,并且在发出提示之后,孩子的输出将使屏幕模糊.这不是您通常需要的,因此在父级中进行wait(2)调用只是礼貌,因此外壳程序仅在一切完成后提示您.

  • the child can exit(2) before the parent, but depending on the child's amount of work, it can do after the parent finishes. In case the parent ends before the child, you'll get the shell prompt again, and your screen will be blurred with the output of the child, right after the prompt has been issued. This is not what you normally desire, so a wait(2) call in the parent is just polite, so the shell only prompts you after everything has finished.

一个很好的测试是在示例中注释wait(2)调用,看看会发生什么.

A good test is to comment the wait(2) call in your example and see what happens.

wait(2)系统调用是一种了解孩子如何结束运行的方式(主要是如果您exec(2)孩子的进程中有其他程序),您可以了解孩子是否因为它exit(2)而死亡ed(并接收其退出代码),因为它被打断了(以及接收到的信号是什么),或者由于某种原因被用户/系统停止了.

the wait(2) system call is a means of knowing how your child ended it's run (mostly if you exec(2) some other program in the child's process) You can learn if your child died because it exit(2)ed (and receive its exit code), because it was interrupted (and what the signal it received was) or if it has been stopped by the user/system for some reason.

读取fork(2)exec(2)exit(2)wait(2)系统调用(全部作为一组相关调用)将启发所有进程关系以及所有这些系统调用如何协作以管理unix进程系统.

A reading of fork(2), exec(2), exit(2) and wait(2) system calls (all as a group of related calls) will enlighten all the process relationships and how all these system calls collaborate to manage the unix process system.

这篇关于c中fork()的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆