日期:2014-05-16  浏览次数:20840 次

UNIX进程的创建,进程链和进程扇
进程扇的 例子:

/* 由一个进程派生多个子进程 */

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>


int main(void)
{
        int i;
        pid_t pid;

        printf("This is a example\n");
        for (i=0 ;i<10; i++)
        {
                pid=fork();
                if (pid == 0)
                        break;
        }
        printf("=============================================\n");
        printf ("My pid is %ld\n",getpid());
        printf ("My parent pid is %ld\n",getppid());

}


进程链的例子:

/* 由父进程派生子进程,子进程再派生子进程 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(void)
{
        pid_t pid;
        int i;

        for(i=0;i<10;i++)
                if ((pid = fork()) <= 0)
                        break;

        printf("=============================================\n");
        printf("My pid is %ld\n",getpid());
        printf("My parent pid is %ld\n",getppid());

        return 0;

}