有名管道

无名管道只适用于有亲缘关系的进程间的通信,那么无亲缘关系的进程间如何通信呢?有名管道正是为了解决这个问题。有名管道(FIFO)不同于无名管道之处在于它提供了一个路径名与之关联,以 FIFO 的文件形式存在于文件系统中,这样,即使与 FIFO 的创建进程不存在亲缘关系的进程,只要可以访问该路径,就能够彼此通过 FIFO 相互通信,因此,通过 FIFO 不相关的进程也能交换数据

有名管道和无名管道的不同点:

  • FIFO 在文件系统中作为一个特殊的文件而存在,但 FIFO 中的内容却存放在内存中。
  • 当使用 FIFO 的进程退出后,FIFO 文件将继续保存在文件系统中以便以后使用。
  • FIFO 有名字,不相关的进程可以通过打开命名管道进行通信。

有名管道和无名管道的相同点:

  • 假如 FIFO 里没有数据,调用 read() 函数从 FIFO 里读数据时 read() 也会阻塞。这个特点和无名管道是一样的。
  • 通信过程中,读进程退出后,写进程向命名管道内写数据时,写进程也会(收到 SIGPIPE 信号)退出。(以可读可写方式 open 管道时,此特点不存在。)
  • 调用 write() 函数向 FIFO 里写数据,当缓冲区已满时 write() 也会阻塞。

 创建有名管道

#include <sys/types.h>
#include <sys/stat.h>

/* 创建有名管道 */
int mkfifo(const char *pathname, mode_t mode);

/* 参数:
    pathname: 路径名,即创建后有名管道的路径名
    mode: 文件的权限,与打开普通文件的 open() 函数中的 mode 参数相同
*/

/* 返回值:
    0:成功。
    -1:失败,文件已存在,或出错。

有名管道的操作

有名管道在操作上和普通文件一样进行,如:open()、write()、read()、close() 等。但是,和无名管道一样,操作命名管道肯定要考虑默认情况下其阻塞特性。

1.以只读和只写方式 open 管道,并且没有指定非阻塞标志 O_NONBLOCK 。则:

  • open() 以只读方式打开 FIFO 时,要阻塞到另一个进程为写而打开此 FIFO;
  • open() 以只写方式打开 FIFO 时,要阻塞到另一个进程为读而打开此 FIFO。
  • 通信过程中若写进程先退出了,就算命名管道里没有数据,调用 read() 函数从 FIFO 里读数据时不阻塞;若写进程又重新运行,则调用 read() 函数从 FIFO 里读数据时又恢复阻塞。

2.以只读和只写方式 open 管道,并指定非阻塞标志 O_NONBLOCK 。则 open() 函数不会阻塞。

 非阻塞标志(O_NONBLOCK)打开的命名管道有以下特点:

  • 先以只读方式打开,如果没有进程已经为写而打开一个 FIFO, 只读 open() 成功,并且 open() 不阻塞。
  • 先以只写方式打开,如果没有进程已经为读而打开一个 FIFO,只写 open() 将出错返回 -1 。
  • read()、write() 读写命名管道中读数据时不阻塞。

3.以可读可写方式 open 管道,则 open() 函数不会阻塞。

  特点:

  • read() 仍会阻塞,缓冲区满时,write() 也会阻塞;
  • 通信过程中,读进程退出后,写进程向命名管道内写数据时,写进程不会退出
  • 通信过程中若写进程先退出了,如果名管道里没有数据,调用 read() 函数从 FIFO 里读数据时会阻塞,与第一种情况不同。

代码

/* study.cpp 写端代码 */
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>

int main()
{
    int ret;
    ret = mkfifo("./FIFO", 0666);
    if(ret != 0)
    {
        perror("mkfifo");
    }

    int fd_w;
    printf("open之前\n");

    /* 1. 以只写方式打开有名管道 */
    //fd_w = open("./FIFO",O_WRONLY);

    /* 2. 以只写方式打开有名管道,非阻塞 */
    fd_w = open("./FIFO",O_WRONLY|O_NONBLOCK);

    /* 3. 以可读可写方式打开有名管道 */
    //fd_w = open("./FIFO",O_RDWR);

    printf("open之后\n");
    if(fd_w < 0)
    {
        perror("open");
        _exit(-1);
    }

    char str[64];
    int i = 0;
    while (i<5)
    {
        i++;
        memset(str,0,sizeof(str));
        sprintf(str,"%d: hello world!",i);
        printf("写:%s\n",str);
        write(fd_w,str,strlen(str)+1);
        sleep(2);
    }
    close(fd_w);
    return 0;
}
/* main.c 读端代码 */
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

