实验四 进程间通信——有名管道

一、实验目的

1、了解有名管道通信的原理;

2、掌握有名管道的创建及使用方法。

二、实验内容

1、编写以非阻塞方式打开的写进程,其功能为接收用户从键盘输入的字符串,写入FIFO;

       2、编写以非阻塞方式打开的读进程,功能为从FIFO中读取数据并打印到终端,遇到字符’p’时暂停读取数据;

       3、编译并执行读进程、写进程。

三、源程序

fiforead.c:

#include <unistd.h>

#include <stdlib.h>

#include <stdbool.h>

#include <stdio.h>

#include <fcntl.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <limits.h>

#include <string.h>

int main()

{

    const char *fifo_name = "/tmp/my_fifo";

    const int open_mode = O_RDONLY;

    const char *open_mode_name = "O_RDONLY";

    int pipe_fd = -1;

    printf("进程%d以%s打开有名管道%s\n", getpid(), open_mode_name, fifo_name);

    pipe_fd = open(fifo_name, open_mode);

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

    if(pipe_fd != -1)

    {

        char buffer[PIPE_BUF + 1];

        memset(buffer, '\0', sizeof(buffer));

        int bytes_read = read(pipe_fd, buffer, PIPE_BUF);

        buffer[bytes_read] = '\0';

  &n
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值