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

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

 

转载于:https://www.cnblogs.com/peixiguang/p/5824261.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值