int main()
{
    int ret;
    ret = mkfifo("./FIFO", 0666);
    if(ret != 0)
    {
        perror("mkfifo");
    }

    int fd_r;
    printf("open之前\n");

	/* 1. 以只写方式打开有名管道 */
    //fd_r = open("./FIFO",O_RDONLY);

	/* 2. 以只写方式打开有名管道,非阻塞 */
    fd_r = open("./FIFO",O_RDONLY|O_NONBLOCK);

    /* 3. 以可读可写方式打开有名管道 */
    //fd_r = open("./FIFO",O_RDWR);

    printf("open之后\n");
    if(fd_r < 0)
    {
        perror("open");
        _exit(-1);
    }

    char str[64];
	int i = 0;
    while (i<10)
    {
		i++;
		memset(str,0,sizeof(str));
        read(fd_r,str,sizeof(str));
		printf("读:%s\n",str);
		sleep(1);
    }
	close(fd_r);
    return 0;
}

执行一下几种情况:

1. 以只读和只写方式 open 管道,并且没有指定非阻塞标志 O_NONBLOCK:

先在一个命令窗口执行 ./study ,可见阻塞在 open 函数:

 ⚙ lingyun@manjaro  ~/Document/CppCode/study  gcc study.cpp -o study
 ⚙ lingyun@manjaro  ~/Document/CppCode/study  gcc main.c -o main
 ⚙ lingyun@manjaro  ~/Document/CppCode/study  ./study               
mkfifo: File exists
open之前                    // open() 阻塞

再在另一个命令窗口执行 ./main ,可见能够继续执行下去:

 ⚙ lingyun@manjaro  ~/Document/CppCode/study  ./main
mkfifo: File exists
open之前
open之后
读:1: hello world!
读:2: hello world!
读:                 //此处写进程已经退出,读进程的 read() 函数就不阻塞了
读:
读:
读:1: hello world!  //此处写进程再次执行,读进程恢复阻塞特性(sleep(1)但得等 2s,说明 read() 阻塞)
读:2: hello world!
读:3: hello world!
读:4: hello world!
读:5: hello world!
 ⚙ lingyun@manjaro  ~/Document/CppCode/study  

第一个命令窗口得以继续执行:

 ⚙ lingyun@manjaro  ~/Document/CppCode/study  ./study
mkfifo: File exists
open之前
open之后                //读进程已经 open 了此管道,open() 不再阻塞
写:1: hello world!
写:2: hello world!
^C                      //此处退出写进程
 ✘ ⚙ lingyun@manjaro  ~/Document/CppCode/study  ./study         //再次执行写进程
mkfifo: File exists
open之前
open之后
写:1: hello world!
写:2: hello world!
写:3: hello world!
写:4: hello world!
写:5: hello world!
 ⚙ lingyun@manjaro  ~/Document/CppCode/study  

2.以只读和只写方式 open 管道,并指定非阻塞标志 O_NONBLOCK:

先执行只读程序:

 ⚙ lingyun@manjaro  ~/Document/CppCode/study  ./main
mkfifo: File exists
open之前
open之后                    // 只读 open() 成功,并且 open() 不阻塞。
读:                        // read() 读有名管道中读数据时不阻塞。
读:
读:
读:
读:
读:
读:
读:
读:
读:
 ⚙ lingyun@manjaro  ~/Document/CppCode/study  

先执行只写程序:

 ⚙ lingyun@manjaro  ~/Document/CppCode/study  ./study               
mkfifo: File exists
open之前
open之后
open: No such device or address        // 只写 open() 将出错返回 -1。
 ✘ ⚙ lingyun@manjaro  ~/Document/CppCode/study  

3.以可读可写方式 open 管道:

 a. 测试在通信过程中退出读进程:

 ⚙ lingyun@manjaro  ~/Document/CppCode/study  ./main        //读进程
mkfifo: File exists
open之前
open之后
读:1: hello world!
读:2: hello world!
^Z                        // 退出读进程
[10]  + 9536 suspended  ./main
 ✘ ⚙ lingyun@manjaro  ~/Document/CppCode/study  
 ⚙ lingyun@manjaro  ~/Document/CppCode/study  ./study
mkfifo: File exists
open之前
open之后
写:1: hello world!
写:2: hello world!
写:3: hello world!        //虽然读进程退出,但写进程在向管道中写入数据时,并没有退出
写:4: hello world!
写:5: hello world!
 ⚙ lingyun@manjaro  ~/Document/CppCode/study  

 b. 测试在通信过程中先退出写进程:

 ⚙ lingyun@manjaro  ~/Document/CppCode/study  ./study
mkfifo: File exists
open之前
open之后
写:1: hello world!
写:2: hello world!
写:3: hello world!
写:4: hello world!
写:5: hello world!          //这里执行完后,写进程即退出
 ⚙ lingyun@manjaro  ~/Document/CppCode/study  
⚙ lingyun@manjaro  ~/Document/CppCode/study  ./main
open之前
open之后
读:1: hello world!
读:2: hello world!
读:3: hello world!
读:4: hello world!
读:5: hello world!
                           //程序阻塞在这里,说明 read() 阻塞了

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值