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è)被寫⼊的數(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)建的種類,它只在創(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)文章
解決:ping: www.baidu.com:未知的名稱或服務(wù)問(wèn)題
文章描述了在CentOS 7中配置網(wǎng)絡(luò)后出現(xiàn)無(wú)法ping通域名的問(wèn)題,并提供了解決步驟,首先檢查電腦的IP地址,確保與VMware網(wǎng)絡(luò)配置一致,然后編輯VMware的網(wǎng)絡(luò)配置,修改ifcfg-ens33文件(注意該文件是只讀的,需要按esc退出插入模式2024-12-12
linux中的系統(tǒng)掛載(卸載)U盤(文件系統(tǒng))
這篇文章主要介紹了linux中的系統(tǒng)掛載(卸載)U盤(文件系統(tǒng)),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09
linux控制臺(tái)下實(shí)現(xiàn)2048小游戲
2048小游戲已經(jīng)火了很久了,各種程序版本的都有,今天我們就來(lái)給大家分享一個(gè)在Linux控制臺(tái)中實(shí)現(xiàn)2048小游戲的代碼,希望大家能夠喜歡。2015-03-03
Linux/Unix關(guān)于時(shí)間和時(shí)間戳的命令行
這篇文章主要介紹了Linux/Unix關(guān)于時(shí)間和時(shí)間戳的命令行以及輸出的樣式區(qū)別,一起來(lái)學(xué)習(xí)下吧。2017-12-12
Ubuntu如何修改時(shí)區(qū)為UTC/CST時(shí)間
這篇文章主要介紹了Ubuntu如何修改時(shí)區(qū)為UTC/CST時(shí)間問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07

