linux 命名管道實(shí)例詳解
linux進(jìn)程間通信——命名管道
FIFO(命名管道)不同于匿名管道之處在于它提供⼀個(gè)路徑名與之關(guān)聯(lián),以FIFO的⽂件形式存儲(chǔ)于⽂件系統(tǒng)中。命名管道是⼀個(gè)設(shè)備⽂件,因此,即使進(jìn)程與創(chuàng)建FIFO的進(jìn)程不存在親緣關(guān)系,只要可以訪問(wèn)該路徑,就能夠通過(guò)FIFO相互通信。值得注意的是,F(xiàn)IFO(first input first output)總是按照先進(jìn)先出的原則⼯作,第⼀個(gè)被寫(xiě)⼊的數(shù)據(jù)將⾸先從管道中讀出。
創(chuàng)建命名管道的系統(tǒng)函數(shù)有兩個(gè):mknod和mkfifo。兩個(gè)函數(shù)均定義在頭⽂件sys/stat.h,函數(shù)原型如下:
#include <sys/types.h> #include <sys/stat.h> int mknod(const char *path,mode_t mod,dev_t dev); int mkfifo(const char *path,mode_t mode);
函數(shù)mknod參數(shù)中path為創(chuàng)建的命名管道的全路徑名:mod為創(chuàng)建的命名管道的模式,指明其存取權(quán)限;dev為設(shè)備值,該值取決于⽂件創(chuàng)建的種類(lèi),它只在創(chuàng)建設(shè)備⽂件時(shí)才會(huì)⽤到。這兩個(gè)函數(shù)調(diào)⽤成功都返回0,失敗都返回-1。下⾯使⽤mknod函數(shù)創(chuàng)建了⼀個(gè)命名管道:
umask(0); if (mknod("/tmp/fifo",S_IFIFO | 0666) == -1) { perror("mkfifo error"); exit(1); }
函數(shù)mkfifo前兩個(gè)參數(shù)的含義和mknod相同。下⾯是使⽤mkfifo的⽰例代碼:
umask(0); if (mkfifo("/tmp/fifo",S_IFIFO|0666) == -1) { perror("mkfifo error!"); exit(1); }
下面為一個(gè)試?yán)?/span>
read端
#include<stdlib.h> #include<stdio.h> #include<sys/types.h> #include<sys/stat.h> #include<fcntl.h> #include<errno.h> #define PATH "./fifo" #define SIZE 128 int main() { umask(0); if (mkfifo (PATH,0666|S_IFIFO) == -1) { perror ("mkefifo error"); exit(0); } int fd = open (PATH,O_RDONLY); if (fd<0) { printf("open fd is error\n"); return 0; } char Buf[SIZE]; while(1){ ssize_t s = read(fd,Buf,sizeof(Buf)); if (s<0) { perror("read error"); exit(1); } else if (s == 0) { printf("client quit! i shoud quit!\n"); break; } else { Buf[s] = '\0'; printf("client# %s ",Buf); fflush(stdout); } } close (fd); return 3; }
下面為weite端:
#include<stdlib.h> #include<stdio.h> #include<unistd.h> #include<sys/types.h> #include<sys/stat.h> #include<string.h> #include<errno.h> #include<fcntl.h> #define PATH "./fifo" #define SIZE 128 int main() { int fd = open(PATH,O_WRONLY); if (fd < 0) { perror("open error"); exit(0); } char Buf[SIZE]; while(1) { printf("please Enter#:"); fflush(stdout); ssize_t s = read(0,Buf,sizeof(Buf)); if (s<0) { perror("read is failed"); exit(1); } else if(s==0) { printf("read is closed!"); return 1; } else{ Buf[s]= '\0'; write(fd,Buf,strlen(Buf)); } } return 0; }
打開(kāi)兩個(gè)終端:
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
Linux下用netstat查看網(wǎng)絡(luò)狀態(tài)、端口狀態(tài)
這篇文章主要介紹了Linux下用netstat查看網(wǎng)絡(luò)狀態(tài)、端口狀態(tài)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-12-12如何重置 RHEL7/CentOS7 系統(tǒng)的root密碼
這篇文章主要介紹了如何重置 RHEL7/CentOS7 系統(tǒng)的root密碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-02-02PHP程序員玩轉(zhuǎn)Linux系列 備份還原MySQL
這篇文章主要為大家詳細(xì)介紹了PHP程序員玩轉(zhuǎn)Linux系列文章,MySQL備份還原教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04Ubuntu16.04安裝clion全過(guò)程及步驟詳解
這篇文章主要介紹了Ubuntu16.04安裝clion全過(guò)程及步驟詳解,clion是一款JetBrains 推出的全新的 C/C++ 跨平臺(tái)集成開(kāi)發(fā)環(huán)境,在Ubuntu系統(tǒng)下使用方便,下面給大家分享操作步驟,需要的朋友可以參考下2020-08-08Linux使用Crontab定時(shí)訪問(wèn)某個(gè)路由地址的方法
我們很多工作都是需要在某個(gè)特定的時(shí)間去執(zhí)行它,下面這篇文章主要給大家介紹了關(guān)于Linux使用Crontab定時(shí)訪問(wèn)某個(gè)路由地址的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2018-06-06