不明白我需要使用哪个逻辑来打开问题。我没有我需要的等级制度。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
//prototype(s)
void createChild(int numberOfChild);
//array with size
int main() {
int arr[] = {2, 3, 1, 0, 0, 0, 0}; // arr of integer number
int size = sizeof(arr) / sizeof(int); // number of element
for (int i = 0; i < size; i++) {
createChild(arr[i]);
}
return 0;
}
void createChild(int numberOfChild){
pid_t pid;
int i;
for (i = 0; i < numberOfChild; i++) {
pid = fork();
if (pid < 0) { printf("error. can't create process\n"); }
else if (pid == 0) { //child process
printf("[son] pid %d from [parent] pid %d\n", getpid(), getppid());
exit(0);
}
}
for (int i = 0; i < numberOfChild; ++i) {
wait(NULL);
}
}
给定一个整数数组,我需要编写一个程序,为每个父亲创建多个与数组中的数组相对应的儿子。 例如:如果数组是 [2, 3, 1,0,0,0,0]。 0号父亲有2个儿子,1号父亲有3个儿子,3号父亲有1个儿子,依此类推。对于每个进程,打印pid号和他父亲的pid号。父亲的计数是按表格级别顺序排列的。
需要用C写代码
我不想将共享内存与管道或其他东西一起使用