23,120
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <unistd.h>
int main(void)
{
pid_t pid;
if ((pid = fork()) < 0) {
printf("fork error\n");
} else if (pid == 0) { /* child process */
printf("child\n");
} else { /* parent process */
printf("parent\n");
}
return 0;
}
#include <stdio.h>
#include <unistd.h>
int main(void)
{
pid_t pid;
if ((pid = fork()) < 0) {
printf("fork error\n");
} else if (pid == 0) { /* child process */
printf("child\n");
exit(0); // jernymy 子进程退出
} else { /* parent process */
printf("parent\n");
wait(NULL); // jernymy 父进程等待子进程退出
}
return 0;
}