Linux 进程通信之FIFO的实现

 更新时间:2020年02月11日 14:18:01   作者:weixin_43903378  
这篇文章主要介绍了Linux 进程通信之FIFO的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

FIFO通信(first in first out)

FIFO 有名管道,实现无血缘关系进程通信。

  • 创建一个管道的伪文件
    • a.mkfifo testfifo 命令创建
    • b.也可以使用函数int mkfifo(const char *pathname, mode_t mode);
  • 内核会针对fifo文件开辟一个缓冲区,操作fifo文件,可以操作缓冲区,实现进程间通信–实际上就是文件读写

man 3 mkfifo

#include <sys/types.h>
#include <sys/stat.h>
int mkfifo(const char *pathname, mode_t mode);

注意事项:

FIFOs
Opening the read or write end of a FIFO blocks until the other end is also opened (by another process or thread). See
fifo(7) for further details.

打开fifo文件时候,read端会阻塞等待write端open,write端同理,也会阻塞等待另外一段打开。

代码示例:
file_w.c 写端

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

int main(int argc, char *argv[]) {
  if(argc != 2) {
    printf("./a.out filename1\n");
    return -1;
  }
  printf("begin open w\n");
  int o_ret = open(argv[1], O_WRONLY);
  printf("end open w\n");
  char buf[256];
  int num = 0;
  while (1) {
    memset(buf, '\0', sizeof(buf));
    sprintf(buf, "xiaoming--%d", num++);
    printf("strlen(buf) = %d\n", strlen(buf));
    write(o_ret, buf, strlen(buf));
    sleep(1);
  }
  close(o_ret);
  return 0;
}
 

file_r.c 读端

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

int main(int argc, char *argv[]) {
  if(argc != 2) {
    printf("./a.out filename1\n");
    return -1;
  }
  printf("begin open r\n");
  int o_ret = open(argv[1], O_RDONLY);
  printf("end open r\n");
  char buf[256];
  int num = 0;
  while (1) {
    memset(buf, '\0', sizeof(buf));
    read(o_ret, buf, sizeof(buf));
    printf("strlen(buf) = %d\n", strlen(buf));
    printf("read is%s\n", buf);
  }
  close(o_ret);
  return 0;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • 改造ctrl+alt+del(默认重启)为一个信息搜集脚本的脚本

    改造ctrl+alt+del(默认重启)为一个信息搜集脚本的脚本

    远程一个服务器总是出现网络故障,因为不方便让IDC工程师做太详细的操作,每次都是让他按ctrl+alt+del重启服务器,最后写了这么一个脚本,实现的效果是
    2011-05-05
  • 详解linux根目录空间不足解决方案

    详解linux根目录空间不足解决方案

    本篇文章主要介绍了详解linux根目录空间不足解决方案,具有一定的参考价值,有兴趣的可以了解一下。
    2017-04-04
  • Linux文件权限与目录管理详解

    Linux文件权限与目录管理详解

    这篇文章主要介绍了Linux文件权限与目录管理,感兴趣的小伙伴们可以参考一下
    2016-03-03
  • linux mount报错:you must specify the filesystem type的解决方法

    linux mount报错:you must specify the filesystem type的解决方法

    这篇文章主要介绍了linux mount报错:you must specify the filesystem type的解决方法,文中给出了详细的解决方法示例,对大家具有一定的参考价值,需要的朋友们下面来一起看看吧。
    2017-03-03
  • Centos7修改主机名hostname的三种方法

    Centos7修改主机名hostname的三种方法

    今天小编就为大家分享一篇关于Centos7修改主机名hostname的三种方法,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-01-01
  • Linux环境下安装JDK1.8

    Linux环境下安装JDK1.8

    本文详细讲解了Linux环境下安装JDK1.8的方法,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-11-11
  • Linux tar命令使用列子

    Linux tar命令使用列子

    对许多用户来说,在DOS和Windows环境下利用工具软件WinZip、ARJ等压缩或解压文件是比较容易的事。但是,在Linux中如何对文件进行压缩与解压呢?
    2008-06-06
  • centos8 安装 nginx的详细教程(图文)

    centos8 安装 nginx的详细教程(图文)

    Nginx是一个web服务器也可以用来做负载均衡及反向代理使用,目前使用最多的就是负载均衡,这篇文章主要介绍了centos8 安装 nginx ,需要的朋友可以参考下
    2019-11-11
  • Centos 6.8编译安装LNMP环境(Nginx+MySQL+PHP)教程

    Centos 6.8编译安装LNMP环境(Nginx+MySQL+PHP)教程

    这篇文章主要介绍了关于CentOS 6.8中编译安装LNMP环境的相关资料,LNMP即Linux,Nginx,MySQL,PHP,文中通过一步步的步骤介绍的非常详细,需要的朋友可以参考借鉴,下面来一起看看吧。
    2017-03-03
  • 有效学习Linux系统的4个方法

    有效学习Linux系统的4个方法

    这篇文章主要为大家详细介绍了有效学习Linux系统的4个方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-01-01

最新评论