1 /*****************************************************************
2 * Copyright (C) 2018 FBI WARNING. All rights reserved.
3 *
4 * 文件名称:fifo_write.c
5 * 创 建 者:constantine
6 * 创建日期:2018年02月26日
7 * 描 述:
8 *
9 *****************************************************************/
10
11 #include <stdio.h>
12 #include <unistd.h>
13 #include <sys/types.h>
14 #include <sys/wait.h>
15 #include <string.h>
16 #include <stdlib.h>
17 #include <sys/stat.h>
18 #include <fcntl.h>
19
20 int main()
21 {
22 int ret,fd;
23 char buf[100]={0};
24 ret = mkfifo("fifo.tmp",O_CREAT | O_EXCL);
25 if(-1 == ret)
26 {
27 perror("mkfifo");
28 exit(1);
29
30 }
上面是写的一端下面是读的一端
/*****************************************************************
* Copyright (C) 2018 FBI WARNING. All rights reserved.
*
* 文件名称:fifo_read.c
* 创 建 者:constantine
* 创建日期:2018年02月26日
* 描 述:
*
*****************************************************************/
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
int main()
{
int ret;
char buf[100] = {0};
int fd = open("fifo.tmp",O_RDONLY);
if(-1 == fd)
{
perror("read");
exit(1);
}
while(1)
{
ret = read(fd,buf,sizeof(buf));
if(-1 == ret)
{
perror("read");
exit(1);
}
if(!strcmp(buf,"bye"))
{
break;
}
printf("read from buf: %s\n",buf);
memset(buf,0,sizeof(buf));
}
return 0;
}
本文提供了一组使用FIFO(先进先出)进行进程间通信的代码示例,包括fifo_write.c和fifo_read.c两个文件。通过创建并利用FIFO文件实现数据的发送与接收过程,展示了基本的FIFO管道通信机制。

1993

被折叠的 条评论
为什么被折叠?



