fork() in C. 我需要解释这段代码
fork() in C. I need explanation on this code
所以,我有这段C代码
我无法理解第二个 'for' 片段的内容。什么时候异常终止?
有人能给我讲讲吗?
#include<unistd.h>
#include<stdio.h>
#include <sys/wait.h>
#define N 30
int main() {
pid_t pid[N];
int i;
int child_status;
for (i = 0; i < N; i++) {
pid[i] = fork();
if (pid[i] == 0) {
sleep(60 - 2 * i);
exit(100 + i);
}
}
for (i = 0; i < N; i++) {
pid_t wpid = waitpid(pid[i], & child_status, 0);
if (WIFEXITED(child_status)) {
printf("Child%d terminated with exit status %d\n", wpid, WEXITSTATUS(child_status));
} else {
printf("Child%d terminated abnormally\n", wpid);
}
}
return (0);
}
当 child 终止时,为了能够找到 child 终止的值(使用 exit 或 return) 我必须将 waitpid() 中的第二个参数与指向 return 上那个整数中的 integer.So 的指针从调用中传递出来,它将包括 2 种类型信息
a) 如果 child 使用 return 正常终止或退出或意外停止
b) 第二种类型将具有终止值。
如果我想知道来自 (a) 的信息,我需要使用宏 WIFEXITED(),如果这为真,则 (b) 来自宏 WEXITSTATUS()。这是一个简单的例子
#include <stdio.h>
#include <stdlib.h> /* For exit() */
#include <unistd.h> /* For fork(), getpid() */
#include <sys/wait.h> /* For waitpid() */
void delay() { /* Just delay */
int i, sum=0;
for (i = 0; i < 10000000; i++)
sum += i;
printf("child (%d) exits...\n", getpid());
exit(5); /* Child exits with 5 */
}
int main() {
int pid, status;
pid = fork();
if (pid == 0) /* child */
delay();
printf("parent (%d) waits for child (%d)...\n", getpid(), pid);
waitpid(pid, &status, 0);
if (WIFEXITED(status)) /* Terminated OK? */
printf("child exited normally with value %d\n", WEXITSTATUS(status));
else
printf("child was terminated abnormaly.\n");
return 0;
}
SOS 宏 WEXITSTATUS() return 只有当 child 是 terminate.So 时值的 8 个最不重要的位child 想通过 exit/waitpid 对他的 parent 说点什么,必须是 255 以内的数字。
所以,我有这段C代码
我无法理解第二个 'for' 片段的内容。什么时候异常终止?
有人能给我讲讲吗?
#include<unistd.h>
#include<stdio.h>
#include <sys/wait.h>
#define N 30
int main() {
pid_t pid[N];
int i;
int child_status;
for (i = 0; i < N; i++) {
pid[i] = fork();
if (pid[i] == 0) {
sleep(60 - 2 * i);
exit(100 + i);
}
}
for (i = 0; i < N; i++) {
pid_t wpid = waitpid(pid[i], & child_status, 0);
if (WIFEXITED(child_status)) {
printf("Child%d terminated with exit status %d\n", wpid, WEXITSTATUS(child_status));
} else {
printf("Child%d terminated abnormally\n", wpid);
}
}
return (0);
}
当 child 终止时,为了能够找到 child 终止的值(使用 exit 或 return) 我必须将 waitpid() 中的第二个参数与指向 return 上那个整数中的 integer.So 的指针从调用中传递出来,它将包括 2 种类型信息 a) 如果 child 使用 return 正常终止或退出或意外停止 b) 第二种类型将具有终止值。 如果我想知道来自 (a) 的信息,我需要使用宏 WIFEXITED(),如果这为真,则 (b) 来自宏 WEXITSTATUS()。这是一个简单的例子
#include <stdio.h>
#include <stdlib.h> /* For exit() */
#include <unistd.h> /* For fork(), getpid() */
#include <sys/wait.h> /* For waitpid() */
void delay() { /* Just delay */
int i, sum=0;
for (i = 0; i < 10000000; i++)
sum += i;
printf("child (%d) exits...\n", getpid());
exit(5); /* Child exits with 5 */
}
int main() {
int pid, status;
pid = fork();
if (pid == 0) /* child */
delay();
printf("parent (%d) waits for child (%d)...\n", getpid(), pid);
waitpid(pid, &status, 0);
if (WIFEXITED(status)) /* Terminated OK? */
printf("child exited normally with value %d\n", WEXITSTATUS(status));
else
printf("child was terminated abnormaly.\n");
return 0;
}
SOS 宏 WEXITSTATUS() return 只有当 child 是 terminate.So 时值的 8 个最不重要的位child 想通过 exit/waitpid 对他的 parent 说点什么,必须是 255 以内的数字。