Linux 进程通信之FIFO的实现
时间:2022-07-27
本文章向大家介绍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 filename1n");
return -1;
}
printf("begin open wn");
int o_ret = open(argv[1], O_WRONLY);
printf("end open wn");
char buf[256];
int num = 0;
while (1) {
memset(buf, '#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 filename1n");
return -1;
}
printf("begin open wn");
int o_ret = open(argv[1], O_WRONLY);
printf("end open wn");
char buf[256];
int num = 0;
while (1) {
memset(buf, ' ', sizeof(buf));
sprintf(buf, "xiaoming--%d", num++);
printf("strlen(buf) = %dn", strlen(buf));
write(o_ret, buf, strlen(buf));
sleep(1);
}
close(o_ret);
return 0;
}
', sizeof(buf));
sprintf(buf, "xiaoming--%d", num++);
printf("strlen(buf) = %dn", 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 filename1n");
return -1;
}
printf("begin open rn");
int o_ret = open(argv[1], O_RDONLY);
printf("end open rn");
char buf[256];
int num = 0;
while (1) {
memset(buf, '#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 filename1n");
return -1;
}
printf("begin open rn");
int o_ret = open(argv[1], O_RDONLY);
printf("end open rn");
char buf[256];
int num = 0;
while (1) {
memset(buf, ' ', sizeof(buf));
read(o_ret, buf, sizeof(buf));
printf("strlen(buf) = %dn", strlen(buf));
printf("read is%sn", buf);
}
close(o_ret);
return 0;
}
', sizeof(buf));
read(o_ret, buf, sizeof(buf));
printf("strlen(buf) = %dn", strlen(buf));
printf("read is%sn", buf);
}
close(o_ret);
return 0;
}
以上就是本文的全部内容,希望对大家的学习有所帮助。
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法