圆月山庄资源网 Design By www.vgjia.com
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; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com
暂无评论...
更新日志
2024年06月26日
2024年06月26日
- 瑞鸣音乐《中国音乐地图之听见河南豫剧经典唱段》2020[WAV分轨]
- 【伊比萨沙发】VA-2024-IbizaSpring2024(LoungeCocktailsfromtheHeart)[FLAC]
- 群星《世界顶级汽车音响试音王》4K金碟16CD[WAV+CUE]
- 群星-音效环绕天碟《极致环绕人声天碟天品》7CD[WAV]
- 泰拉克SACD古典试音碟7SACD-ISO
- 群星《红歌宝典》20CD[WAV/分轨]
- 《101系列:好歌101首 细听年代精选》[WAV+CUE][3.6GB]
- 《101系列 6CD 国语经典101首歌曲 张学友 王菲代表作》[APE+CUE][2.8GB]
- 《101系列:国语经典101 VOL2 6CD 精选最经典101首》[WAV+CUE][2.3GB]
- 群星《十年网络红歌》15CD[WAV分轨]
- 宝丽金-环球10周年至50周年经典金曲15CD[低速原抓WAV+CUE]
- FIM唱片《十大男伶》[低速原抓WAV+CUE]
- 《101系列:情歌101 6CD》[WAV+CUE][3.1GB]
- 庄心妍《降调版合集》[320K/MP3][52.96MB]
- 庄心妍《降调版合集》[FLAC/分轨][118.78MB]