UNIX FIFO: the process hangs if I don't close the input side of the fifo
我刚开始使用UNIX FIFO,在尝试第一个FIFO程序时发现了一些东西。 该程序以这种方式工作:创建FIFO后,使用
谢谢你的耐心。 这是主要功能的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | int main(int argc, char *argv[]) { if(argc != 2) { printf("An argument must be specified\ "); return -1; } int ret = mkfifo("./fifo.txt", 0644); char buf; if(ret < 0) { perror("Error creating FIFO"); return -1; } pid_t pid = fork(); if(pid < 0) { perror("Error creating child process"); return -1; } if(pid == 0) /* child */ { int fd = open("./fifo.txt", O_RDONLY); /* opens the fifo in reading mode */ while(read(fd, &buf, 1) > 0) { write(STDOUT_FILENO, &buf, 1); } write(STDOUT_FILENO,"\ ", 1); close(fd); return 0; } else /* father */ { int fd = open("./fifo.txt", O_WRONLY); /* opens the fifo in writing mode */ write(fd, argv[1], strlen(argv[1])); close(fd); waitpid(pid, NULL, 0); return 0; } } |
首先,第一件事:您发布的代码存在多个问题。
未成年人:通常的称呼是"父母和孩子",而不是"父亲和孩子"。
我已修改您的程序以纠正此问题并提高可读性:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | #include <sys/types.h> #include <sys/stat.h> #include <sys/wait.h> #include <fcntl.h> #include <stdio.h> #include <string.h> #include <unistd.h> int main(int argc, char *argv[]) { if (argc != 2) { printf("An argument must be specified\ "); return 1; } int ret = mkfifo("./fifo.txt", 0644); char buf; if (ret < 0) { perror("Error creating FIFO"); return 1; } pid_t pid = fork(); if (pid < 0) { perror("Error creating child process"); return 1; } if (pid == 0) { /* child */ int fd = open("./fifo.txt", O_RDONLY); /* opens the fifo in reading mode */ while(read(fd, &buf, 1) > 0) { write(STDOUT_FILENO, &buf, 1); } write(STDOUT_FILENO,"\ ", 1); close(fd); return 0; } else { /* parent */ int fd = open("./fifo.txt", O_WRONLY); /* opens the fifo in writing mode */ write(fd, argv[1], strlen(argv[1])); close(fd); waitpid(pid, NULL, 0); return 0; } } |
但最重要的是,您没有提到要使用的操作系统和编译器。
我无法重现该问题,并且我怀疑它可能与上面列出的问题之一有关。