FIFO 和普通文件

来源:互联网 发布:使用ip连接mysql数据库 编辑:程序博客网 时间:2024/06/02 07:20
FIFO; a named pipe
[root@localhost ch13]# cat fifo3.c /*************write*****/#include <unistd.h>#include <stdlib.h>#include <stdio.h>#include <string.h>#include <fcntl.h>#include <limits.h>#include <sys/types.h>#include <sys/stat.h>#define FIFO_NAME "/tmp/my_fifo"#define BUFFER_SIZE PIPE_BUF//PIPE_BUF=4096#define TEN_MEG (1024 * 1024 * 10)int main(){    int pipe_fd;    int res;    int open_mode = O_WRONLY;    int bytes_sent = 0;    char buffer[BUFFER_SIZE + 1]="agds";    if (access(FIFO_NAME, F_OK) == -1) {        res = mkfifo(FIFO_NAME, 0777);        if (res != 0) {            fprintf(stderr, "Could not create fifo %s\n", FIFO_NAME);            exit(EXIT_FAILURE);        }    }    printf("Process %d opening FIFO O_WRONLY\n", getpid());    pipe_fd = open(FIFO_NAME, open_mode);    printf("Process %d result %d\n", getpid(), pipe_fd);    if (pipe_fd != -1) {        while(bytes_sent < TEN_MEG) {            res = write(pipe_fd, buffer, BUFFER_SIZE);            if (res == -1) {                fprintf(stderr, "Write error on pipe\n");                exit(EXIT_FAILURE);            }printf("%s\n",buffer);            bytes_sent += res;        }        (void)close(pipe_fd);     }    else {        exit(EXIT_FAILURE);            }    printf("Process %d finished\n", getpid());    exit(EXIT_SUCCESS);}[root@localhost ch13]# [root@localhost ch13]# [root@localhost ch13]# [root@localhost ch13]# cat fifo4.c /*************read*****/#include <unistd.h>#include <stdlib.h>#include <stdio.h>#include <string.h>#include <fcntl.h>#include <limits.h>#include <sys/types.h>#include <sys/stat.h>#define FIFO_NAME "/tmp/my_fifo"#define BUFFER_SIZE PIPE_BUFint main(){    int pipe_fd;    int res;    int open_mode = O_RDONLY;    char buffer[BUFFER_SIZE + 1];    int bytes_read = 0;    memset(buffer, '\0', sizeof(buffer));        printf("Process %d opening FIFO O_RDONLY\n", getpid());    pipe_fd = open(FIFO_NAME, open_mode);    printf("Process %d result %d\n", getpid(), pipe_fd);    if (pipe_fd != -1) {        do {            res = read(pipe_fd, buffer, BUFFER_SIZE);printf("%s\n",buffer);            bytes_read += res;        } while (res > 0);        (void)close(pipe_fd);    }    else {        exit(EXIT_FAILURE);    }    printf("Process %d finished, %d bytes read\n", getpid(), bytes_read);    exit(EXIT_SUCCESS);}[root@localhost ch13]# 
[root@localhost ch13]# rm /tmp/my_fifo -f[root@localhost ch13]# ./fifo3 &[2] 17216[root@localhost ch13]# Process 17216 opening FIFO O_WRONLY[root@localhost ch13]# time ./fifo4 ...agdsagdsagdsagdsagdsagdsagdsagdsagdsagdsagdsagdsagdsagdsagdsagdsagdsagdsagdsagdsProcess 17220 finished, 10485760 bytes readreal    0m0.610suser    0m0.002ssys    0m0.284sProcess 17216 finished[root@localhost ch13]# //FIFO文件是fifo3创建,所以先运行fifo3,如果先运行fifo4则找不到my_fifo而出错
fifo3 is blocked when it opens without NONBLOCK mode until fifo4 also open the same FIFO file
[root@localhost ch13]# ll /tmp/my_fifo prwxr-xr-x. 1 root root 0 Aug 26 03:14 /tmp/my_fifo
the FIFO file my_fifo is 0 bytes


如果他们之间读写的不是FIFO文件,而是一个普通文件会是怎么样呢

[root@localhost ch13]# rm /tmp/my_fifo -f[root@localhost ch13]# touch /tmp/my_fifo [root@localhost ch13]# ll /tmp/my_fifo -rw-r--r--. 1 root root 0 Aug 26 03:20 /tmp/my_fifo
run fifo3 and fifo4 just as the cmd  above,then
Process 17276 finished, 10485760 bytes readreal0m0.253suser0m0.006ssys0m0.235s
[root@localhost ch13]# ll /tmp/my_fifo -rw-r--r--. 1 root root 10485760 Aug 26 03:23 /tmp/my_fifo
看来,普通文件用于进程间通信也不慢哈
并且还可以从外部查看通信的内容和大小
但是不管open中是否用了O_NONBLOCK,都会O_NONBLOCK的






原创粉丝点击