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!\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);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值