管道通信

265 篇文章 0 订阅
http://blog.csdn.net/wc7620awjh/article/details/7709581

什么是管道?

      管道是单向的、先进先出的,它把一个进程的输出和另一个进程的输入连接在一起。一个进程(写进程)在管道的尾部写入数据,另一个进程(读进程)从管道的头部读出数据

管道的分类

  管道包括无名管道名管道两种,前者用于父进程和子进程间的通信,后者可用于运行于同一系统中的任意两个进程间的通信。

无名管道的创建

  无名管道由pipe( )函数创建:

   int pipe(int filedis[2]);

   当一个管道被创建时,它会创建两个文件描述符:filedis[0]用于读管道,filedis[1]用于写管道。

        管道通信示意图如图1所示:

                                                               

                                                                                               图1   管道通信示意图

管道关闭

    关闭管道只是将两个文件描述符关闭即可,可以使用普通的close函数逐个关闭。

无名管道读写

    管道用于不同进程间通信。通常先创建一个管道,再通过fork函数创建一个子进程,该子进程会继承父进程创建的管道。注意事项:必须在系统调用fork()前调用pipe(),否则子进程将不会继承文件描述符。否则,会创建两个管道,因为父子进程共享同一段代码段,都会各自调用pipe(),即建立两个管道,出现异常错误。无名管道读写过程如图2所示:

                                                                            

                                                                                                 图2 无名管道读写示意图

无名管道实例:

  1. /**********************************************************
  2. *实验要求:   使用pipe创建无名管道并实现父子进程之间的通讯。
  3. *功能描述:   在父进程中通过无名管道的写端写入数据,通过子进程从管道读端读出相*           应的数据。
  4. *日    期:   2010-9-17
  5. *作    者:   国嵌
  6. **********************************************************/ 
  7. #include <unistd.h> 
  8. #include <sys/types.h> 
  9. #include <errno.h> 
  10. #include <stdio.h> 
  11. #include <string.h> 
  12. #include <stdlib.h> 
  13.  
  14. /*
  15. * 程序入口
  16. * */ 
  17. int main() 
  18.     int pipe_fd[2]; 
  19.     pid_t pid; 
  20.     char buf_r[100]; 
  21.     char* p_wbuf; 
  22.     int r_num; 
  23.      
  24.     memset(buf_r,0,sizeof(buf_r)); 
  25.      
  26.     /*创建管道*/ 
  27.     if(pipe(pipe_fd)<0) 
  28.     { 
  29.         printf("pipe create error\n"); 
  30.         return -1; 
  31.     } 
  32.      
  33.     /*创建子进程*/ 
  34.     if((pid=fork())==0) //子进程执行序列 
  35.     { 
  36.         printf("\n"); 
  37.         close(pipe_fd[1]);//子进程先关闭了管道的写端 
  38.         sleep(2); /*让父进程先运行,这样父进程先写子进程才有内容读*/ 
  39.         if((r_num=read(pipe_fd[0],buf_r,100))>0) 
  40.         { 
  41.             printf("%d numbers read from the pipe is %s\n",r_num,buf_r); 
  42.         }    
  43.         close(pipe_fd[0]); 
  44.         exit(0); 
  45.     } 
  46.     else if(pid>0)//父进程执行序列 
  47.     { 
  48.         close(pipe_fd[0]); //父进程先关闭了管道的读端 
  49.         if(write(pipe_fd[1],"Hello",5)!=-1) 
  50.             printf("parent write1 Hello!\n"); 
  51.         if(write(pipe_fd[1]," Pipe",5)!=-1) 
  52.             printf("parent write2 Pipe!\n"); 
  53.         close(pipe_fd[1]); 
  54.         waitpid(pid,NULL,0); /*等待子进程结束*/ 
  55.         exit(0); 
  56.     } 
  57.     return 0; 
/**********************************************************
*实验要求:   使用pipe创建无名管道并实现父子进程之间的通讯。
*功能描述:   在父进程中通过无名管道的写端写入数据,通过子进程从管道读端读出相*           应的数据。
*日    期:   2010-9-17
*作    者:   国嵌
**********************************************************/
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#include <string.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]);
		waitpid(pid,NULL,0); /*等待子进程结束*/
		exit(0);
	}
	return 0;
}


命名管道

命名管道和无名管道基本相同,但也有不同点:无名管道只能有父进程使用;但是通过命名管道,不相关的进程也能交换数据。

命名管道的创建

#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时,或者FIFO已满时。

2 使用O_NONBLOCK:访问要求无法满足时不阻塞,立即出错返回,error是ENXIO。

