天天看點

"命名管道" 和 "匿名管道" 通訊

匿名管道的通信

#include <unistd.h>
#include <stdio.h>
#include <string.h>

#define MAX_LINE 80 
#define PIPE_STDIN 0 
#define PIPE_STDOUT 1 

/**
	元素myPipe[1]包含的檔案描述符用于管道的輸入
	元素myPipe[0] 包含的檔案描述符用于管道的輸出
		 ----mypipe[1]             -----mypipe[0]
	write() -|---------------->mypipe----------------->read()
		 ----stdout                -----stdin
*/

int main()
{
	const char *string={"A sample message."};
     	int ret, myPipe[2];
 	char buffer[MAX_LINE+1];

	/* 建立管道 */
 	ret = pipe( myPipe );
 	if (ret == 0) {
		/* 将消息寫入管道 */
		//用write函數把消息寫入管道。站在應用程式的角度,它是在向stdout輸出
		write( myPipe[PIPE_STDOUT], string, strlen(string) );
		/* 從管道讀取消息 */
		ret = read(myPipe[PIPE_STDIN], buffer, MAX_LINE);
		/* 利用NULL結束字元串 */
		//buffer變量的末尾添加一個NULL,這樣就能利用printf函數正确的輸出它了
 		buffer[ ret ] = 0;
 		printf("%s\n", buffer);
 	}
 	return 0;
}
           
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <wait.h>

#define MAX_LINE 80

int main()
{
	int thePipe[2], ret;
	char buf[MAX_LINE+1];
	const char *testbuf={"a test string."};
	if ( pipe( thePipe ) == 0 ) 
	{
		if (fork() == 0)
		{
			ret = read( thePipe[0], buf, MAX_LINE );
			buf[ret] = 0;
			printf( "Child read %s\n", buf );
		} 
		else
		{
			ret = write( thePipe[1], testbuf, strlen(testbuf) );
			ret = wait( NULL );
		}
		
		close( thePipe[0] );
		close( thePipe[1] );
	}

	return 0;
}
           

命名管道的通信

fifo_read.c

/*fifo_read.c*/ 
#include <sys/types.h> 
#include <sys/stat.h> 
#include <errno.h> 
#include <fcntl.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <unistd.h>

/*定義FIFO路徑*/ 
#define FIFO "/tmp/myfifo" 

int main(int argc,char** argv)  
{  
	char buf_r[100];  
	int  fd;  
	int  nread;  
	/*建立FIFO管道*/ 
	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));  
	/*打開FIFO管道,不阻塞方式*/ 
	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);  
   	
   	return 0;
}  
           

fifo_write.c

/*fifo_write.c*/ 
#include <sys/types.h> 
#include <sys/stat.h> 
#include <errno.h> 
#include <fcntl.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h>
#include <unistd.h>

/*FIFO管道路徑*/ 
#define FIFO_SERVER "/tmp/myfifo" 

int main(int argc,char** argv)  
{  
	int fd = 0;  
	char w_buf[100];  
	int nwrite;  
	/*打開FIFO管道*/ 
        fd=open(FIFO_SERVER,O_WRONLY|O_NONBLOCK,0);  
	if(fd==-1);  
	if(errno == ENXIO)  
	       printf("open error; no reading process\n");  
	/*判斷有沒有參數輸入*/ 
	if(argc==1)  
	     printf("Please send something\n");  
	/*複制參數輸入*/ 
	strcpy(w_buf,argv[1]);  
	/*寫到FIFO去*/ 
	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);  
     
     return 0;
}  
           

繼續閱讀