日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > linux >内容正文

linux

Linux学习笔记-命名管道(FIFO)

發(fā)布時(shí)間:2025/3/15 linux 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Linux学习笔记-命名管道(FIFO) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

目錄

?

理論

例子


理論

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

成功返回o,出錯(cuò)返回-1

他包含在頭文件:

#include <sys/types.h>

#include <sys/stat.h>

?

?

這里要注意:

1.只要對(duì)FIFO有適當(dāng)?shù)脑L問(wèn)權(quán)限,FIFO可用在任何兩個(gè)沒(méi)有任何關(guān)系的進(jìn)程之間通信;

2.本質(zhì)是內(nèi)核中的一塊緩存,另在文件系統(tǒng)中以一個(gè)特殊的設(shè)備文件(管道文件)存在;

3.在文件系統(tǒng)中只有一個(gè)索引塊存放文件的路徑,沒(méi)有數(shù)據(jù)塊,所有數(shù)據(jù)存放在內(nèi)核中;

4.命名管道必須在讀和寫(xiě)中同時(shí)打開(kāi),否則單獨(dú)讀或者單獨(dú)寫(xiě)會(huì)發(fā)生阻塞;

5.命令mkfifo創(chuàng)建命名管道(命令內(nèi)部調(diào)用mkfifo函數(shù))

6.對(duì)FIFO的操作與操作普通文件一樣;

?

例子

程序運(yùn)行截圖如下:

?

?

fifo_read.c

#include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <memory.h>int main(int argc, char *argv[]){if(argc < 2){printf("usage:%s fifo\n", argv[0]);exit(1);}printf("open fifo read... \n");int fd = open(argv[1], O_RDONLY);if(fd < 0){perror("open error");exit(1);}else{printf("open file success:%d\n", fd);}//從命名管道中讀取數(shù)據(jù)char buf[512];memset(buf, 0, sizeof(buf));while(read(fd, buf, sizeof(buf)) < 0){perror("read error");}printf("%s\n", buf);close(fd);exit(0); }


fifo_write.c源碼如下:

#include <unistd.h> #include <memory.h> #include <stdlib.h> #include <stdio.h> #include <fcntl.h>int main(int argc, char *argv[]){if(argc < 2){printf("usage:%s fifo\n", argv[0]);exit(1);}printf("open fifo write ... \n");//打開(kāi)命名管道int fd = open(argv[1], O_WRONLY);if(fd < 0){perror("open error");exit(1);}else{printf("open fifo success:%d\n", fd);}char *s = "12345678901234";size_t size = strlen(s);if(write(fd, s, size) != size){perror("write error");}close(fd);exit(0); }

這里要?jiǎng)?chuàng)建一個(gè)pip,命令如下:mkfifo s.pipo

總結(jié)

以上是生活随笔為你收集整理的Linux学习笔记-命名管道(FIFO)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。