64,648
社区成员
发帖
与我相关
我的任务
分享
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(void)
{
pid_t pid;
int status;
int process_num = 5;
pid_t child_pids[process_num];
for(int i = 0; i<process_num; ++i){
pid = fork();
if(pid == 0)
{ //child process
printf(" %d %d \n", getpid(), getppid());
exit(0);
}
else if( pid > 0 )
{ //parent process
child_pids[i] = pid;
}
else{
printf("fork error:%s", strerror(errno));
exit(1);
}
}
for(int i = 0; i<process_num; ++i){
waitpid(child_pids[i],NULL , 0);
printf("process exit:%d\n", child_pids[i]);
}
return 0;
}
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(void)
{
pid_t pid;
int status;
pid = fork();
if(pid == 0)
{
printf(" %d %d \n", getpid(), getppid());
// 打印信息
}
else if( pid > 0 )
{
printf(" %d \n", getpid());
// 打印信息
}
else
// failed
waitpid(pid ,NULL , 0);
return 0;
}
[/quote]
只创建了一个子进程嘛.....要两个以上
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(void)
{
pid_t pid;
int status;
pid = fork();
if(pid == 0)
{
printf(" %d %d \n", getpid(), getppid());
// 打印信息
}
else if( pid > 0 )
{
printf(" %d \n", getpid());
// 打印信息
}
else
// failed
waitpid(pid ,NULL , 0);
return 0;
}