无名管道

特点

  • 1、半双工,数据在同一时刻只能在一个方向上流动。
  • 2、数据只能从管道的一端写入,从另一端读出。同意进程可以访问管道两端,也可以自发自收。
  • 3、写入管道中的数据遵循先入先出的规则。若连续写入多次再读出,则将多次写入的内容一并读出。
  • 4、管道所传送的数据是无格式的,这要求管道的读出方与写入方必须事先约定好数据的格式,如多少字节算一个消息等。
  • 5、管道不是普通的文件,不属于某个文件系统,其只存在于内存中。
  • 6、管道在内存中对应一个缓冲区。不同的系统其大小不一定相同。
  • 7、从管道读数据是一次性操作,数据一旦被读走,它就从管道中被抛弃,释放空间以便写更多的数据。
  • 8、管道没有名字,只能在具有公共祖先的进程(父进程与子进程,或者两个兄弟进程,具有亲缘关系)之间使用。
#include <unistd.h>
/* 创建无名管道 */
int pipe(int fildes[2]);

/* 参数:
    filedes: 为 int 型数组的首地址,其存放了管道的文件描述符 filedes[0]、filedes[1]。
    当一个管道建立时,它会创建两个文件描述符 fd[0] 和 fd[1]。其中 fd[0] 固定用于读管道,而 fd[1] 固定用于写管道。一般文件 I/O 的函数都可以用来操作管道( lseek() 除外)。
*/

/* 返回值:
    0:成功
    1:失败
*/

例子

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

int main()
{
    int fds[2] = {0};
    pid_t pid;
    
    /* 创建一个管道 */
    if(pipe(fds) < 0)
    {
        perror("pipe");
        exit(-1);
    }

    pid = fork();
    if(pid<0)
    {
        perror("fork");
        exit(-1);
    }
    else if(pid == 0)
    {
        printf("子进程\n");
        char* str = "你好,世界!";
        write(fds[1],str,strlen(str));
        printf("写入管道:%s\n",str);
        close(fds[1]);
        exit(0);
    }
    else
    {
        wait(NULL);
        printf("父进程\n");
        char buf[64];
        memset(buf,0,sizeof(buf));
        read(fds[0],buf,sizeof(buf));
        printf("读出管道:%s\n",buf);
        close(fds[0]);
    }
    return 0;
}

执行可见,父进程可以从管道中读出子进程写入的 "你好,世界!"。

[lingyun@manjaro study]$ gcc study.cpp -o study
[lingyun@manjaro study]$ ./study
子进程
写入管道:你好,世界!
父进程
读出管道:你好,世界!
[lingyun@manjaro study]$ 

阻塞的特点

默认的情况下,从管道中读写数据,最主要的特点就是阻塞问题(这一特点应该记住),当管道里没有数据,另一个进程默认用 read() 函数从管道中读数据是阻塞的。当然,我们编程时可通过 fcntl() 函数设置文件的阻塞特性。

设置为阻塞:  fcntl(fd, F_SETFL, 0);
设置为非阻塞:fcntl(fd, F_SETFL, O_NONBLOCK);

此外 write() 函数也会阻塞,调用 write() 函数向管道里写数据,当缓冲区已满时 write() 也会阻塞。

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

int main()
{
    int fds[2] = {0};
    pid_t pid;

    if(pipe(fds) < 0)
    {
        perror("pipe");
        exit(-1);
    }

    pid = fork();
    if(pid<0)
    {
        perror("fork");
        exit(-1);
    }
    else if(pid == 0)
    {
        printf("子进程\n");
/* 3. 测试 write() 函数也阻塞
        int count = 0;
        char str[2048];
        memset(str,'A',sizeof(str));
        while(1)
        {
            count++;
            write(fds[1],str,sizeof(str));
            printf("写入%d次\n",count);
        }   */
        exit(0);
    }
    else
    {
        /* 1. 设置为阻塞,其实 read() 默认就是阻塞 */
        //fcntl(fds[0],F_SETFL,0);
        /* 2. 设置为非阻塞 */
        //fcntl(fds[0],F_SETFL,O_NONBLOCK);
        wait(NULL);
        printf("父进程\n");
        char buf[64];
        memset(buf,0,sizeof(buf));
        printf("读之前\n");
        read(fds[0],buf,sizeof(buf));
        printf("读之后\n");
        printf("读出管道:%s\n",buf);
        close(fds[0]);
        close(fds[1]);
    }
    return 0;
}

不使用 fcntl() 函数或者使用 fcntl(fd, F_SETFL, 0) 时,可见 read() 阻塞了:

[lingyun@manjaro study]$ gcc study.cpp -o study
[lingyun@manjaro study]$ ./study
子进程
父进程
读之前
^C
[lingyun@manjaro study]$

使用 fcntl(fd, F_SETFL, O_NONBLOCK) 时,可见 read() 未阻塞:

[lingyun@manjaro study]$ gcc study.cpp -o study
[lingyun@manjaro study]$ ./study
子进程
父进程
读之前
读之后
读出管道:
[lingyun@manjaro study]$ 

write() 函数的阻塞,可见缓冲区的大小为 32x2048 个字节,当缓冲区满了时,write() 函数也会阻塞:

[lingyun@manjaro study]$ gcc study.cpp -o study
[lingyun@manjaro study]$ ./study
子进程
写入1次
写入2次
写入3次
写入4次
................
写入31次
写入32次
^C
[lingyun@manjaro study]$

还有一个特点,通信过程中,别的进程先结束后当前进程读端口关闭后向管道内写数据时,write() 所在进程会(收到 SIGPIPE 信号)退出,收到 SIGPIPE 默认动作为中断当前进程。

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

int main()
{
    int fds[2] = {0};
    pid_t pid;

    if(pipe(fds) < 0)
    {
        perror("pipe");
        exit(-1);
    }

    pid = fork();
    if(pid<0)
    {
        perror("fork");
        exit(-1);
    }
    else if(pid == 0)
    {
        printf("子进程\n");
        /* 相对于父进程,这个操作为"别的进程先结束" */
        exit(0);
    }
    else
    {
        wait(NULL);

        /* 当前进程读端口关闭 */
        close(fds[0]);

        printf("父进程\n");
        char* str = "你好,世界!";

        /* write()会收到 SIGPIPE 信号,默认动作为中断当前进程 */
        write(fds[1],str,sizeof(str));

        /* 让程序停在这儿 */
        while(1);
        close(fds[1]);
    }
    return 0;
}

执行可见,程序并没有执行到 while(1),而是到 write() 就中断了:

[lingyun@manjaro study]$ gcc study.cpp -o study
[lingyun@manjaro study]$ ./study
子进程
父进程
[lingyun@manjaro study]$ 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值