#include <iostream>
#include <stdio.h>
#include <unistd.h>
int main(int argc, const char * argv[]) {
// insert code here...
std::cout << "Hello, World!\n";
printf("fork before\n");
pid_t pid = fork();
if (pid == -1){
printf("fork error\n");
}else if (pid == 0){
printf("I am child process, pid=%d, ppid=%d\n", getpid(), getppid());
}else {
printf("I am parent process, child_pid=%d, pid=%d, ppid=%d\n",pid, getpid(), getppid());
}
printf("fork after\n");
return 0;
}
输出如下:
Hello, World!
fork before
I am parent process, child_pid=39732, pid=39731, ppid=39727
fork after
Hello, World!
fork before
I am child process, pid=39732, ppid=1
fork after