fifo通信(first in first out)
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; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。
如对本文有疑问, 点击进行留言回复!!
荐 23-TensorFlow2.2+Keras版本新特性---自动图
荐 Flutter:最难搞的搭建环境,一次性成功!(新手踩坑)
ZABBIX自定义监控指定的LINUX磁盘分区,并配置触发器产生告警
Android7以上读写/data/data下其他APP私有文件的注意点
荐 MnasNet:经典轻量级神经网络搜索方法 | CVPR 2019
网友评论