一、实验目的
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


479

被折叠的 条评论
为什么被折叠?



