p11-7.c

来自「SUN Solaris8平台下进程间通信」· C语言 代码 · 共 34 行

C
34
字号
#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 + =
减小字号Ctrl + -
显示快捷键?