Pipe

无名管道:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

int read_pipe(int fd)
{
	int n;
	char buf[1024];
	
	while(1)
	{
		n = read(fd,buf,sizeof(buf) - 1);
		buf[n] = '\0';

		printf("Read %d bytes: %s.\n",n,buf);

		if(strncmp(buf,"quit",4) == 0)
			break;
	}

	return 0;
}

int write_pipe(int fd)
{
	
	/*
	char buf[] = "hello word";
	sizeof(buf) : 11
	strlen(buf) : 10
	*/
	char buf[1024];

	while(1)
	{
		printf(">");
		fgets(buf,sizeof(buf),stdin);
		buf[strlen(buf) - 1] = '\0';

		write(fd,buf,strlen(buf));

	}
}

int main(int argc, const char *argv[])
{
	pid_t pid;
	int fd[2];

	if(pipe(fd) < 0)
	{
		perror("Fail to pipe");
		exit(EXIT_FAILURE);
	}

	if((pid = fork()) < 0)
	{
		perror("fail to fork");
		exit(EXIT_FAILURE);
	}

	if(pid == 0)
	{
		close(fd[1]);
		read_pipe(fd[0]);
	}

	if(pid > 0)
	{
		close(fd[0]);
		write_pipe(fd[1]);
	}
	
	exit(EXIT_SUCCESS);
}

pipe 读写操作

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>

int read_pipe(int r_fd,const char *filename)
{
	int n;
	int file_fd;
	char buf[1024];

	if((file_fd = open(filename,O_WRONLY | O_CREAT | O_TRUNC,0666)) < 0)
	{
		fprintf(stderr,"Fail to open %s : %s.\n",filename,strerror(errno)); 
		exit(EXIT_FAILURE);
	}
	
	while(1)
	{
		n = read(r_fd,buf,sizeof(buf));
		if(n == 0)
			break;
		//错误
		//write(filename,buf,strlen(buf));
		write(file_fd,buf,n);
	}

	close(file_fd);
	close(r_fd);

	return 0;
}

int write_pipe(int w_fd,const char *filename)
{
	int n;
	int file_fd;
	char buf[1024];

	if((file_fd = open(filename,O_RDONLY)) < 0)
	{
		fprintf(stderr,"Fail to open %s : %s.\n",filename,strerror(errno)); 
		exit(EXIT_FAILURE);
	}

	while(1)
	{
		n = read(file_fd,buf,sizeof(buf));
		if(n == 0)
			break;

		write(w_fd,buf,n);
	}

	close(w_fd);
	close(file_fd);

	return 0;
}

//./a.out src dest
int main(int argc, const char *argv[])
{
	pid_t pid;
	int pipe_fd[2];

	if(argc < 3)
	{
		fprintf(stderr,"Usage : %s src dest.\n",argv[0]);
		exit(EXIT_FAILURE);
	}

	if(pipe(pipe_fd) < 0)
	{
		perror("Fail to pipe");
		exit(EXIT_FAILURE);
	}

	if((pid = fork()) < 0)
	{
		perror("Fail to fork");
		exit(EXIT_FAILURE);
	}

	if(pid == 0)
	{
		close(pipe_fd[1]);
		read_pipe(pipe_fd[0],argv[2]);
	}

	if(pid > 0)
	{
		close(pipe_fd[0]);
		write_pipe(pipe_fd[1],argv[1]);
	}
	
	exit(EXIT_SUCCESS);
}

 

### 关于 Pipe 在操作系统和编程中的用法及实现 #### 定义与基本功能 Pipe 是一种用于进程间通信(IPC)的方法,在 Linux 和 Unix 类系统中广泛应用。它允许一个进程的数据输出被另一个进程作为输入来处理,形成所谓的管道通信[^1]。 #### 工作机制 当创建了一个 pipe 后,实际上是在内存中开辟了一块缓冲区用来暂存数据。向管道(共享文件)提供输入的发送进程(即写进程)以字符流形式将大量数据送入管道;而接收管道输出的接收进程(即读进程)则从管道中接收(读)这些数据。因为发送进程和接收进程都是利用同一个管道来进行消息传递,所以这种通信方式被称为管道通信[^3]。 #### 函数调用 `pipe()` 为了建立一条匿名管道,可以使用 C 库提供的 `pipe()` 系统调用。此函数会返回两个文件描述符——fd[0] 代表读端口,fd[1] 表示写端口。通常情况下,子进程中关闭不需要的那个方向上的描述符,只保留需要用到的那一方。 ```c #include <unistd.h> int pipe(int fd[2]); ``` 如果成功,则返回 0 并设置数组 `fd[]` 中的第一个成员为读取端点,第二个成员为写入端点;如果有错,则返回 -1,并设定 errno 变量指出具体原因。 #### 使用场景举例 假设有一个父进程想要启动多个子进程并让它们之间能够相互通信,那么就可以先由父进程调用一次 `pipe()` 来获取一对连接两端的新文件描述符,之后再通过 fork 创建新的子进程。这样做的好处在于父子之间的联系更加紧密,而且可以通过简单的 read/write 操作完成复杂的任务协调工作。 #### 注意事项 - 当任意一方关闭了对应的文件描述符后,另一方继续尝试操作该已关闭的方向将会触发 SIGPIPE 信号,默认行为是终止当前程序执行。 - 如果读者全部退出但仍有未读取的信息存在于 buffer 内部的话,后续任何试图往里面追加新内容的行为都会阻塞直到有足够的空间可用为止。 - 对于无名管道而言,只有具有共同祖先关系的一组进程才能访问到相同的实例对象。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

静思心远

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值