fork(1) download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. void son_function(void);
  5.  
  6. int main(void) {
  7. int i;
  8. int pid;
  9.  
  10. for (i = 0; i < 3; i++)
  11. {
  12. pid = fork();
  13.  
  14. if (pid == 0)
  15. {
  16. son_function();
  17. }
  18.  
  19. if (pid < 0)
  20. {
  21. exit(1);
  22. }
  23. }
  24. return 0;
  25. }
  26.  
  27. void son_function(void)
  28. {
  29. printf("my pid=%d\n", getpid());
  30. printf("%d: alpha\n", getpid());
  31. printf("%d: beta\n", getpid());
  32. printf("%d: charlie\n", getpid());
  33. exit(0);
  34. }
Success #stdin #stdout 0s 2248KB
stdin
Standard input is empty
stdout
my pid=2013
2013: alpha
2013: beta
2013: charlie
my pid=2012
2012: alpha
2012: beta
2012: charlie
my pid=2011
2011: alpha
2011: beta
2011: charlie