网站的详情页面,商标图案设计自动生成,注册公司域名后如何做网站,如何建立网站教材FIFO常被称为命名管道#xff0c;以区分管道(pipe)。管道(pipe)只能用于“有血缘关系”的进程间。但通过FIFO#xff0c;不相关的进程也能交换数据。FIFO是Linux基础文件类型中的一种#xff08;p,管道文件#xff09;。但FIFO文件在磁盘上没有数据块#xff0c;仅仅用来标…FIFO常被称为命名管道以区分管道(pipe)。管道(pipe)只能用于“有血缘关系”的进程间。但通过FIFO不相关的进程也能交换数据。FIFO是Linux基础文件类型中的一种p,管道文件。但FIFO文件在磁盘上没有数据块仅仅用来标识内核中一条通道。各进程可以打开这个文件进行read/write实际上是在读写内核通道这样就实现了进程间通信。另外使用统一fifo文件可以有多个读端和多个写端。
FIFO文件p的创建方式1. 命令mkfifo 管道名 2. 库函数int mkfifo(const char *pathname, mode_t mode); 成功0 失败-1 当mkfifo的第一个参数是一个已经存在的路径名时则会出错返回-1因此一般使用该函数时要判断返回值。 第二个参数为8进制数一般设置为0666即可即管道文件只需要读写权限不需要执行权限。 包含库文件#include sys/types.h #include sys/stat.h
一旦创建了一个FIFO就可以使用open打开它常见的文件I/O函数都可用于fifo。如close、read、write、unlink等。unlink可以删除一个管道文件。
注意当进程对命名管道的使用结束后命名管道依然存在于文件系统中除非对其进行删除操作。命名管道的数据读取后也会消失不能反复读取即且严格遵循先进先出的规则。因此每次命名管道文件使用完后其大小为0字节不会产生中间临时文件。 //使用函数创建命名管道命令行参数指定文件名字
#include sys/types.h
#include sys/stat.h
#include stdio.h
#include stdlib.hint main(int argc , char *argv[ ])
{if(argc 2){printf(./a.out fifoname\n);exit(1);}int ret;mode_t mode0666;ret mkfifo(argv[1],mode);if(ret -1){perror(mkfifo);exit(1);}exit(0);
}
//向管道文件中读写数据实现进程间通信
#include stdio.h
#include unistd.h
#include sys/stat.h
#include sys/types.h
#include fcntl.h
#include stdlib.h
#include string.hvoid sys_err(char *str)
{perror(str);exit(1);
}int main(int argc, char *argv[])
{int fd, i;char buf[4096];if (argc 2) {printf(Enter like this: ./a.out fifoname\n);return -1;}fd open(argv[1], O_WRONLY);if (fd 0)sys_err(open);i 0;while (1) {sprintf(buf, hello itcast %d\n, i);write(fd, buf, strlen(buf));sleep(1);}close(fd); return 0;
}#include stdio.h
#include unistd.h
#include sys/stat.h
#include sys/types.h
#include fcntl.h
#include stdlib.h
#include string.hvoid sys_err(char *str)
{perror(str);exit(1);
}int main(int argc, char *argv[])
{int fd, len;char buf[4096];if (argc 2) {printf(./a.out fifoname\n);return -1;}fd open(argv[1], O_RDONLY);if (fd 0)sys_err(open);while (1) {len read(fd, buf, sizeof(buf));write(STDOUT_FILENO, buf, len);sleep(3); //多个读端时应增加睡眠秒数放大效果}close(fd);return 0;
}