1> 使用有名管道完成两个进程之间相互通信
#include<myhead.h>
int main(int argc, const char *argv[])
{
//
int wfd=-1;
if((wfd=open("./pipe1",O_RDONLY))==-1)
{
perror("wfd open error");
return -1;
}
printf("读端打开成功\n");
char wbuf[128]="";
while(1)
{
memset(wbuf,0,sizeof(wbuf));
read(wfd,wbuf,sizeof(wbuf));
printf("%s\n",wbuf);
if(strcmp(wbuf,"quit")==0)
break;
}
close(wfd);
/************************************/
int rfd=-1;
if((rfd=open("./pipe2",O_WRONLY))==-1)
{
perror("rfd open error");
return -1;
}
printf("写端打开成功,请开始输入:\n");
fflush(stdout);
char sbuf[128]="";
while(1)
{
memset(sbuf,0,sizeof(sbuf));
read(0,sbuf,sizeof(sbuf));
sbuf[strlen(sbuf)-1]='\0';
write(rfd,sbuf,sizeof(sbuf));
if(strcmp(sbuf,"quit")==0)
break;
}
close(rfd);
return 0;
}
#include<myhead.h>
int main(int argc, const char *argv[])
{
//
int rfd=-1;
if((rfd=open("./pipe1",O_WRONLY))==-1)
{
perror("rfd open error");
return -1;
}
printf("写端打开成功,请开始输入:\n");
char sbuf[128]="";
while(1)
{
memset(sbuf,0,sizeof(sbuf));
read(0,sbuf,sizeof(sbuf));
sbuf[strlen(sbuf)-1]='\0';
write(rfd,sbuf,sizeof(sbuf));
if(strcmp(sbuf,"quit")==0)
break;
}
close(rfd);
/**************************************************/
int wfd=-1;
if((wfd=open("./pipe2",O_RDONLY))==-1)
{
perror("wfd open error");
return -1;
}
printf("读端打开成功\n");
char wbuf[128]="";
while(1)
{
memset(wbuf,0,sizeof(wbuf));
read(wfd,wbuf,sizeof(wbuf));
printf("%s\n",wbuf);
fflush(stdout);
if(strcmp(wbuf,"quit")==0)
break;
}
close(wfd);
return 0;
}