标签:
/*************************************************************************
 > File Name:    fifo3.c
 > Description:  fifo3.c程序是生产者程序,它在需要时创建管道,然后尽可能块地向管道中写入数据
 > Author:       Liubingbing
 > Created Time: 2015年07月14日 星期二 21时28分28秒
 > Other:        fifo3.c
 ************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.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()
{
	int pipe_fd;
	int res;
	int open_mode = O_WRONLY;
	int bytes_sent = 0;
	char buffer[BUFFER_SIZE + 1];
	/* 检查FIFO_NAME文件是否存在,如果不存在就创建它 */
	if (access(FIFO_NAME, F_OK) == -1) {
		/* mkfifo创建命名管道(即特殊类型的文件FIFO) */
		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_OWRONLY\n", getpid());
	/* open函数以O_WRONLY方式打开FIFO文件,如果成功pipe_fd指向打开的文件 */
	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) {
			/* write函数从buffer指向的内存中写入BUFFER_SIZE个字节到pipe_fd文件中
			 * 如果成功则返回实际写入的字节数 */
			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);
}
第一个程序是生产者程序,它在需要时创建管道,然后尽可能地向管道中写入数据/*************************************************************************
 > File Name:    fifo4.c
 > Description:  fifo4.c程序是消费者程序,它从FIFO读取数据并丢弃它们
 > Author:       Liubingbing
 > Created Time: 2015年07月14日 星期二 22时07分34秒
 > Other:        fifo4.c
 ************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.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()
{
	int pipe_fd;
	int res;
	int open_mode = O_RDONLY;
	char buffer[BUFFER_SIZE + 1];
	int bytes_read = 0;
	memset(buffer, '\0', sizeof(buffer));
	
	printf("Process %d opening FIFO O_RDONLY\n", getpid());
	/* open函数打开FIFO_NAME文件,以open_mode的方式(即O_RDONLY)
	 * 如果成功,则返回文件描述符 */
	pipe_fd = open(FIFO_NAME, open_mode);
	printf("Process %d result %d\n", getpid(), pipe_fd);
	if (pipe_fd != -1) {
		do {
			/* read函数从pipe_fd指向的文件中读入BUFFER_SIZE个字节的数据到buffer指向的内存 
			 * 如果成功,返回实际读入数据的字节数 */
			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);
}
第二个程序是消费者程序,它的代码要简单的多,它从FIFO读取数据并丢弃它们.版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/yiranant/article/details/46885413