有名管道fifo实现任意进程间通信

环境:linux C

功能:有名管道fifo实现任意进程间通信

/*create_fifo.c*/

#include
#include
#include
#include
#include
#include


int main()
{
if(mkfifo("myfifo",0666) != 0)
{
perror("mkfifo");
exit(-1);
}
printf("myfifo created successfully\n");
return 0;

}


/*write_fifo.c*/

#include
#include
#include
#include
#include
#define N 32
int main()
{
int pfd;
char buf[N];
//使用文件IO打开写端
if((pfd = open("myfifo",O_WRONLY)) < 0)
{
perror("open");
exit(-1);
}
printf("write opened\n");
while(1)
{
fgets(buf,N,stdin);
if(strncmp(buf,"quit",4) == 0)
break;
write(pfd,buf,N);
}
//关闭有名管道文件
close(pfd);
return 0;

}

/*read_fifo.c*/

#include
#include
#include
#include
#include
#define N 32
int main()
{
int pfd;
char buf[N];
//使用文件IO打开读端
if((pfd = open("myfifo",O_RDONLY)) < 0)
{
perror("open");
exit(-1);
}
printf("read opened\n");
while(read(pfd,buf,N) > 0)
{
fputs(buf,stdout);
}
//关闭有名管道文件
close(pfd);
return 0;
}

你可能感兴趣的:(多进程)