读取:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc, char const *argv[])
{
int fd = open("/home/gec/pipe",O_RDWR);
if(fd < 0)
{
perror("管道打开失败\n");
return -1;
}else
{
printf("管道打开成功\n");
}
while (1)
{
char buf[1024] = {0};
read(fd,buf,1024);
if(buf != "")
{
printf("%s\n",buf);
}
}
return 0;
}
写入:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
int main(int argc, char const *argv[])
{
int fd = open("/home/gec/pipe",O_RDWR);
if(fd < 0)
{
perror("管道打开失败\n");
return -1;
}else
{
printf("管道打开成功\n");
}
while (1)
{
char buf[1024] = {0};
scanf("%s",buf);
write(fd,buf,strlen(buf));
}
return 0;
}
效果:
总结: