📄 test5.c
字号:
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <string.h>#include <errno.h>#include <sys/stat.h>#include <fcntl.h>#define FIFO "/tmp/fifo.test"void child_process_main(void);void parent_process_main(void);int main(int argc, char **argv){ umask(0); // int mkfifo(const char *path, mode_t mode); if (mkfifo(FIFO, 0644) < 0) { fprintf(stderr, "mkfifo() failed: %s\n", strerror(errno)); if (errno == EEXIST) { fprintf(stdout, "The fifo file is alreay existed.\n"); } else { exit(1); } } pid_t pid; if ((pid = fork()) < 0) { fprintf(stderr, "fork() failed: %s\n", strerror(errno)); unlink(FIFO); exit(1); } else if (pid == 0) { // XXX: child process //sleep(3); child_process_main(); exit(0); } parent_process_main(); unlink(FIFO); return 0;}void child_process_main(void){ int fd; //fd = open(FIFO, O_RDONLY | O_NONBLOCK); fd = open(FIFO, O_RDONLY); if (fd < 0) { fprintf(stderr, "[%d]open FIFO %s for reading failed: %s\n", getpid(), FIFO, strerror(errno)); } else { fprintf(stdout, "[%d]open FIFO %s for reading successed, fd = %d\n", getpid(), FIFO, fd); sleep(10); close(fd); } sleep(100);}void parent_process_main(void){ int fd; //int open(const char *pathname, int flags); //int open(const char *pathname, int flags, mode_t mode); //fd = open(FIFO, O_WRONLY | O_NONBLOCK); fd = open(FIFO, O_WRONLY); if (fd < 0) { fprintf(stderr, "[%d]open FIFO %s for writting failed: %s\n", getpid(), FIFO, strerror(errno)); } else { fprintf(stdout, "[%d]open FIFO %s for writting successed, fd = %d\n", getpid(), FIFO, fd); sleep(10); close(fd); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -