码迷,mamicode.com
首页 > 系统相关 > 详细

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

时间:2016-08-31 02:14:28      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:

#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 |pstree

bash───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 5

pstree 

bash───fork_fan───5*[fork_fan]

 

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

标签:

原文地址:http://www.cnblogs.com/peixiguang/p/5824261.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!