IPC通信之有名管道

/*first used pipe create pipe,then open and operate the pipe,last close pipe*/

#include
#include
#include
#include
main()
{
  //1.create pipe
    mkfifo("p.pipe",0666);
  //2.open pipe
    int fd=open("p.pipe",O_RDWR);
  //3.write content to pipe
    char buf[30]={};
    read(0,buf,30);
    write(fd,buf,strlen(buf));
  //4.close pipe
    close(fd);
}

===========================================

/*another app can used the pipe by open the pipe of the same name above*/

/*default p.pipe be ceated on current directory*/

#include
#include
#include
main()
{
  //1.open pipe
   int fd=open("p.pipe",O_RDWR);

  //2.read content from pipe
   char buf[30]={};
   read(fd,buf,30);
   printf("%s\n",buf);
  //3.close pipe
   close(fd);
}

 

你可能感兴趣的:(linux)