1、用进程相关API函数编程一个程序,使之产生一个进程扇:父进程产生一系列子进程,每个子进程打印自己的PID然后退出。要求父进程最后打印PID。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(int argc, char const *argv[])
{
pid_t x;
// 1.输入子进程个数
int n;
printf("pls input child process num: ");
scanf("%d", &n); while(getchar()!='\n');
//2.产生n个子进程
int i;
for(i=0; i<n; i++)
{
x=fork(); //父进程继续派生子进程,子进程不需要
if(x>0)//父进程
{
continue;
}
if(x==0)//子进程
{
break;
}
}
//从循环出来后,父进程等待子进程结束后,最后打印pid&#x