more loops, higher count, use flush
This commit is contained in:
parent
240a5d5ebf
commit
4f2637ffc0
19
fork.c
19
fork.c
@ -3,25 +3,36 @@
|
|||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
#include <sys/wait.h>
|
||||||
|
|
||||||
|
#define LOOPS_PER_CHILD 2000
|
||||||
|
#define CHILDS 30
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
for (int start = 0; start < 50; start += 10) {
|
for (int start = 0; start < LOOPS_PER_CHILD * CHILDS; start += LOOPS_PER_CHILD) {
|
||||||
pid_t pid = fork();
|
pid_t pid = fork();
|
||||||
if (pid == -1) {
|
if (pid == -1) {
|
||||||
fprintf(stderr, "Error forking: %s\n", strerror(errno));
|
fprintf(stderr, "Error forking: %s\n", strerror(errno));
|
||||||
return 1;
|
return 1;
|
||||||
} else if (pid == 0) {
|
} else if (pid == 0) {
|
||||||
// I AM FORK!
|
// I AM FORK!
|
||||||
for (int i = start; i < start + 10; i++) {
|
pid_t childPid = getpid();
|
||||||
printf("%d: %d\n", pid, i);
|
|
||||||
|
for (int i = start; i < start + LOOPS_PER_CHILD; i++) {
|
||||||
|
fprintf(stdout, "%d: %d\n", childPid, i);
|
||||||
|
fflush(stdout);
|
||||||
}
|
}
|
||||||
printf("Fork %d finished counting from %d\n", getpid(), start);
|
printf("Fork %d finished counting from %d\n", childPid, start);
|
||||||
return 0;
|
return 0;
|
||||||
} else {
|
} else {
|
||||||
printf("Fork %d successfull\n", pid);
|
printf("Fork %d successfull\n", pid);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// wait for all childs to terminate
|
||||||
|
while (wait(NULL) > -1);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
|
||||||
fork();
|
fork();
|
||||||
fork();
|
fork();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user