进程管道通信

来源:互联网 发布:单片机串口是什么 编辑:程序博客网 时间:2024/06/05 15:14

一、管道通信

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);//文件已经存在,打开方式可以为0if(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 0
原创粉丝点击