pipe管道

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


int main()
{
    int fd[2];
    char rbuf[4]={0};
    char* wbuf[256];
    pid_t pid;
    int childexit=0;
    int i;
    int cmd;
    if (pipe(fd) == -1)
    {
        perror("fd");
exit(0);
    }
    if ((pid = fork()) == 0)
    {
        printf("\n");
close(fd[1]);
sleep(1);
        while(!childexit)
       {
        read(fd[0], rbuf, 4);
cmd=atoi(rbuf);
if (cmd == 0)
{
   printf("child du over,exitd\n");
   childexit=1;
}
else if (handle(cmd) != 0)
return;
sleep(1);
    }
    close(fd[0]);
    exit(0);
    }
    else
    {
        close(fd[0]);
        wbuf[0]="111";
wbuf[1]="050";
wbuf[2]="211";
wbuf[3]="000";
        for(i=0; i<4; i++)
   write(fd[1], wbuf[i], 4);
close(fd[1]);
    }
}
int handle(int cmd)
{
    if((cmd<0) || (cmd>256))
    {
        printf("child:cmddddd\n");
return -1;
    }
    printf("child:from pa %d\n", cmd);
    return 0;
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值