在下面的程序中,我错误地在调用fork并将返回值分配给childpid(第18行)时引入了一个错误。fork()返回值bug
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <sys/types.h>
4 #include <stdlib.h>
5 #include <string.h>
6
7 int main(){
8
9 int fd[2], nbytes;
10 pid_t childpid = -1;
11 char string[] = "Hello, world!";
12 char readbuffer[80];
13
14 pipe(fd);
15 printf("Parent: Beginning of Program...\n");
16
17
18 if(childpid = fork() == -1){ // BUG-FIX: Missing parenthesis (childpid=fork())==-1
19 printf("childpid == -1\n");
20 perror("fork");
21 exit(1);
22 }
23 if(childpid == 0){
24 // child process closes up input of pipe
25 close(fd[0]);
26
27 // send string through output side of pipe
28 write(fd[1], string, (strlen(string)+1));
29 printf("Child %d: Finished writing to pipe!\n",childpid);
30 exit(0);
31 }
32 else{
33 // parent closes output side of pipe
34 close(fd[1]);
35
36 // read in a string from the pipe
37 nbytes = read(fd[0], readbuffer, sizeof(readbuffer));
38 printf("Parent %d: Received string: %s\n", childpid,readbuffer);
39 }
40
41
42 printf("Parent %d: End of program\n", childpid);
43 return 0;
44 }
马车输出是:
$ ./a.exe
Parent: Beginning of Program...
Child 0: Finished writing to pipe!
运行它多次,我注意到的是从来没有达到else块。这意味着某种程度上,childpid在父进程中从来没有被赋值> 0。这很奇怪,因为childpid初始化为-1开始,fork发生了(这就是为什么childpid在子进程中得到0值)但父进程'childpid从未得到> 0的值 - 为什么?
的修复ofcourse是围绕一个括号,这使输出的分配:
$ ./a
Parent: Beginning of Program...
Parent 106648: Received string: Hello, world!
Child 0: Finished writing to pipe!
Parent 106648: End of program
我知道了修复,但我是一个有点不清楚如何对自己的缺陷代码的输出解释!为什么childpid会在子进程中得到0,但在父进程中没有得到正值?