FIFO读进程:

  1. /**********************************************************
  2. *实验要求:   使用mkfifo创建有名管道并实现两个进程之间的通讯。
  3. *功能描述:   创建一个进程,并从已经建立好的有名管道中,读出事先写入的
  4. *           数据。
  5. *日    期:   2010-9-17
  6. *作    者:   国嵌
  7. **********************************************************/ 
  8. #include <sys/types.h> 
  9. #include <sys/stat.h> 
  10. #include <errno.h> 
  11. #include <fcntl.h> 
  12. #include <stdio.h> 
  13. #include <stdlib.h> 
  14. #include <string.h> 
  15. #define FIFO "/tmp/myfifo" 
  16.  
  17. /*
  18. * 程序入口
  19. * */ 
  20. int main(int argc,char** argv) 
  21.     char buf_r[100]; 
  22.     int  fd; 
  23.     int  nread; 
  24.  
  25.     printf("Preparing for reading bytes...\n"); 
  26.     memset(buf_r,0,sizeof(buf_r)); 
  27.      
  28.     /* 打开管道 */ 
  29.     fd=open(FIFO,O_RDONLY|O_NONBLOCK,0); 
  30.     if(fd==-1) 
  31.     { 
  32.         perror("open"); 
  33.         exit(1);     
  34.     } 
  35.     while(1) 
  36.     { 
  37.         memset(buf_r,0,sizeof(buf_r)); 
  38.          
  39.         if((nread=read(fd,buf_r,100))==-1) 
  40.         { 
  41.             if(errno==EAGAIN) 
  42.                 printf("no data yet\n"); 
  43.         } 
  44.         printf("read %s from FIFO\n",buf_r); 
  45.         sleep(1); 
  46.     } 
  47.     /*后面三句话是不会被运行到的,但不会影响程序运行的效果当程序在上面的死循环中执行时收到信号后会马上结束运行而没有执行后面的三句话。这些会在后面的信号处理中讲到,现在不理解没有关系,这个问题留给大家学习了信号处理之后来解决。*/ 
  48.     close(fd); //关闭管道 
  49.     pause(); /*暂停,等待信号*/ 
  50.     unlink(FIFO); //删除文件 
/**********************************************************
*实验要求:   使用mkfifo创建有名管道并实现两个进程之间的通讯。
*功能描述:   创建一个进程,并从已经建立好的有名管道中,读出事先写入的
*           数据。
*日    期:   2010-9-17
*作    者:   国嵌
**********************************************************/
#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"

/*
 * 程序入口
 * */
int main(int argc,char** argv)
{
	char buf_r[100];
	int  fd;
	int  nread;

	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);
	}
   	/*后面三句话是不会被运行到的,但不会影响程序运行的效果当程序在上面的死循环中执行时收到信号后会马上结束运行而没有执行后面的三句话。这些会在后面的信号处理中讲到,现在不理解没有关系,这个问题留给大家学习了信号处理之后来解决。*/
	close(fd); //关闭管道
	pause(); /*暂停,等待信号*/
	unlink(FIFO); //删除文件
}


FIFO写进程:

  1. /**********************************************************
  2. *实验要求:   使用mkfifo创建有名管道并实现两个进程之间的通讯。
  3. *功能描述:   创建一个进程,并在其中创建一个有名管道,并向其写入数据。
  4. *日    期:   2010-9-17
  5. *作    者:   国嵌
  6. **********************************************************/ 
  7. #include <sys/types.h> 
  8. #include <sys/stat.h> 
  9. #include <errno.h> 
  10. #include <fcntl.h> 
  11. #include <stdio.h> 
  12. #include <stdlib.h> 
  13. #include <string.h> 
  14. #define FIFO_SERVER "/tmp/myfifo" 
  15.  
  16. /*
  17. * 程序入口
  18. * */ 
  19. int main(int argc,char** argv) 
  20.     int fd; 
  21.     char w_buf[100]; 
  22.     int nwrite; 
  23.      
  24.     /*创建有名管道*/ 
  25.     if((mkfifo(FIFO_SERVER,O_CREAT|O_EXCL|O_RDWR)<0)&&(errno!=EEXIST)) 
  26.     { 
  27.         printf("cannot create fifoserver\n"); 
  28.     } 
  29.  
  30.     /*打开管道*/ 
  31.     fd=open(FIFO_SERVER,O_WRONLY |O_NONBLOCK,0); 
  32.     if(fd==-1) 
  33.     { 
  34.         perror("open"); 
  35.         exit(1); 
  36.     } 
  37.      
  38.     /*入参检测*/ 
  39.     if(argc==1) 
  40.     { 
  41.         printf("Please send something\n"); 
  42.         exit(-1); 
  43.     } 
  44.     strcpy(w_buf,argv[1]); 
  45.      
  46.     /* 向管道写入数据 */ 
  47.     if((nwrite=write(fd,w_buf,100))==-1) 
  48.     { 
  49.         if(errno==EAGAIN) 
  50.             printf("The FIFO has not been read yet.Please try later\n"); 
  51.     } 
  52.     else  
  53.     { 
  54.         printf("write %s to the FIFO\n",w_buf); 
  55.     } 
  56.     close(fd); //关闭管道 
  57.     return 0; 
/**********************************************************
*实验要求:   使用mkfifo创建有名管道并实现两个进程之间的通讯。
*功能描述:   创建一个进程,并在其中创建一个有名管道,并向其写入数据。
*日    期:   2010-9-17
*作    者:   国嵌
**********************************************************/
#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"

/*
 * 程序入口
 * */
int main(int argc,char** argv)
{
	int fd;
	char w_buf[100];
	int nwrite;
	
    /*创建有名管道*/
	if((mkfifo(FIFO_SERVER,O_CREAT|O_EXCL|O_RDWR)<0)&&(errno!=EEXIST))
	{
		printf("cannot create fifoserver\n");
	}

	/*打开管道*/
	fd=open(FIFO_SERVER,O_WRONLY |O_NONBLOCK,0);
	if(fd==-1)
	{
	    perror("open");
		exit(1);
	}
	
	/*入参检测*/
	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);
	}
	close(fd); //关闭管道
	return 0;
}


运行读进程结果如下:

打开另外一个终端,运行写进程结果如下:

同时读进程结果发生变化如下:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值