进程学习:进程间通信(传统通信方式)1.无名管道

无名管道的特点:

1.适用场景:只能用于具有亲缘关系的两个进程之间;
2.通信模式:半双工模式,具有固定的读端(0)和写端(1);
3.读写方式:用文件io,不支持lseek;
4.读阻塞:当管道中无数据时;
写阻塞:当管道中写满数据时;
5.管道大小:无名管道大小64k;
6.管道破裂:关闭读端,仍然想管道中写入数据时;
对管道进行写的进程会收到内核发来SIGPIPE的信号,可以捕获;

来,我们用无名管道先写一个回显程序:

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

#define N 32

int main(int argc, const char *argv[])
{
    int pipefd[2];
    int pid;

    if(pipe(pipefd) < 0)
    {
        perror("pipe error");
        exit(1);
    }

    if( (pid = fork()) < 0)
    {
        perror("fork error");
        exit(1);
    }
    else if(pid == 0)
    {
        char buf[N];
        int ret;

        close(pipefd[0]);

        puts("im chd start");
        while(fgets(buf, N, stdin) != NULL)
        {
            write(pipefd[1], buf, N);
            if(strncmp(buf, "quit", 4) == 0)
            {
                break;
            }
        }
        puts("im chd end");

        exit(2);
    }
    else if(pid > 0)
    {
        int chdstatus, out_pid, ret;
        char buf[N];

        close(pipefd[1]);

        puts("im farther start");
        //父进程在这里会阻塞,应为管道的特点中有一条“当管道中无数据时,会读阻塞”,
        //程序启动时,如果父进程现将占到系统资源执行到这里仍然会阻塞,因为管道中没
        //有数据。所以父进程要想执行,必须依赖于子进程向管道中写入数据;

        //但是如果是读取一个文件,文件为空,此时read等于0,则不会执行while循环;
        //在这里主要提一下管道无数据,会读阻塞;
        while(read(pipefd[0], buf, N) > 0)
        {
            puts("im if");
            if(strncmp(buf, "quit", 4) == 0)
            {
                exit(1);;
            }

            fputs(buf, stdout);
        }
        puts("im farther end");

        out_pid = waitpid(pid, &chdstatus, 0);
        printf("outpid = %d\n", out_pid);

        if(WIFEXITED(chdstatus))
        {
            printf("exit_num = %d\n", WEXITSTATUS(chdstatus));
        }

        exit(0);
    }

    return 0;
}

下面我们用无名管道来实现一个文件服务器的功能

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值