fork download
  1. /* main program illustrating the UNIX fork() system call.
  2. Compile using cc -o main main.c
  3. */
  4. #include <stdio.h>
  5. main() {
  6. for(int i=0;i<8;i++) // loop will run 8 times
  7. {
  8. if(fork() == 0)
  9. {
  10. printf("I am child number: %d and pid : %d\n", i+1, getpid());
  11. exit(0);
  12. }
  13. }
  14. for(int i=0;i<8;i++) // loop will run 8 times
  15. wait(NULL);
  16. }
Success #stdin #stdout 0.01s 5456KB
stdin
Standard input is empty
stdout
I am child number: 7 and pid : 15555
I am child number: 8 and pid : 15556
I am child number: 6 and pid : 15554
I am child number: 5 and pid : 15553
I am child number: 4 and pid : 15552
I am child number: 3 and pid : 15551
I am child number: 2 and pid : 15550
I am child number: 1 and pid : 15549