管道

什么是管道?

  管道是Unix中最古老的进程间通信的形式

   我们把从一个进程连接到另一个进程的一个数据流称为一个“管道”

管道限制:

1、管道式半双工的,数据只能向一个方向流动;需要双方通信时,需要建立起两个管道

2、只能用于具有共同祖先的进程(具有亲缘关系的进程)之间进行通信;通常,一个管道有一个进程创建,然后该进程调用fork,此后父子进程之间就可以应用该通道。


匿名管道pipe

包含头文件<unistd.h>

功能:创建一无名管道

原型是:

 包含头文件<unistd.h>
功能:创建一无名管道
原型
int pipe(int fd[2]);
参数
fd:文件描述符数组,其中fd[0]表示读端, fd[1]表示写端
返回值:成功返回0,失败返回错误代码


事例一:从子进程发送信息到父进程


/*
 * PipeDemo1.cpp
 *
 *  Created on: Aug 1, 2014
 *      Author: zfs
 */
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <dirent.h>
#include <signal.h>
/*
 #define ERROR_EXIT(m)(perror(m),exit(EXIT_FAILURE))
 */
#define ERROR_EXIT(m) \
	   do\
	   {      \
		   perror(m);\
           exit(EXIT_FAILURE);\
        }\
	   while(0)

int main(int argc, char* argv[]) {

	int pipefd[2];
	int ret = pipe(pipefd);
	if (ret == -1)
		ERROR_EXIT("pipe error");
	pid_t pid;
	pid = fork();
	if (pid == -1) {
		ERROR_EXIT("fork error");
	}
	if (pid == 0) {
		close(pipefd[0]);
        ::write(pipefd[1],"hello",5);
        close(pipefd[1]);
        exit(EXIT_SUCCESS);
	}
     close(pipefd[1]);
     char buf[10]={0};
     ::read(pipefd[0],buf,5);
     printf("buf:%s\n",buf);

	return 0;
}
输出:hello

事例二:实现 类似ls | wc -w的功能

/*
 * pipeDemo2.cpp
 *
 *  Created on: Aug 1, 2014
 *      Author: zfs
 */

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <dirent.h>
#include <signal.h>
/*
 #define ERROR_EXIT(m)(perror(m),exit(EXIT_FAILURE))
 */
#define ERROR_EXIT(m) \
	   do\
	   {      \
		   perror(m);\
           exit(EXIT_FAILURE);\
        }\
	   while(0)

int main(int argc, char* argv[]) {

	int pipefd[2];
	int ret = pipe(pipefd);
	if (ret == -1)
		ERROR_EXIT("pipe error");
	pid_t pid;
	pid = fork();
	if (pid == -1) {
		ERROR_EXIT("fork error");
	}
	if (pid == 0) { //子进程

		dup2(pipefd[1], STDOUT_FILENO); //文件描述副复制的用法
		close(pipefd[1]);
		close(pipefd[0]);
		::execlp("ls", "ls", NULL);
		fprintf(stderr, "error execute ls \n");
		exit(EXIT_FAILURE);
	}
	//
	dup2(pipefd[0], STDIN_FILENO);
	close(pipefd[0]);
	close(pipefd[1]);
	::execlp("wc", "wc", "-w", NULL);
	fprintf(stderr, "error execute ls \n");
	exit(EXIT_FAILURE);
	return 0;
}
事例三:实现文件copy的作用

/*
 * pipeDemo2.cpp
 *
 *  Created on: Jul 31, 2014
 *      Author: zfs
 */

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <dirent.h>
#include <signal.h>
/*
 #define ERROR_EXIT(m)(perror(m),exit(EXIT_FAILURE))
 */
#define ERROR_EXIT(m) \
	   do\
	   {      \
		   perror(m);\
           exit(EXIT_FAILURE);\
        }\
	   while(0)

int main(int argc, char* argv[]) {

	close(0);
	open("01.cpp", O_RDONLY);
	close(1);
	open("10.cpp", O_WRONLY | O_CREAT | O_TRUNC, 0644);
	execlp("cat", "cat", NULL);
	return 0;
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值