zoukankan      html  css  js  c++  java
  • Linux_ mkfifo 命名管道 操作demo

    main1.c

    #include <stdlib.h>
    #include <stdio.h>
    
    #define MY_FIFO  "/tmp/myfifo"
    
    int main(void)
    {
        int ret;
    
        ret = mkfifo(MY_FIFO, 0777);
        if (ret == -1) {
            printf("create fifo failed!
    ");
        }
    
        return 0;
    }
    

    main2.c

    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <stdio.h>
    
    #define MY_FIFO2  "/tmp/myfifo2"
    
    int main(void)
    {
        int ret;
    
        //ret = mkfifo(MY_FIFO, 0777);
    
        ret = mknod(MY_FIFO2, 0777|S_IFIFO, 0);
        if (ret == -1) {
            printf("create fifo failed!
    ");
        }
    
        return 0;
    }
    

    main3_r.c

    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <stdlib.h>
    #include <stdio.h>
    
    #define FIFO_NAME "/tmp/myfifo"
    
    int main(void)
    {
        int ret;
        int fd;
    
        /* œ»ºÏ≤È÷∏∂®µƒ√¸√˚π‹µ¿ «∑Ò“—æ≠¥Ê‘⁄ */
        if (access(FIFO_NAME, F_OK) == -1) {
            ret = mkfifo(FIFO_NAME, 0777);
            if (ret == -1) {
                printf("creat fifo failed!
    ");
                exit(1);
            }
        }
    
        printf("process(%d) is opening fifo...
    ", getpid());
           fd = open(FIFO_NAME, O_RDONLY);
           //fd = open(FIFO_NAME, O_RDONLY | O_NONBLOCK);
        printf("process(%d) had opened fifo. ret = %d
    ", getpid(), fd);
    
        sleep(10);
        return 0;
    }
    

    main3_w.c

    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <stdlib.h>
    #include <stdio.h>
    
    #define FIFO_NAME "/tmp/myfifo"
    
    int main(void)
    {
        int ret;
        int fd;
    
        /* œ»ºÏ≤È÷∏∂®µƒ√¸√˚π‹µ¿ «∑Ò“—æ≠¥Ê‘⁄ */
        if (access(FIFO_NAME, F_OK) == -1) {
            ret = mkfifo(FIFO_NAME, 0777);
            if (ret == -1) {
                printf("creat fifo failed!
    ");
                exit(1);
            }
        }
    
        printf("process(%d) is opening fifo...
    ", getpid());
           fd = open(FIFO_NAME, O_WRONLY);
        printf("process(%d) had opened fifo. ret = %d
    ", getpid(), fd);
    
        sleep(10);
        return 0;
    }
    

    main4_r.c

    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <limits.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    #define MY_FIFO  "/tmp/myfifo"
    #define DATA_SIZE  (10*1024*1024)
    
    int main(void)
    {
        int ret;
        int fifo_fd;
        int cnt = 0;
        char buff[PIPE_BUF];
    
        if (access(MY_FIFO, F_OK) == -1) {
            ret = mkfifo(MY_FIFO, 0777);
            if (ret == -1) {
                printf("create fifo failed!
    ");
                exit(1);
            }
        }
    
        fifo_fd = open(MY_FIFO, O_RDONLY);
        if (fifo_fd == -1) {
            printf("open fifo failed!
    ");
            exit(1);
        }
        printf("open fifo succeed!
    ");
    
        while(1) {
            ret = read(fifo_fd, buff, sizeof(buff));
            if (ret > 0) {
                printf("read %d bytes!
    ", ret);
            } else if (ret == 0) {
                printf("read finished!
    ");
                break;
            } else if (ret == -1) {
                printf("read failed!
    ");
                exit(1);
            }
    
            cnt += ret;     
        }
    
        printf("read finished! cnt=%d
    ", cnt);
    }
    

    main4_w.c

    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <limits.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    
    #define MY_FIFO  "/tmp/myfifo"
    #define DATA_SIZE  (10*1024*1024)
    
    int main(void)
    {
        int ret;
        int fifo_fd;
        int cnt = 0;
        char buff[PIPE_BUF];
    
        if (access(MY_FIFO, F_OK) == -1) {
            ret = mkfifo(MY_FIFO, 0777);
            if (ret == -1) {
                printf("create fifo failed!
    ");
                exit(1);
            }
        }
    
        fifo_fd = open(MY_FIFO, O_WRONLY);
        if (fifo_fd == -1) {
            printf("open fifo failed!
    ");
            exit(1);
        }
        printf("open fifo succeed!
    ");
    
        while (cnt < DATA_SIZE) {
            ret  = write(fifo_fd, buff, sizeof(buff));
            if (ret == -1) {
                printf("write fifo failed!
    ");
                exit(1);
            } 
    
            cnt += ret;
            printf("write %d bytes to fifo! cnt=%d
    ", ret, cnt);       
        }
    
        printf("write finished!cnt = %d
    ", cnt);
        close(fifo_fd);
    }
    

    main5_r.c

    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <limits.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    #define MY_FIFO  "/tmp/myfifo"
    #define DATA_SIZE  (10*1024*1024)
    
    int main(void)
    {
        int ret;
        int fifo_fd;
        int cnt = 0;
        char buff[PIPE_BUF];
    
        if (access(MY_FIFO, F_OK) == -1) {
            ret = mkfifo(MY_FIFO, 0777);
            if (ret == -1) {
                printf("create fifo failed!
    ");
                exit(1);
            }
        }
    
        fifo_fd = open(MY_FIFO, O_RDONLY);
        if (fifo_fd == -1) {
            printf("open fifo failed!
    ");
            exit(1);
        }
        printf("open fifo succeed!
    ");
    
        while(1) {
            ret = read(fifo_fd, buff, sizeof(buff));
            if (ret > 0) {
                buff[ret] =0;
                printf("received: %s
    ", buff);
            } else if (ret == 0) {
                printf("read finished!
    ");
                break;
            } else if (ret == -1) {
                printf("read failed!
    ");
                exit(1);
            }
        }
    
        close(fifo_fd);
        return 0;
    }
    

    main5_w.c

    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <limits.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    
    #define MY_FIFO  "/tmp/myfifo"
    #define DATA_SIZE  (10*1024*1024)
    
    int main(void)
    {
        int ret;
        int fifo_fd;
        int cnt = 0;
        char buff[PIPE_BUF];
    
        if (access(MY_FIFO, F_OK) == -1) {
            ret = mkfifo(MY_FIFO, 0777);
            if (ret == -1) {
                printf("create fifo failed!
    ");
                exit(1);
            }
        }
    
        fifo_fd = open(MY_FIFO, O_WRONLY);
        if (fifo_fd == -1) {
            printf("open fifo failed!
    ");
            exit(1);
        }
        printf("open fifo succeed!
    ");
    
        while(1) {
            scanf("%s", buff);
            if (!strcmp(buff, "exit")) {
                break;
            }
    
            ret  = write(fifo_fd, buff, sizeof(buff));
            if (ret == -1) {
                printf("write fifo failed!
    ");
                exit(1);
            } 
        }   
    
        printf("write finished!cnt = %d
    ", cnt);
        close(fifo_fd);
    }
    
  • 相关阅读:
    树上莫队 SPOJ COT2
    UVA 11478(差分约束 + 二分)
    可图的度序列判断与构造
    Codeforces Round #306 (Div. 2) 550A Two Substrings
    UVA 11300 分金币
    HDU 2546 饭卡(01 背包)
    hdu 1142 最短路+记忆化
    codeforces ice cave
    codeforces school mark(贪心)
    JavaScript函数
  • 原文地址:https://www.cnblogs.com/Sico2Sico/p/5384214.html
Copyright © 2011-2022 走看看