进程管道通信

一、管道通信

1、pipe 无名管道

int pipe_1[2];
pipe(pipe_1)


定义整形数组pipe_1作为管道

由pipe函数定义后,pipe_1[0]为读端,pipe_1[1]为写端

实例:父进程等待,子进程写数据;写完后父进程读取写进的数据

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



int main(void)
{
	char str[10];
	pid_t pid;
	int pipe_1[2];

	if(pipe(pipe_1)<0)
	{
		printf("error\n");
		return -1;
	}
	
	if(pid=fork()>0)//父进程
	{
		close(pipe_1[1]);
		sleep(2);	   //延时,等待子进程写数据
		
		if(read(pipe_1[0],str,5)>0)
		printf("read hello successful\n");
		close(pipe_1[0]);
		
		exit(0);
	}
	else              //子进程
	{
		close(pipe_1[0]);
		
		if(write(pipe_1[1],"hello",5)>0)
		printf("writed hello successful\n");	
	
		close(pipe_1[1]);
		waitpid(pipe_1,NULL,0);   //写入完毕,等待父进程读取结束
		exit(0);
	}

}

运行结果



注意:如果改成子进程等待,父进程写数据。则会因为父进程提前结束出现如图所示情况



2、fifo 有名管道

mkfifo(FIFO_SERVER,O_CREAT|O_EXCL|O_RDWR)

创建fifo,FIFO_SERVER即为FIFO名字,操作如同文件


实例:创建FIFO,先往FIFO里写入“123”,再将其读出


/*=============================write=========================================*/
#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_RDWR|O_NONBLOCK,0);//文件已经存在,打开方式可以为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;
}
</pre><pre name="code" class="cpp"><pre name="code" class="cpp">/*=============================read=========================================*/
#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); //perror后面需要加exit}while(1){memset(buf_r,0,sizeof(buf_r));if((nread=read(fd,buf_r,100))==-1){if(errno==EAGAIN)//读的时候需要判断errnoprintf("no data yet\n");}printf("read %s from FIFO\n",buf_r);sleep(1);} //后面三句话是不会被运行到的,但不会影响程序运行的效果当程序在上面的死循环中执行时收到信号后会马上结束运行而没有执行后面的三句话。这些会在后面的信号处理中讲到,现在不理解没有关系,这个问题留给大家学习了信号处理之后来解决。close(fd); //关闭管道pause(); /*暂停,等待信号*/unlink(FIFO); //删除文件}

 


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值