1. 程式人生 > >Linux程序通訊--無名管道(pipe)和有名管道(FIFO)通訊

Linux程序通訊--無名管道(pipe)和有名管道(FIFO)通訊

管道通訊

管道是單向的、先進先出的,它把一個程序的輸出和另一個程序的輸入連線在一起。一個程序(寫程序)在管道的尾部寫入資料,另一個程序(讀程序)從管道的頭部讀出資料

管道建立
管道包括無名管道和有名管道兩種,前者用於父程序和子程序的通訊,後者可用於運行於同一系統中的任意兩個程序間的通訊。
無名管道由pipe()函式建立:
int pipe(int filedis[2]);
當一個管道建立時,它會建立兩個檔案描述符:filedis[0]用於讀管道,filedis[1]用於寫管道。
在這裡插入圖片描述
管道關閉
關閉管道只需將這兩個檔案描述符關閉即可,可以使用普通的close函式逐個關閉

#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{
	int pipe_fd[2];
	if(pipe(pipe_fd)<0)  //建立管道
	{
	printf("pipe create error\n");
	return -1;
	}
	else 
		printf("pipe create success\n");
	close(pipe_fd[0]);   //關閉管道
	close(pipe_fd[1]);
}

管道讀寫
管道用於不同程序間的通訊。通常先建立一個管道,再通過fork函式建立一個子程序,該子程序會繼承父程序所建立的管道。

在這裡插入圖片描述
父程序用於從管道寫入資料,子程序用於從管道讀取資料。
注意:必須在系統呼叫fork()前呼叫pipe(),否則子程序將不會繼承檔案描述符

#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{
	int pipe_fd[2];
	pid_t pid;
	char buf_r[100];
	char* p_wbuf;
	int r_num;
	
	memset(buf_r,0,sizeof(buf_r));
	
	/*建立管道*/
	if(pipe(pipe_fd)<0)
	{
		printf("pipe create error\n");
		return -1;
	}
	
	/*建立子程序*/
	if((pid=fork())==0)  //子程序
	{
		printf("\n");
		close(pipe_fd[1]);
		sleep(2); /*為什麼要睡眠  給父程序寫的時間*/ 
		if((r_num=read(pipe_fd[0],buf_r,100))>0)
		{
			printf(   "%d numbers read from the pipe is %s\n",r_num,buf_r);
		}	
		close(pipe_fd[0]);
		exit(0);
  	}
	else if(pid>0)
	{
		close(pipe_fd[0]);
		if(write(pipe_fd[1],"Hello",5)!=-1)
			printf("parent write1 Hello!\n");
		if(write(pipe_fd[1]," Pipe",5)!=-1)
			printf("parent write2 Pipe!\n");
		close(pipe_fd[1]);
		sleep(3);
		waitpid(pid,NULL,0); /*等待子程序結束*/
		exit(0);
	}
	return 0;
}


在linux上執行的結果:
在這裡插入圖片描述

有名管道(命名管道)
命名管道和無名管道基本相同,但也有不同點:無名管道只能由父子程序使用;但是通過命名管道,不想關的程序也能交換資料

建立
#include<sys/types.h>
#include<sys/stat.h>
int mkfifo(const char* pathname,mode_t mode)
*pathname:FIFO檔名
*mode:檔案屬性
一旦建立了一個FIFO,就可以用open開啟它,一般的檔案訪問函式(close,read,write等)都可用於FIFO

操作
當開啟FIFO時,非阻塞標誌(O_NONBLOCK)
將對以後的讀寫產生如下影響:
1、沒有使用O_NONBLOCK:訪問要求無法滿足時程序將阻塞。如試圖讀取空的FIFO,將導致程序阻塞
2、使用O_NONBLOCK:訪問要求無法滿足時不阻塞,立刻出錯返回,errno是ENXIO

讀檔案

#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define FIFO "/tmp/myfifo"

main(int argc,char** argv)
{
	char buf_r[100];
	int  fd;
	int  nread;
	
	/* 建立管道 */
	if((mkfifo(FIFO,O_CREAT|O_EXCL)<0)&&(errno!=EEXIST))
		printf("cannot create fifoserver\n");
	
	printf("Preparing for reading bytes...\n");
	
	memset(buf_r,0,sizeof(buf_r));
	
	/* 開啟管道 */
	fd=open(FIFO,O_RDONLY|O_NONBLOCK,0);
	if(fd==-1)
	{
		perror("open");
		exit(1);	
	}
	while(1)
	{
		memset(buf_r,0,sizeof(buf_r));
		
		if((nread=read(fd,buf_r,100))==-1)
		{
			if(errno==EAGAIN)
				printf("no data yet\n");
		}
		printf("read %s from FIFO\n",buf_r);
		sleep(1);
	}	
	pause(); /*暫停,等待訊號*/
	unlink(FIFO); //刪除檔案
}


寫檔案

#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define FIFO_SERVER "/tmp/myfifo"

main(int argc,char** argv)
{
	int fd;
	char w_buf[100];
	int nwrite;
		
	/*開啟管道*/
	fd=open(FIFO_SERVER,O_WRONLY|O_NONBLOCK,0);
	
	if(argc==1)
	{
		printf("Please send something\n");
		exit(-1);
	}
	
	strcpy(w_buf,argv[1]);
	
	/* 向管道寫入資料 */
	if((nwrite=write(fd,w_buf,100))==-1)
	{
		if(errno==EAGAIN)
			printf("The FIFO has not been read yet.Please try later\n");
	}
	else 
		printf("write %s to the FIFO\n",w_buf);
}