首页 > 代码库 > 一、进程与信号的进程链与进程扇

一、进程与信号的进程链与进程扇

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

#include <stdio.h>#include <fcntl.h>#include <string.h>int main(int argc,char *argv[]){    pid_t pid;    int counter;    if(argc<2)    {        printf("usage: %s number\n",argv[1]);        exit(1);    }    counter=atoi(argv[1]);    if(counter<2)    {        counter =2;    }    int i=1;    for(;i<counter;i++)    {        pid=fork();        if(pid<0)        {            printf("fork error");            exit(1);        }        else if (pid >0)        {            break;        }            }    printf("progress pid: %d,ppid: %d\n",getpid(),getppid());    while(1)
{
//阻塞
    sleep(1);
}
close(pid);}

编译执行

ps -ef |grep fork_link |pstreebash───fork_link───fork_link───fork_link───fork_link子进程的链式结构

子进程扇式结构

#include <unistd.h>#include <stdlib.h>#include <stdio.h>#include <string.h>#include <fcntl.h>int main(int argc,char *argv[]){    int counter=0;    if(argc <2)    {        printf("usage: %s number\n",argv[1]);        exit(1);    }    counter=atoi(argv[1]);    if(counter<2)    {        counter=2;    }    pid_t pid;    int i=1;    for(;i<=counter;i++)    {        pid=fork();        if(pid<0)        {            printf("fork error");            exit(1);        }        else if(pid == 0)        {            //子进程退出,只留父进程,形成扇形             break;        }            }    printf("process pid: %d ppid: %d\n",getpid(),getppid());        while(1)    {        //阻塞         sleep(2);    }    close(pid);}

编译运行

./fork_fan 5pstree bash───fork_fan───5*[fork_fan]

 

一、进程与信号的进程链与进程扇