进程通信(接上篇PFIFO)

对于 pipe 只能用于一个进程家族之间通信,如果想要让管道在更宽泛的环境中,那是不行的,原因是它没有名字的,另外的进程看不到它,这样就有了命名管道。
命名管道叫 FIFO ,可以用 mkfifo()在磁盘 上创建一个 FIFO 文件
这就是它与 pipe 的本质区别, pipe 完全就是存在与内存中
当进程想通过该 FIFO 来通信时就可以标准的 open 打开该文件,然后开始读写操作。
对于 FIFO 的读写实现,它与 pipe 的区别在于: FIFO open 这一操作,而 pipe 是在调用 pipe 这个系统调用时直接创建了一对文件描述符用于通信。
并且, FIFO open 操作还有些细致的地方要考虑,例如如果写者先打开,尚无读者,那么肯定是不能通信了,所以就需要先去睡眠等待读者打开该 FIFO ,反之对读者亦然。
所需头文件

     #include <sys/types.h>

     #include <sys/stat.h>

 

命名管道FIFO举例:

写端:

#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

#define TEN_MEG (1024 * 1024 * 10)

  int main()

{

    intpipe_fd;

    int res;

    intopen_mode = O_WRONLY;

    intbytes_sent = 0;

    char buffer[BUFFER_SIZE + 1];

    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);

            }

            bytes_sent += res;

        }

        (void)close(pipe_fd);

    }

    else {

        exit(EXIT_FAILURE);       

    }

    printf("Process %d finished\n", getpid());

    exit(EXIT_SUCCESS);

}

读端:

#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

int main()

{

    intpipe_fd;

    int res;

    intopen_mode = O_RDONLY;

    char buffer[BUFFER_SIZE + 1];

    intbytes_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);

            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);

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值