5 次分叉后只有 1 child (C)
Only 1 child after 5 forks (C)
我正在尝试在 C 中创建一个处理器场。我从打开消息队列开始,然后尝试创建工作进程:(请注意 NROF_WORKERS 是 5)
static void
makechildren (void) {
// Only the parent should fork. Any children created will become workers.
pid_t processID;
pid_t farmerPID = getpid(); // To identify who the farmer is
// Loop creating processes, indexed by NROF_WORKERS
int i = 0;
while (i < NROF_WORKERS){
if (getpid() == farmerPID){
i++;
printf ("Parent is creating a child!%d\n", getpid());
processID = fork();
}
}
if (processID < 0){
perror("fork() failed");
exit(1);
}
else {
// If parent, start farming
if (processID == farmerPID) {
printf("Parent reporting in!%d\n");
}
// If child, become a worker
if (processID == 0) {
printf("Child reporting in!%d\n", getpid());
join();
}
}
}
如您所见,我希望 parent 在创建 child 时进行报告,之后我希望 parent 和所有 children报告。然而,这就是我得到的全部:
Parent is creating a child!11909
Parent is creating a child!11909
Parent is creating a child!11909
Parent is creating a child!11909
Parent is creating a child!11909
Child reporting in!11914
现在,我确实注意到 11909 和 11914 的差异是 5。所以我的问题是:是否创建了其他进程?如果是这样,他们怎么不报告?如果不是,我做错了什么?另外, parent 根本没有报告,这是怎么造成的?
所有 children 都已创建,但将在 while
循环中永远循环,因为 i
仅针对 parent:
int i = 0;
while (i < NROF_WORKERS){
if (getpid() == farmerPID){
i++; // <---- This is happening for the parent process only.
printf ("Parent is creating a child!%d\n", getpid());
processID = fork();
}
}
唯一要终止的child是最后一个,i
等于NROF_WORKERS
。
另外 parent 是 "not reporting",因为您要检查的 processID
等于 parent PID 永远不会等于它,因为它等于最新 fork
结果,即最新创建的 child PID:
.........
processID = fork();
.........
.........
if (processID == farmerPID) {
printf("Parent reporting in!%d\n");
}
你总是打印 farmerPid
!但是由于消息被打印了 5 次,您实际上创建了 5 个进程:
while (i < NROF_WORKERS){
if (getpid() == farmerPID){
i++;
printf ("Parent is creating a child!%d\n", getpid());
processID = fork();
}
}
如果你想打印 children pids 那么你的代码必须在 parent 和 child 之间有所不同,如:
while (i < NROF_WORKERS){
if (getpid() == farmerPID){
i++;
printf ("Parent is creating a child!\n");
processID = fork();
if (processID==0) { // child
printf("I am the child %d\n",getpid());
} else { // parent
printf("Parent just created child %d\n",processID);
}
}
}
我正在尝试在 C 中创建一个处理器场。我从打开消息队列开始,然后尝试创建工作进程:(请注意 NROF_WORKERS 是 5)
static void
makechildren (void) {
// Only the parent should fork. Any children created will become workers.
pid_t processID;
pid_t farmerPID = getpid(); // To identify who the farmer is
// Loop creating processes, indexed by NROF_WORKERS
int i = 0;
while (i < NROF_WORKERS){
if (getpid() == farmerPID){
i++;
printf ("Parent is creating a child!%d\n", getpid());
processID = fork();
}
}
if (processID < 0){
perror("fork() failed");
exit(1);
}
else {
// If parent, start farming
if (processID == farmerPID) {
printf("Parent reporting in!%d\n");
}
// If child, become a worker
if (processID == 0) {
printf("Child reporting in!%d\n", getpid());
join();
}
}
}
如您所见,我希望 parent 在创建 child 时进行报告,之后我希望 parent 和所有 children报告。然而,这就是我得到的全部:
Parent is creating a child!11909
Parent is creating a child!11909
Parent is creating a child!11909
Parent is creating a child!11909
Parent is creating a child!11909
Child reporting in!11914
现在,我确实注意到 11909 和 11914 的差异是 5。所以我的问题是:是否创建了其他进程?如果是这样,他们怎么不报告?如果不是,我做错了什么?另外, parent 根本没有报告,这是怎么造成的?
所有 children 都已创建,但将在 while
循环中永远循环,因为 i
仅针对 parent:
int i = 0;
while (i < NROF_WORKERS){
if (getpid() == farmerPID){
i++; // <---- This is happening for the parent process only.
printf ("Parent is creating a child!%d\n", getpid());
processID = fork();
}
}
唯一要终止的child是最后一个,i
等于NROF_WORKERS
。
另外 parent 是 "not reporting",因为您要检查的 processID
等于 parent PID 永远不会等于它,因为它等于最新 fork
结果,即最新创建的 child PID:
.........
processID = fork();
.........
.........
if (processID == farmerPID) {
printf("Parent reporting in!%d\n");
}
你总是打印 farmerPid
!但是由于消息被打印了 5 次,您实际上创建了 5 个进程:
while (i < NROF_WORKERS){
if (getpid() == farmerPID){
i++;
printf ("Parent is creating a child!%d\n", getpid());
processID = fork();
}
}
如果你想打印 children pids 那么你的代码必须在 parent 和 child 之间有所不同,如:
while (i < NROF_WORKERS){
if (getpid() == farmerPID){
i++;
printf ("Parent is creating a child!\n");
processID = fork();
if (processID==0) { // child
printf("I am the child %d\n",getpid());
} else { // parent
printf("Parent just created child %d\n",processID);
}
}
}