1个回答
展开全部
很简单,打印父进程pid,然后创建子进程,并打印子进程pid,然后再创建一个子进程,并打印pid。
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
int pid1, pid2;
printf("Parent's PID is %d\n", getpid());
pid1 = fork();
if (pid1 == 0)
{
printf("Child1's PID is %d\n", getpid());
exit(0);
}
else if(pid1 < 0)
{
printf("fork error!\n");
exit(1);
}
wait(NULL);
printf("Child1 (PID=%d) already exit\n", pid1);
pid2 = fork();
if (pid2 == 0)
{
printf("Child2's PID is %d\n", getpid());
exit(0);
}
else if(pid2 < 0)
{
printf("fork error!\n");
exit(1);
}
wait(NULL);
printf("Child2 (PID=%d) already exit\n", pid2);
return 0;
}
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
int pid1, pid2;
printf("Parent's PID is %d\n", getpid());
pid1 = fork();
if (pid1 == 0)
{
printf("Child1's PID is %d\n", getpid());
exit(0);
}
else if(pid1 < 0)
{
printf("fork error!\n");
exit(1);
}
wait(NULL);
printf("Child1 (PID=%d) already exit\n", pid1);
pid2 = fork();
if (pid2 == 0)
{
printf("Child2's PID is %d\n", getpid());
exit(0);
}
else if(pid2 < 0)
{
printf("fork error!\n");
exit(1);
}
wait(NULL);
printf("Child2 (PID=%d) already exit\n", pid2);
return 0;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询