Hi boys, do you know how many processes the following codes produce? It is 2^5.
So we can get a conclusion which you have n amounts for-statement and you will
get 2^n processes. If you don't know the reason, send me a mail, which i will
describe the reasons in details ;-)
#include
#include
#include
int main(void)
{
int i = 0;
pid_t j;
for (i = 0; i < 5; i++) {
if (0 > (j = fork())) {
fprintf(stderr, "Fork Error!\n");
return 1;
}
if (0 == j) {
fprintf(stdout, "child: %d", getpid());
} else {
fprintf(stdout, "parent: %d\n", getpid());
}
}
return 0;
}
阅读(545) | 评论(0) | 转发(0) |