#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>

int main()
{
        pid_t pid;
        char *m;
        int n;
        pid=fork();
        if(pid==0)
        {
                m="this is child";
                n=5;
        }
        else
        {
                m="this is father";
                n=3;    
        }

        for(;n>0;n--)
        {

                puts(m);
                sleep(1);
        }
        if(pid!=0)
        {
            pid_t child_pid;
            child_pid=wait(NULL);
            
            printf("child has finished: %d\n",child_pid);
        
        }
        exit(0);

}

相关文章: