首页 > 系统相关 >进程间通信之fifo

进程间通信之fifo

时间:2023-02-03 11:13:57浏览次数:52  
标签:char int fifo 间通信 fd 进程 include buf

一、引言

  FIFO常被称为有名管道,不同于管道(pipe)。pipe仅适用于“有血缘关系”的IPC。但FIFO还可以应用于不相关的进程的IPC。实际上,FIFO是Linux基础文件类型中的一种,是在读写内核通道。

函数原型:

 

int mkfifo(const char *pathname, mode_t mode); 

成功返回 0, 失败返回 -1

 

命令:

mkfifo + 管道名   例:mkfifo fifo_one

操作步骤:

1) 通过命令行创建fifo;

2) 使用open、close、read、write等I/O函数操作fifo。

二、例程

1) 创建fifi:

#mkfifio fifo_one

2) fifo 写函数

复制代码
 1 #include <stdio.h>
 2 #include <unistd.h>
 3 #include <sys/stat.h>
 4 #include <sys/types.h>
 5 #include <fcntl.h>
 6 #include <stdlib.h>
 7 #include <string.h>
 8 
 9 void sys_err(char *str)
10 {
11     perror(str);
12     exit(-1);
13 }
14 int main(int argc, char *argv[])
15 {
16     int fd, i;
17     char buf[4096];
18 
19     if (argc < 2) {
20         printf("Please enter: ./a.out fifoname\n");
21         return -1;
22     }
23     fd = open(argv[1], O_WRONLY);//打开fifo
24     if (fd < 0) 
25         sys_err("open");
26 
27     i = 0;
28     while (1) {
       printf("write fifo \n ");
29  sprintf(buf, "hello itcast %d\n", i++); //格式化输出到buf 31 write(fd, buf, strlen(buf)); //向fifo写入buf 32 sleep(1); 33 } 34 close(fd); 35 36 return 0; 37 }
复制代码

3) fifo 读函数

复制代码
 1 #include <stdio.h>
 2 #include <unistd.h>
 3 #include <sys/stat.h>
 4 #include <sys/types.h>
 5 #include <fcntl.h>
 6 #include <stdlib.h>
 7 #include <string.h>
 8 
 9 void sys_err(char *str)
10 {
11     perror(str);
12     exit(1);
13 }
14 
15 int main(int argc, char *argv[])
16 {
17     int fd, len;
18     char buf[4096];
19 
20     if (argc < 2) {
21         printf("Please enter: ./a.out fifoname\n");
22         return -1;
23     }
24     fd = open(argv[1], O_RDONLY);//打开fifo
25     if (fd < 0) 
26         sys_err("open");
27     while (1) {
28         len = read(fd, buf, sizeof(buf));//从fifo读取数据到buf
29         write(STDOUT_FILENO, buf, len); //将buf写入标准输出
30         sleep(3);           //多個读端时应增加睡眠秒数,放大效果.
31     }
32     close(fd);
33     return 0;
34 }
复制代码

编译执行:

     

标签:char,int,fifo,间通信,fd,进程,include,buf
From: https://www.cnblogs.com/kn-zheng/p/17088481.html

相关文章

  • 进程状态与进程关系
    一、进程的六种状态Linux系统下进程通常存在6种不同的状态,分为:就绪态、运行态、僵尸态、可中断睡眠状态(浅度睡眠)、不可中断睡眠状态(深度睡眠)以及暂停态。就......
  • Oracle系统常见进程有哪些,作用是什么?
    转至:https://vip.kingdee.com/article/336895665106415104?isKnowledge=2&productLineId=8Oracle系统常见进程如下: 五个必须的ORACLE后台进程SMON、PMON、DBWn、CKPT......
  • 写了个监控 ElasticSearch 进程异常的脚本!
    作者:JackTian来源:公众号「杰哥的IT之旅」ID:Jake_Internet服务器配置免密钥环境准备:配置免密钥前,需要在服务器的hosts文件中配置目标主机名称与IP对应关系。vim/......
  • Linux进程和计划任务
    一、进程及其命令介绍1.1什么是进程要想了解进程,首先我们要了解什么是程序。程序是一组计算机能识别和执行的指令,运行于电子计算机上,满足人们某种需求的信息化工具;而进程......
  • pgrep:根据名称或其他属性查找进程(查找系统中running状态的进程)
    用法示例#根据名称过滤dewanWork~/code/shell%pgrepqemu3570191dewanWork~/code/shell%pgrepqemu-l3570191qemu-system-x86dewanWork~/code/shell%p......
  • linux查看进程号、端口号、服务名互查信息
    1、已知程序名称查看进程号ps-ef|grep程序名称##或者ps-aux|grep程序名称pgrep-l程序名称......
  • 进程与子进程
    一、子进程1.fork()创建子进程一个现有的进程可以调用fork()函数创建一个新的进程,调用fork()函数的进程称为父进程,由fork()函数创建出来的进程被称为子进程(child......
  • Linux系统日常进程管理与监控
    1.进程是什么进程是正在执行的一个程序或命令,每一个进程都是一个运行的实体,都有自己的地址空间,并占用一定的系统资源。在LINUX系统中进程ID用PID表示,范围从0-32768,其中PI......
  • Python服务进程管理工具supervisor使用记录
    [本文出自天外归云的博客园]学习资料supervisor文档:http://supervisord.org/running.html踩坑总结问题1:提示找不到一些包含/tmp的路径需要修改supervisord.conf配置文......
  • 项目运行报错:目标进程已退出,但未引发 CoreCLR 启动事件。
      项目运行报错:目标进程已退出,但未引发CoreCLR启动事件。请确保将目标进程配置为使用.NETCore。如果目标进程未运行.NETCore,则发生这种情况并不意外。 解决方......