01/10/2018, 09:23
How does exit(0) in child process work?
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
#include <sys/wait.h>
int main() {
pid_t pid;
int num_coconuts = 17;
pid = fork();
if(pid == 0) {
num_coconuts = 42;
exit(0);
} else {
wait(NULL); /*wait until the child terminates */
}
printf("I see %d coconuts!
", num_coconuts);
exit(0);
}
What do the program display on the screen ? Why ?
Obvious,I run the program and know the result
The output is : I see 17 coconuts
But when I delete exit(0) in the if statement, the output is : I see 42
coconuts I see 17 coconuts
So,my question is how exit(0) effects to the child and parent process ?
Bài liên quan
https://linux.die.net/man/2/fork