Linux下pipe通信实例

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/sem.h>
#include <sys/mman.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <stropts.h>
#include <poll.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <procfs.h>
#include <time.h>
#include <dirent.h>
#include <pwd.h>

int main(int argc, char** args){
    int size;
    int new_size;
    int fd_num;
    struct pollfd *fds;
    struct pollfd *new_fds;
    int pfd[2];
    int tmp_pfd[2];
    pid_t sub_pid;
    char buf[100]="Im Child.";
    char ans[100];
    int r,i;
    int create_flag = 1;
    int close_flag = 1;
    int FD_ADD_NUM = 200;
    if( argc == 1 ) FD_ADD_NUM = 200;
    if( argc == 2 ) FD_ADD_NUM = atoi(args[1]);
    fd_num = FD_ADD_NUM;
    if( fd_num <= 0 )return 1;
    size = fd_num * sizeof(struct pollfd);
    fds = (struct pollfd *)malloc( size );
    if( fds == NULL )return 2;
    memset((char *)fds,0x00,size);
    if ( pipe(pfd) == -1 ) {
        return 3;
    }
    
    sub_pid = fork();
    if ( sub_pid == -1 ) {
        return 4;
    }
    if ( sub_pid == 0 ) {
        /* sub process */
        close(pfd[0]);
        free(fds);
        sub_pid = fork();
        if ( sub_pid == 0 ) {
            /* sub sub process */
            close(pfd[1]);
            sleep(10000);
            switch(errno) {
            case EAGAIN:
                printf("sleep EAGAIN\n");
                break;
            case EINTR:
                printf("sleep EINTR\n");
                break;
            default:
                printf("sleep errno=%d\n",errno);
                break;
            }
            printf("sleep is stopped!\n");
            return 0;
        }
        while(1)
        {
            write(pfd[1], &buf, 10);
            sleep(1);
            switch(errno) {
            case EAGAIN:
                printf("sleep EAGAIN\n");
                break;
            case EINTR:
                printf("sleep EINTR\n");
                break;
            default:
                break;
            }
        }
    }
    close(pfd[1]);
    fds[0].fd = pfd[0];
    fds[0].events = POLLIN;
    fds[0].revents = 0;
    while(1){
        if(create_flag)
        {
            for(i=1; i<FD_ADD_NUM ; i++) {
                if ( pipe(tmp_pfd) == -1 ) {
                    printf("pipe errno=%d\n",errno);
                    return 5;
                }
                close(tmp_pfd[1]);
                fds[i].fd = tmp_pfd[0];
                fds[i].events = POLLIN;
                fds[i].revents = 0;
            }
            create_flag = 0;
        }
        r = poll(fds, fd_num, INFTIM);
        if ( r == -1 ) {
            switch(errno) {
            case EAGAIN:
                printf("poll EAGAIN\n");
                continue;
            case EINTR:
                printf("poll EINTR\n");
                continue;
            default:
                printf("poll errno=%d\n",errno);
                printf("fd_num = %d\n",fd_num);
                return 5;
            }
        }
        for(i=0; i<fd_num ; i++) {
            if ( fds[i].revents & (POLLIN|POLLRDNORM) ) {
                r = read(fds[i].fd,&ans,10);
            }
            else if (fds[i].revents & POLLHUP ) {
                close(fds[i].fd);
                fds[i].fd = -1;
            }
        }
        if(close_flag){
            for(i=1; i<FD_ADD_NUM ; i++) {
                close(fds[i].fd);
            }
            close_flag = 0;
        }
        // malloc more
        new_size = size + FD_ADD_NUM * sizeof(struct pollfd);
        new_fds = (struct pollfd *)malloc( new_size );
        memcpy(new_fds, fds, sizeof(struct pollfd));
        memcpy(&new_fds[FD_ADD_NUM + 1], &fds[1], size-sizeof(struct pollfd));
        create_flag = 1;
        close_flag = 1;
        free(fds);
        size = new_size;
        fds = new_fds;
        fd_num += FD_ADD_NUM;
        if( fd_num % 6400 == 0 )
        {
            printf("fd_num = %d\n",fd_num);
        }
    }
    return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值