进程的管道通信pipe(int)函数的运用

/*
 * pipe.c
 *
 *  Created on: 2012-7-13
 *      Author: liwei.cai
 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <errno.h>

#define MAX_DATA_LEN 256
#define DELAY_TIME 1

int main()
{
	pid_t pid ;
	int pipe_fd[2];
	char buf[MAX_DATA_LEN];
	const char data[] = "Pipe Test Program";
	int real_read, real_write;

	memset((void*)buf, 0, sizeof(buf));
	//创建管道
	if (pipe(pipe_fd) < 0)
	{
		printf("pipe create error\n");
		exit(1);
	}

	//创建一个子进程
	if ((pid = fork()) == 0)
	{
		//子进程关闭写描述符,并通过使子进程暂停1秒等待父进程已关闭相应的读描述符
		close(pipe_fd[1]);
		sleep(DELAY_TIME*3);

		//子进程读取管道内容
		if((real_read = read(pipe_fd[0], buf, MAX_DATA_LEN)) > 0)
		{
			printf("%d bytes read from the pipe is '%s'\n",real_read,buf);
		}
		//关闭子进程读描述符
		close(pipe_fd[0]);
		exit(0);
	}
	else if (pid > 0)
	{
		//父进程关闭读描述符,并通过使父进程暂停1秒等待子进程已关闭相应的写描述符
		close(pipe_fd[0]);
		sleep(DELAY_TIME);

		if ((real_write = write(pipe_fd[1],data, strlen(data))) != -1)
		{
			printf("Parent wrote %d bytes: '%s'\n", real_write, data);
		}
		//关闭父进程写描述符
		close(pipe_fd[1]);

		//收集子进程退出信息
		waitpid(pid, NULL, 0);//阻塞等待,非阻塞等待的参数是:WNOHANG
		exit(0);
	}
}

你可能感兴趣的:(进程的管道通信pipe(int)函数的运用)