#include<myhead.h>
int main(int argc, const char *argv[])
{
pid_t pid;
pid=fork();
if(pid<0)
{
perror("fork error");
return -1;
}
else if(pid==0)
{
mkfifo("./myfifo1",0777);
FILE*fp;
if((fp= fopen("./myfifo1","w"))==NULL)//1写
{
perror("myfifo1 fopen error");
return -1;
}
else
{
char buf[128]="";
while(1)
{
fgets(buf,sizeof(buf)-1,stdin);
fputs(buf,fp);
fflush(fp);
if(strcmp(buf,"quit\0")==0)
{
break;
}
}
}
fclose(fp);
exit(EXIT_SUCCESS);
}
else if(pid>0)
{
mkfifo("./myfifo2",0777);
FILE*fp;
if((fp= fopen("./myfifo2","r"))==NULL)//1读,
{
perror("myfifo1 fopen error");
return -1;
}
else
{
char buf[128]="";
while(1)
{
fgets(buf,sizeof(buf)-1,fp);
// fflush(fp);
if(strcmp(buf,"quit\n")==0)
{
break;
}
printf("2:%s",buf);
}
}
fclose(fp);
exit(EXIT_SUCCESS);
wait(NULL);
}
return 0;
}
#include<myhead.h>
int main(int argc, const char *argv[])
{
pid_t pid;
pid=fork();
if(pid<0)
{
perror("fork error");
return -1;
}
else if(pid==0)
{
FILE*fp;
if((fp= fopen("./myfifo2","w"))==NULL)//2写
{
perror("myfifo1 fopen error");
return -1;
}
else
{
char buf[128]="";
while(1)
{
fgets(buf,sizeof(buf)-1,stdin);
fputs(buf,fp);
fflush(fp);
if(strcmp(buf,"quit")==0)
{
break;
}
}
}
fclose(fp);
exit(EXIT_SUCCESS);
}
else if(pid>0)
{
FILE*fp;
if((fp= fopen("./myfifo1","r"))==NULL)//1读
{
perror("myfifo1 fopen error");
return -1;
}
else
{
char buf[128]="";
while(1)
{
fgets(buf,sizeof(buf)-1,fp);
// fflush(fp);
if(strcmp(buf,"quit\n")==0)
{
break;
}
printf("1:%s",buf);
}
}
fclose(fp);
exit(EXIT_SUCCESS);
wait(NULL);
}
return 0;
}