/*
* function: fork一个子进程,并打印其相应pid
*
* 2020-12-01
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
printf("===before fork-1===\n");
printf("===before fork-2===\n");
printf("===before fork-3===\n");
pid_t pid = fork();
if (pid < 0) // 错误
{
perror("fork");
}
else if (pid == 0) // 子进程
{
printf("I'm Child, I'm pid = %d, my parent pid = %d\n", getpid(), getppid());
}
else // 父进程
{
printf("I'm parent, I'm pid = %d, I'm child pid = %d\n", getpid(), pid);
}
printf("===after fork===\n");
return 0;
}
fork.cpp
最新推荐文章于 2023-06-05 15:58:54 发布