Linux_ mkfifo 命名管道 操作demo

来源:互联网 发布:淘宝卖家开店 编辑:程序博客网 时间:2024/05/16 09:36

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!\n");    }    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!\n");    }    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!\n");            exit(1);        }    }    printf("process(%d) is opening fifo...\n", getpid());       fd = open(FIFO_NAME, O_RDONLY);       //fd = open(FIFO_NAME, O_RDONLY | O_NONBLOCK);    printf("process(%d) had opened fifo. ret = %d\n", 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!\n");            exit(1);        }    }    printf("process(%d) is opening fifo...\n", getpid());       fd = open(FIFO_NAME, O_WRONLY);    printf("process(%d) had opened fifo. ret = %d\n", 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!\n");            exit(1);        }    }    fifo_fd = open(MY_FIFO, O_RDONLY);    if (fifo_fd == -1) {        printf("open fifo failed!\n");        exit(1);    }    printf("open fifo succeed!\n");    while(1) {        ret = read(fifo_fd, buff, sizeof(buff));        if (ret > 0) {            printf("read %d bytes!\n", ret);        } else if (ret == 0) {            printf("read finished!\n");            break;        } else if (ret == -1) {            printf("read failed!\n");            exit(1);        }        cnt += ret;         }    printf("read finished! cnt=%d\n", 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!\n");            exit(1);        }    }    fifo_fd = open(MY_FIFO, O_WRONLY);    if (fifo_fd == -1) {        printf("open fifo failed!\n");        exit(1);    }    printf("open fifo succeed!\n");    while (cnt < DATA_SIZE) {        ret  = write(fifo_fd, buff, sizeof(buff));        if (ret == -1) {            printf("write fifo failed!\n");            exit(1);        }         cnt += ret;        printf("write %d bytes to fifo! cnt=%d\n", ret, cnt);           }    printf("write finished!cnt = %d\n", 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!\n");            exit(1);        }    }    fifo_fd = open(MY_FIFO, O_RDONLY);    if (fifo_fd == -1) {        printf("open fifo failed!\n");        exit(1);    }    printf("open fifo succeed!\n");    while(1) {        ret = read(fifo_fd, buff, sizeof(buff));        if (ret > 0) {            buff[ret] =0;            printf("received: %s\n", buff);        } else if (ret == 0) {            printf("read finished!\n");            break;        } else if (ret == -1) {            printf("read failed!\n");            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!\n");            exit(1);        }    }    fifo_fd = open(MY_FIFO, O_WRONLY);    if (fifo_fd == -1) {        printf("open fifo failed!\n");        exit(1);    }    printf("open fifo succeed!\n");    while(1) {        scanf("%s", buff);        if (!strcmp(buff, "exit")) {            break;        }        ret  = write(fifo_fd, buff, sizeof(buff));        if (ret == -1) {            printf("write fifo failed!\n");            exit(1);        }     }       printf("write finished!cnt = %d\n", cnt);    close(fifo_fd);}
0 0
原创粉丝点击