📄 p11-7.c
字号:
#include <stdio.h>#include <stdlib.h>#include <fcntl.h>#include <sys/types.h>#include <sys/stat.h>#include <unistd.h>#include <limits.h>#define FIFO_FILE "myfifo"int main(void){ int fifo_fd, n, total_bytes = 0; char readbuf[PIPE_BUF+1]; /* 如果FIFO不存在,创建它*/ if (access(FIFO_FILE, F_OK) == -1) { if (mkfifo(FIFO_FILE, 0666) != 0) { printf( "Could not create fifo %s\n", FIFO_FILE); exit(EXIT_FAILURE); } } /* 打开此FIFO文件 */ if ((fifo_fd = open(FIFO_FILE, O_RDONLY)) <0 ){ printf( "Could not open fifo %s\n", FIFO_FILE); exit(EXIT_FAILURE); } printf("Process %d opened: %s\n", getpid(), FIFO_FILE); while( (n = read(fifo_fd, readbuf, PIPE_BUF)) > 0 ){ printf("%d received %d bytes\n", getpid(), n); total_bytes +=n; } close(fifo_fd); printf("Process %d received %d bytes then finished\n", getpid(),total_bytes); return(EXIT_SUCCESS);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -