fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <sys/types.h>
  5. #include <time.h>
  6.  
  7. int main()
  8. {
  9.  
  10. FILE *fp = fopen("test.txt", "wt"); //저장할 파일 생성.
  11.  
  12. int i;
  13. int j;
  14. int k;
  15. int status;
  16. time_t rawtime; // 시간 출력
  17. struct tm *timeinfo;
  18.  
  19. int dead_child_pid;
  20. time(&rawtime);
  21. timeinfo = localtime(&rawtime);
  22.  
  23. pid_t pid;
  24. pid_t pid_child; // 부모와 자식의 pid확인 및 pid리턴값
  25. pid_t pid_parent;
  26. pid_t a[10];
  27.  
  28.  
  29.  
  30. for(i=0; i<5;i++)
  31. {
  32. pid = fork();
  33.  
  34.  
  35.  
  36. if (pid == -1)
  37. {
  38. fprintf(stderr, "fork failed");
  39. return 1;
  40. }
  41. else if(pid==0)
  42. {
  43.  
  44. for(j=0;j<3;j++)
  45. {
  46. printf("[%dprocess] #%d\n",getpid(),j);
  47. }
  48. return 0;
  49. }
  50. }
  51.  
  52. while((dead_child_pid = wait(NULL)) != -1) {
  53. printf("%d [child] 종료. %d [부모]\n", dead_child_pid, getpid());
  54. }
  55.  
  56. return 0;
  57. }
  58.  
Success #stdin #stdout 0s 1964KB
stdin
Standard input is empty
stdout
[5058process] #0
[5058process] #1
[5058process] #2
[5059process] #0
[5059process] #1
[5059process] #2
[5057process] #0
[5057process] #1
[5057process] #2
[5056process] #0
[5056process] #1
[5056process] #2
[5055process] #0
[5055process] #1
[5055process] #2
5058 [child] 종료. 5052 [부모]
5059 [child] 종료. 5052 [부모]
5057 [child] 종료. 5052 [부모]
5056 [child] 종료. 5052 [부모]
5055 [child] 종료. 5052 [부모]