#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main()
{
pid_t pid=0;
if((pid=fork())>0)
printf("I am the parent,my pid %u,my child's pid=%u/n",getpid(),pid);
else if (pid==0)
printf("I am the child,my pid=%u,my parent's id=%u/n",getpid(),getppid());
else
{
perror("fork");
return 1;
}
return 0;
}
#include <stdlib.h>
#include <unistd.h>
int main()
{
pid_t pid=0;
if((pid=fork())>0)
printf("I am the parent,my pid %u,my child's pid=%u/n",getpid(),pid);
else if (pid==0)
printf("I am the child,my pid=%u,my parent's id=%u/n",getpid(),getppid());
else
{
perror("fork");
return 1;
}
return 0;
}
博客展示了一段C语言代码,包含stdio.h、stdlib.h和unistd.h头文件,在main函数中使用fork函数创建子进程,通过判断返回值区分父进程和子进程,并分别输出进程信息,若出错则输出错误信息。





