Linux

关注公众号 jb51net

关闭
首页 > 网站技巧 > 服务器 > Linux > Linux命名管道

Linux命名管道方式

作者:s_little_monster_

这篇文章主要介绍了Linux命名管道方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

一、命名管道

1、与匿名管道的关系

命名管道由mkfifo创建,是一个文件,打开要用open打开

命名管道与匿名管道之间唯一的区别就是它们创建和打开的方式不同,其他基本上相同

命名管道也只能和有“血缘”的进程进行通信

2、工作原理

通过mkfifo创建,会生成一个文件,这就是我们的命名管道文件,它的大小为0

可以看到它的第一列为p,说明它是特殊文件

在我们对普通文件进行打开的时候,我们要进行的结构其实是这样的

其中这个刷盘的过程就是文件缓冲区中的数据刷新到硬盘上的过程,而我们的fifo文件即命名管道文件是没有刷盘的,所以数据只会待在文件缓冲区里,因为在Linux中,多个进程打开同一个文件所指向的文件缓冲区只有一个,所以如果此时再有一个进程以读方式打开fifo文件,它们之间就会以文件缓冲区作为纽带连接,形成了一个结构,这个结构,与我们以前所讲的匿名管道形成的管道结构是一摸一样的

3、系统调用接口

#include <sys/types.h>
#include <sys/stat.h>
int mkfifo(const char *filename, mode_t mode);
// filename:文件路径
// mode:文件权限
// 返回值:如果管道创建成功返回0,如果失败返回-1并设置errno

4、实现两个进程间通信

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

#define FIFO_NAME "myfifo"
#define BUFFER_SIZE 256

int main()
{
    char message[BUFFER_SIZE];

    // 创建命名管道
    if (mkfifo(FIFO_NAME, 0666) == -1)
    {
        if (errno != EEXIST)
        {
            perror("mkfifo");
            return 1;
        }
    }

    // 打开命名管道以进行写入操作
    int fd = open(FIFO_NAME, O_WRONLY);
    if (fd == -1)
    {
        perror("open");
        return 1;
    }

    // 获取用户输入的消息
    while (1)
    {
        printf("Enter a message to send: ");
        fgets(message, BUFFER_SIZE, stdin);
        message[strcspn(message, "\n")] = 0; // 移除换行符

        // 向命名管道写入消息
        if (write(fd, message, strlen(message)) == -1)
        {
            perror("write");
            close(fd);
            return 1;
        }

        printf("Message sent successfully.\n");
    }
    // 关闭命名管道
    close(fd);

    return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#define FIFO_NAME "myfifo"
#define BUFFER_SIZE 256

int main()
{
    int fd;
    char buffer[BUFFER_SIZE];

    // 打开命名管道以进行读取操作
    fd = open(FIFO_NAME, O_RDONLY);
    if (fd == -1)
    {
        perror("open");
        return 1;
    }
    while (1)
    {
        // 从命名管道读取消息
        ssize_t bytes_read = read(fd, buffer, BUFFER_SIZE - 1);
        if (bytes_read == -1)
        {
            perror("read");
            close(fd);
            return 1;
        }
        buffer[bytes_read] = '\0'; // 添加字符串结束符

        // 输出接收到的消息
        if(buffer[0] == 0)  break;
        printf("Received message: %s\n", buffer);
    }
    // 关闭命名管道
    close(fd);

    // 删除命名管道
    if (unlink(FIFO_NAME) == -1)
    {
        perror("unlink");
        return 1;
    }

    return 0;
}

二、可变参数列表

我们后面要借助命名管道来写一个日志文件,我们需要用到可变参数列表的知识,在这里详细解释一下

可变参数列表允许函数接受不定数量和类型的参数,一般在使用时放到最后面,例如我们前面在进程中断中的函数:

int execl(const char *path, const char *arg, ...);

我们现在有“表”的概念,只要是一个表,最后一个元素一定是NULL,来标志着这个表的结束

与其相关的关键元素有以下四个:(头文件stdarg.h

void va_start(va_list ap, last);
//ap:va_list 类型的变量
//last:可变参数列表之前的最后一个固定参数
type va_arg(va_list ap, type);
//ap:va_list 类型的变量
//type:要获取的参数的类型
void va_end(va_list ap);
//ap:之前用 va_start 初始化过的 va_list 变量

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文