有名管道,创建两个发送端父进程写入管道1和管道2,子进程读取管道2和管道1.

#include<stdio.h>
#include<string.h>
#include<myhead.h>
int main(int argc, const char *argv[])
{
	pid_t pid;
	pid=fork();
	if(pid>0)
	{
		int send;
		send = open("./my_fifo1",O_WRONLY);
		if(send==-1)
		{
			perror("open send");
			return -1;
		}
		char buff[1024];
		char arr[10]="quit\n";
		while(1)
		{
			memset(buff,'\0',sizeof(buff));
			int len=read(0,buff,sizeof(buff));
			//write(send,buff,len);
			if(strcmp(buff,arr)==0)
			{
			
				break;
			
			}
			//sleep(1);
			write(send,buff,len);
		
		}
		close(send);
	
	}
	else if(pid==0)
	{
		int rev;
		rev=open("./my_fifo2",O_RDONLY);
		if(rev==-1)
		{
			perror("open rev");
			return -1;
		}
		char buff1[1024];
		char arr[10]="quit\n";
		while(1)
		{
			memset(buff1,'\0',sizeof(buff1));
			int len1=read(rev,buff1,sizeof(buff1));
			//write(1,buff1,len1);
			if(strcmp(buff1,arr)==0)
			{
				break;
			
			}
			//sleep(1);
			write(1,buff1,len1);
		}
		close(rev);
	}
	else
	{
		perror("fork");
		return -1;
	
	}
	return 0;
}
#include<stdio.h>
#include<string.h>
#include<myhead.h>
int main(int argc, const char *argv[])
{
	pid_t pid;
	pid=fork();
	if(pid>0)
	{
		int send;
		send = open("./my_fifo2",O_WRONLY);
		if(send==-1)
		{
			perror("open send");
			return -1;
		}
		char buff[1024];
		char brr[10]="quit\n";

		while(1)
		{
			memset(buff,'\0',sizeof(buff));
			int len=read(0,buff,sizeof(buff));
			//write(send,buff,len);
			if(strcmp(buff,brr)==0)
			{
			
				break;
			}
			//sleep(1);
			write(send,buff,len);
		}
		close(send);
	
	}
	else if(pid==0)
	{
		int rev;
		rev=open("./my_fifo1",O_RDONLY);
		if(rev==-1)
		{
			perror("open rev");
			return -1;
		}
		char buff1[1024];
		char brr[10]="quit\n";
		while(1)
		{
			memset(buff1,'\0',sizeof(buff1));
			int len1=read(rev,buff1,sizeof(buff1));
		//	write(1,buff1,len1);
			if(strcmp(buff1,brr)==0)
			{
				break;
			
			}
			//sleep(1);
			write(1,buff1,len1);
		}
		close(rev);
	}
	else
	{
		perror("fork");
		return -1;
	
	}
	return 0;
}

#include<stdio.h>
#include<string.h>
#include<myhead.h>
int main(int argc, const char *argv[])
{
	if(mkfifo("./my_fifo1",0664)==-1)
	{
		perror("mkfifo");
		return -1;
	
	}
	return 0;
}
#include<stdio.h>
#include<string.h>
#include<myhead.h>
int main(int argc, const char *argv[])
{
	if(mkfifo("./my_fifo2",0664)==-1)
	{
		perror("mkfifo");
		return -1;
	
	}
	return 0;
}

在C语言中,`mkfifo()`函数用于在指定目录下创建一个命名管道(FIFO),也称为无名管道的一种特殊形式。这种管道是在文件系统中创建的一个特殊类型的文件,通常用于具有单个写者和多个读者的进程间通信。 为了基于这个有名管道实现两个进程之间的通信,你可以按照以下步骤操作: 1. **创建管道**: 使用`mkfifo()`函数创建一个 FIFO 文件,比如 `mypipe`. 例如: ```c #include <unistd.h> #include <fcntl.h> int main(int argc, char *argv[]) { if (argc != 2) { printf("Usage: %s <path_to_fifo>\n", argv[0]); return -1; } const char *pipe_path = argv[1]; int result = mkfifo(pipe_path, S_IRUSR | S_IWUSR); // 设置权限,只允许所有者读写 if (result == -1) { perror("Failed to create FIFO"); return -1; } printf("Named pipe created at %s\n", pipe_path); return 0; } ``` 运行时传入你想创建管道的路径,如 `./create_pipe mypipe`. 2. **打开并监听管道**: 创建进程,它将打开管道进行读取写入。例如,父进程可能读取数据: ```c parent_process.c ``` ```c #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int read_from_fifo(const char *pipe_path) { int fd = open(pipe_path, O_RDONLY); // 以只读模式打开 if (fd == -1) { perror("Failed to open pipe"); return -1; } char buffer[100]; ssize_t bytes_read = read(fd, buffer, sizeof(buffer)); if (bytes_read == -1) { perror("Failed to read from pipe"); } else { printf("Received message: %.*s\n", bytes_read, buffer); } close(fd); return 0; } int main() { ... read_from_fifo("mypipe"); } ``` 3. **创建子进程**: 子进程负责向管道写入数据: ```c child_process.c ``` ```c #include <stdio.h> #include <string.h> void write_to_fifo(const char *pipe_path, const char *message) { int fd = open(pipe_path, O_WRONLY); // 以只写模式打开 if (fd == -1) { perror("Failed to open pipe for writing"); return; } size_t message_length = strlen(message); if (write(fd, message, message_length) != message_length) { perror("Failed to write to pipe"); } close(fd); } int main() { ... char msg[] = "Hello from child process!"; write_to_fifo("mypipe", msg); } ``` 运行这两个程序,先确保父进程运行,然后启动子进程。这样,子进程写入的数据就会被父进程读取,实现了进程间的通信。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值