mkfifo函数创建有名管道

本文介绍了命名管道(FIFO)的概念及其在不同进程间的通信机制。详细解析了命名管道的打开规则,并通过示例代码展示了如何使用命名管道进行进程间的数据交换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

pipe创建的管道只能在具有共同祖先的进程间通信,而mkfifo能在不相关的进程间交换数据。通俗举例来说,一个在一个c文件中通信,一个可在多个c文件中通信。
命名管道打开的规则:
为读打开FIFO:
O_NONBLOCK disable:阻塞直到有相应进程为写而打开FIFO
O_NONBLOCK enable:立刻返回成功
为写打开FIFO:
O_NONBLOCK disable:阻塞直到有相应进程为读而打开FIFO
O_NONBLOCK enable:立刻返回失败,错误码为ENXIO
打开文件描述符默认为阻塞。


管道创建与写管道程序:
#include<stdio.h>
#include<sys/stat.h>
#include<unistd.h>
#include<errno.h>
#include<fcntl.h>
#define SIZE 20

int main( int argc, char **argv )
{
    int fd;

    if( (mkfifo(argv[1], 0666)==-1) && (errno!=EEXIST) )//加后一半可令创建管道后的再次读取不输出错误提示!
    {
        perror( "create fifo error!" );
        exit( -1 );
    }
    if( (fd=open(argv[1], O_WRONLY)) == -1 )
    {
        perror( "open file error!" );
        exit( -1 );
    }
    write( fd, argv[2], SIZE );
    close( fd );
    puts( "write success!" );

    exit( 1 );
}


管道接收程序:
#include<stdio.h>
#include<sys/stat.h>
#include<unistd.h>
#include<errno.h>
#include<fcntl.h>
#include<stdlib.h>
#define SIZE 20

int main( int argc, char **argv )
{
    int fd;
    char buffer[SIZE];

    if( (fd=open(argv[1], O_RDONLY | O_NONBLOCK)) == -1 )
    {
        perror( "open file error!" );
        exit( -1 );
    }
    while( 1 )
    {
        memset( buffer, 0, SIZE );
        if( read(fd, buffer, SIZE) == -1 )
        {
            perror( "please input something!" );
        }
        else
        {
            printf( "receieve %s!", buffer );
        }
        sleep( 1 );
    }
}

在Unix或Linux系统中,使用`mkfifo`函数创建的是名为管道,即`named pipe`或`FIFO (First In First Out)`。它是半双工的,意味着只能在一个方向上传输数据。下面是一个简单的C语言示例,展示了如何创建一个有名管道并在两个进程中进行通信: 首先,我们需要创建管道文件: ```bash mkdir -p /path/to/pipes mkfifo /path/to/pipes/my_pipe ``` 然后,在一个进程(例如`producer`)中: ```c #include <stdio.h> #include <unistd.h> #include <fcntl.h> int main(int argc, char *argv[]) { if (argc != 2) { printf("Usage: producer <message>\n"); return 1; } const char *msg = argv[1]; int fd = open("/path/to/pipes/my_pipe", O_WRONLY); // 写模式打开 if (fd == -1) { perror("Failed to open the named pipe"); return 1; } write(fd, msg, strlen(msg) + 1); // 发送消息到管道 close(fd); printf("Producer sent message '%s'\n", msg); return 0; } ``` 接着,在另一个进程(例如`consumer`)中: ```c #include <stdio.h> #include <unistd.h> #include <fcntl.h> int main(int argc, char *argv[]) { if (argc != 1) { printf("Usage: consumer\n"); return 1; } int fd = open("/path/to/pipes/my_pipe", O_RDONLY); // 读模式打开 if (fd == -1) { perror("Failed to open the named pipe"); return 1; } char buffer[100]; // 预设接收缓冲区大小 read(fd, buffer, sizeof(buffer)); // 从管道接收数据 buffer[strlen(buffer) - 1] = '\0'; // 删除换行符 printf("Consumer received message '%s'\n", buffer); close(fd); return 0; } ``` 当你运行这两个程序时,一个进程(如`producer`)可以向`my_pipe`发送一条消息,另一个进程(如`consumer`)会接收到并显示出来。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值