1
#include<head.h>
int main(int argc, char const *argv[])
{
if(mkfifo("./linux",0664)==-1)
{
perror("mkfifo eerror");
return -1;
}
int qfd=open("./guandao.txt",O_CREAT|O_WRONLY|O_TRUNC,0664);
if(qfd==-1)
{
perror("open error");
return -1;
}
pid_t pid=fork();
if(pid<0)
{
perror("pid error");
return -1;
}
if(pid==0)
{
int wfd=open("./linux",O_WRONLY);
if(wfd==-1)
{
perror("open error");
return -1;
}
printf("管道文件已打开\n");
char wbuf[128]="";
while(1)
{
printf("请输入>>");
fgets(wbuf,sizeof(wbuf),stdin);
wbuf[strlen(wbuf)-1]=0;
write(wfd,wbuf,strlen(wbuf));
write(qfd,wbuf,strlen(wbuf));
if(strcmp(wbuf,"quit")==0)
{
break;
}
}
close(wfd);
exit(EXIT_SUCCESS);
}
int rfd=open("./linux",O_RDONLY);
if(rfd==-1)
{
perror("open error");
return -1;
}
printf("管道读端已打开\n");
char rbuf[128]="";
while(1)
{
bzero(rbuf,sizeof(rbuf));
read(rfd,rbuf,sizeof(rbuf));
if(strcmp(rbuf,"quit")==0)
{
break;
}
printf("收到消息为:%s\n",rbuf);
}
close(rfd);
system("rm linux");
return 0;
}
2
1.c
#include<head.h>
int main(int argc, char const *argv[])
{
if(mkfifo("./linux1",0664)==-1)
{
perror("mkfifo error");
return -1;
}
if(mkfifo("./linux2",0664)==-1)
{
perror("mkfifo error");
return -1;
}
getchar();
system("mk linux1");
system("mk linux2");
return 0;
}
2.c
#include<head.h>
int main(int argc, char const *argv[])
{
pid_t pid=fork();
if(pid<0)
{
perror("fork error");
return -1;
}
if(pid==0)
{
int wfd=open("./linux1",O_WRONLY);
if(wfd==-1)
{
perror("open error");
return -1;
}
printf("管道1已经打开\n");
char wbuf[128]="";
while(1)
{
printf("请输入>>");
fgets(wbuf,sizeof(wbuf),stdin);
wbuf[strlen(wbuf)-1]=0;
write(wfd,wbuf,strlen(wbuf));
if(strcmp(wbuf,"quit")==0)
{
break;
}
}
close(wfd);
exit(EXIT_SUCCESS);
}
int rfd=open("./linux2",O_RDONLY);
if(rfd==-1)
{
perror("open error");
return -1;
}
printf("管道2读端已打开\n");
char rbuf[128]="";
while(1)
{
bzero(rbuf,sizeof(rbuf));
read(rfd,rbuf,sizeof(rbuf));
if(strcmp(rbuf,"quit")==0)
{
break;
}
printf("2收到的消息为:%s\n",rbuf);
}
close(rfd);
return 0;
}
3.c
#include<head.h>
int main(int argc, char const *argv[])
{
pid_t pid=fork();
if(pid<0)
{
perror("fork error");
return -1;
}
if(pid==0)
{
int rfd=open("./linux1",O_RDONLY);
if(rfd==-1)
{
perror("open error");
return -1;
}
printf("管道1读端已打开\n");
char rbuf[128]="";
while(1)
{
bzero(rbuf,sizeof(rbuf));
read(rfd,rbuf,sizeof(rbuf));
if(strcmp(rbuf,"quit")==0)
{
break;
}
printf("1收到的消息为:%s\n",rbuf);
}
close(rfd);
exit(EXIT_SUCCESS);
}
int wfd=open("./linux2",O_WRONLY);
if(wfd==-1)
{
perror("open error");
return -1;
}
printf("管道2已经打开\n");
char wbuf[128]="";
while(1)
{
printf("请输入>>");
fgets(wbuf,sizeof(wbuf),stdin);
wbuf[strlen(wbuf)-1]=0;
write(wfd,wbuf,strlen(wbuf));
if(strcmp(wbuf,"quit")==0)
{
break;
}
}
close(wfd);
return 0;
}
思维导图