进程创建fork()

简单进程创建例子:

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

int main()
{
    pid_t pid;
    pid_t ppid;
    switch(pid = fork())//子进程返回0,父进程返回子进程的进程ID
    {
        case -1:
            return -1;
        case  0:
            printf ("in child pid = %d\n",getpid());
            printf ("in child parent pid = %d\n",getppid());
            return 0;
        default:
            printf ("in parent child pid = %d\n",pid);
            printf ("in parent pid = %d\n",getpid());
            wait (&pid);
            wait (&ppid);
            return 0;
    }
}

 

posted @ 2018-11-19 12:08  cicero  阅读(231)  评论(0编辑  收藏  举报