简体   繁体   English

简单的fork程序中无法预测的输出

[英]Unpredictable output in simple fork program

Pardon me community for asking such a silly question - I've already spent 5+ hours trying to figure out reason but I've failed. 请原谅我一个如此愚蠢的问题的社区-我已经花了5个多小时试图找出原因,但我失败了。

Below is the code, it was a question on our exam - we were asked if we can predict the output of this code and will it be definite? 以下是代码,这是我们考试中的一个问题-我们被问到是否可以预测此代码的输出,并且确定吗?

After running this code on several machines, I've observed that output everywhere is - " child parent" . 在多台机器上运行此代码后,我发现到处输出的都是-“ child parent”。

My understanding of below code is that once the process has forked, a copy of process will be created which can only execute the statements which are written below the fork() call only. 我对以下代码的理解是,一旦流程分叉,将创建流程的副本,该副本只能执行仅在fork()调用下编写的语句。 Now once fork() is done, there will be two processes - a child process & parent process . 现在完成fork()后,将有两个进程-子进程和父进程。 ( we've been instructed in our school that first parent process terminates and then child process when we are using pipes, however on searching internet I can only see that it cannot be decided which runs first. (我们学校曾指示我们在使用管道时首先终止父进程,然后终止子进程,但是在搜索互联网时,我只能看到无法确定哪个先运行。

Here is what I think should happen - 我认为这应该发生-

Scenario 1 - Child executes first 方案1-儿童先执行

I think that when child executes first, it will first print "child", then write to file descriptor and then child process terminates due to _exit(0) and then parent process proceeds, reads and prints Parent. 我认为,当child首先执行时,它将首先打印“ child”,然后写入文件描述符,然后由于_exit(0)终止子进程,然后父进程继续进行,读取并打印Parent。 This is what is happening. 这就是正在发生的事情。 I don't know whether this is actually happening or not. 我不知道这是否真的在发生。

Scenario 2 : Parent is executed first, 方案2:先执行父项,

now it's attempting to first read empty pipe. 现在,它尝试首先读取空管道。 Parent will try to read data, get blocked, then child will first print child, then write to pipe, which will enable read command and hence parent will be printed after it. 父级将尝试读取数据,被阻止,然后子级将首先打印子级,然后写入管道,这将启用读取命令,因此父级将在其后打印。

Can someone confirm if my understanding is correct and point out mistakes in my understanding.. 有人可以确认我的理解是否正确,并指出我的理解中的错误。

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/wait.h>
int BUFSIZE = 4096;
int main (int argc, char* argv[])
 {   int fd[2];
     pid_t pid;
     char buffer[BUFSIZE];
     pipe(fd);
     if ((pid = fork()) == 0)
    {   printf("child\n");
    write(fd[1], "a", 1);
     _exit(0);
     }
    read(fd[0], buffer, BUFSIZE);
    printf("parent\n");
     }

In your program, you have a pipe. 在您的程序中,您有一个管道。 The parent reads from one head of the pipe, and therefore he waits till someone writes on the other head of the pipe. 父从管道的一个磁头读取,因此,他等待 ,直到有人在管道的另一头写。 The child writes, so even if parent was to execute first, he would wait for child to write. 孩子会写,所以即使父母先执行,他也会等待孩子写。 Change this: 更改此:

read(fd[0], buffer, BUFSIZE);
printf("parent\n");

to

printf("parent\n");
read(fd[0], buffer, BUFSIZE);

and rerun it. 并重新运行它。 Now it will print before trying to read. 现在它将在尝试阅读之前打印。 Even so, from time to time, it may be the child the first who prints, and sometimes will be the parent. 即使这样,时不时地,孩子还是第一个打印的孩子,有时是父母的孩子。 It all depends who gets the processor first. 这完全取决于谁先获得处理器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM