#读代码:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <string.h>
#include <fcntl.h>
#include <sys/select.h>
#include <sys/stat.h>
#include <errno.h>
int main()
{
 int rfd;
 char str[32];
 printf("读程序\n");
 mkfifo("fifo",S_IWUSR|S_IRUSR|S_IRGRP|S_IROTH);//管道只能实现单向通信,需创建管道文件
 rfd=open("fifo",O_RDONLY);//只读且阻塞

 /*if(rfd==-1)
 {
     printf("Open file error\n");
     exit(1);
 }*/
 
 while(1)
 {
    
    memset(str,0,sizeof(str));
    if((i=read(rfd,str,sizeof(str)))>0)
        printf("read:%s\n",str);
 }
    close(rfd);
}

#写代码:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/stat.h>
int main(int argc,char *argv[])
{
 int wfd;
 char str[32];
 mkfifo("fifo",S_IWUSR|S_IRUSR|S_IRGRP|S_IROTH);           //管道只能实现单向通信,需创建管道文件
 sscanf(argv[1],"%s",str);//编译写入str
 wfd=open("fifo",O_WRONLY);//只写且阻塞
 if(wfd<=0)
  return 0;
 write(wfd,str,sizeof(str));
 close(wfd);
 exit(0);
}