#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <time.h>

int main()
{
    
    FILE *fp = fopen("test.txt", "wt");  //저장할 파일 생성.
    
    int i;
    int j;
    int k;
    int status;
    time_t rawtime;                     // 시간 출력
    struct tm *timeinfo;
    
    int dead_child_pid;
    time(&rawtime);
    timeinfo = localtime(&rawtime);
    
    pid_t pid;
    pid_t pid_child;                    //   부모와 자식의 pid확인 및 pid리턴값
    pid_t pid_parent;
    pid_t a[10];
    
    
    
    for(i=0; i<5;i++)
    {
        pid = fork();
        
        
        
        if (pid == -1)
        {
            fprintf(stderr, "fork failed");
            return 1;
        }
        else if(pid==0)
        {
            
            for(j=0;j<5;j++)
            {
                printf("[%dprocess] #%d\n",getpid(),j);
            }
            return 0;
        }
        else
        {
            /*    while((dead_child_pid = waitpid(-1, &status, WNOHANG)) >0)
            {
                printf("%d [child] 종료. %d [부모]n", dead_child_pid, getpid());
            }       1번
            */
            /*   while(wait(NULL)!=-1 )
            {
                printf("%d [child] 종료. %d [부모]n",wait(NULL),getpid());
            } 2번
            */
            
            while((dead_child_pid = wait(NULL)) != -1) {
                printf("%d [child] 종료. %d [부모]\n", dead_child_pid, getpid());
            }
        }
        
    }
    
    return 0;
}