Source files aren't supposed to be executables
[eleutheria.git] / ipc / fork_mult.c
blob3a0319b0553342434d9adbcc3f746f3e3a29d9ac
1 /* compile with:
2 gcc fork_mult.c -o fork_mult -Wall -W -Wextra -ansi -pedantic */
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <unistd.h>
7 #include <sys/wait.h>
9 #define NUM_CHILDS 5
11 int main(void)
13 pid_t pid[NUM_CHILDS];
14 int i, retval;
16 for (i = 0; i < NUM_CHILDS; i++) {
17 if ((pid[i] = fork()) < 0) { /* fork error */
18 perror("fork");
19 exit(EXIT_FAILURE);
22 else if (pid[i] > 0) { /* parent process */
23 wait(&retval);
26 else { /* child process (pid = 0) */
27 printf("Child[%d] = %d\n", i, getpid());
29 /* break, or else every child will spawn it's own children */
30 break;
33 return EXIT_SUCCESS;