23,121
社区成员
发帖
与我相关
我的任务
分享
int main()
{
pid_t child1;
pid_t child2;
child1 = fork();
child2 = fork();
if(child1 == 0)
{
printf("Child1:a\n");
return 0;
}
if(child2 == 0)
{
printf("Child2:b\n");
return 0;
}
else
{
waitpid(child1,NULL,0);
waitpid(child2,NULL,0);
printf("Parent:c\n");
}
return 0;
}
for (int i = 0; i<2; i++)
{
pid = fork();
if(pid <0){ }
else if (pid == 0){ }
else{ }
}
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/times.h>
#include <limits.h>
#include <sys/resource.h>
int main()
{
pid_t pid,pid1;
int i;
int status;
//printf("before fork, pid = %d\n",getpid());
pid = fork();
if( pid ==0 )
{
//printf("in child, pid = %d \n", getpid());
//execl("/bin/ps", "ps", "-o", "pid,ppid,pgrp,session,tpgid,comm",NULL);
execl("./st","st","5",NULL);
exit(0);
}
else
{
//printf("in father ,pid= %d\n", getpid());
sleep(1);
kill(pid,SIGTSTP);
waitpid(pid,&status,WUNTRACED);
pid1 = fork();
if (pid1 == 0)
{
execl("./st","st","5",NULL);
exit(0);
}
else
{
sleep(1);
for( i = 0; i < 5; i ++)
{
kill(pid1,SIGTSTP);
waitpid(pid1,&status,WUNTRACED);
kill(pid,SIGCONT);
sleep(1);
kill(pid,SIGTSTP);
waitpid(pid,&status,WUNTRACED);
kill(pid1,SIGCONT);
sleep(1);
}
}
waitpid(pid, &status,0);
}
return 0;
}