管道(一)

一、什么是管道

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

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

(3)本质:内核缓冲区

二、管道限制

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

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

三、匿名管道pipe

(1)包含头文件<unistd.h>

功能:创建一无名管道

原型:int pipe(int fd[2]);

参数:fd:文件描述符数组,其中fd[0]表示读端, fd[1]表示写端

          返回值:成功返回0,失败返回错误代码

(2)创建管道后示意图

四、示例

示例1:

子进程向父进程输入hello字符串

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

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#include <sys/time.h>


#define ERR_EXIT(m) \
	do \
	{ \
		perror(m); \
		exit(EXIT_FAILURE); \
	} while(0)


int main(int argc, char *argv[])
{
	int pipefd[2];
	if (pipe(pipefd) == -1)
		ERR_EXIT("pipe error");

	pid_t pid;
	pid = fork();
	if (pid == -1)
		ERR_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, 10);
	printf("buf=%s\n", buf);
	
	return 0;

}

 

示例2:

相当于执行 ls | wc -w 命令

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

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#include <sys/time.h>


#define ERR_EXIT(m) \
	do \
	{ \
		perror(m); \
		exit(EXIT_FAILURE); \
	} while(0)


int main(int argc, char *argv[])
{
	int pipefd[2];
	if (pipe(pipefd) == -1)
		ERR_EXIT("pipe error");

	pid_t pid;
	pid = fork();
	if (pid == -1)
		ERR_EXIT("fork error");

	if (pid == 0)
	{
        // 将管道写入端重定向的 STDOUT_FILENO
		dup2(pipefd[1], STDOUT_FILENO);
        // 关闭pipefd[1],pipefd[0]
		close(pipefd[1]);
		close(pipefd[0]);
		execlp("ls", "ls", NULL);
		fprintf(stderr, "error execute ls\n");
		exit(EXIT_FAILURE);
	}
    // 将pipefd[0]读重定向到STDIN_FILENO
	dup2(pipefd[0], STDIN_FILENO);
    // 关闭文件描述符
	close(pipefd[0]);
	close(pipefd[1]);
	execlp("wc", "wc", "-w", NULL);
	fprintf(stderr, "error execute wc\n");
	exit(EXIT_FAILURE);

}

示例3:

close(0);关闭0文件描述符,标准输入,open("Makefile", O_RDONLY); 新打开的文件占用0文件描述符

close(1); 关闭1文件描述符,标准输出,open("Makefile2", O_WRONLY | O_CREAT | O_TRUNC, 0644);新打开的文件占用1文件描述符

cat 命令将0文件描述符的内容,输出到1文件描述符中

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

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#include <sys/time.h>


#define ERR_EXIT(m) \
	do \
	{ \
		perror(m); \
		exit(EXIT_FAILURE); \
	} while(0)


int main(int argc, char *argv[])
{
	close(0);
	open("Makefile", O_RDONLY);
	close(1);
	open("Makefile2", O_WRONLY | O_CREAT | O_TRUNC, 0644);

	execlp("cat", "cat", NULL);

	return 0;
	
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值