一个LINUX下管道通信的例子

这篇博客详细介绍了在Linux环境下如何实现管道通信,特别关注客户端的实现细节,探讨了遇到的问题及其解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

client端------现在有问题...........

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

#define FIFO_HANDLE_NAME	"/tmp/fifo_handle "
#define FIFO_CLIENT_NAME	"/tmp/fifo_client_%d "

struct ps_fifo_struct{
	pid_t pid;
	char str[64];
};

int main()
{
	int fifo_handle, fifo_client;		//fifo_handle is use as write_pipe
	struct ps_fifo_struct ps_fifo;
	char client_fifo_name[64];

	fifo_handle = open(FIFO_HANDLE_NAME, O_WRONLY);		//判断管道文件是否已经存在
	if (fifo_handle == -1)							
	{
		fprintf(stderr, "Open handle fifo failed\n");
		exit(EXIT_FAILURE);	
	}
	
	ps_fifo.pid = getpid();
	memset(client_fifo_name, 0, sizeof(client_fifo_name));
	sprintf(client_fifo_name, FIFO_CLIENT_NAME, ps_fifo.pid);
	if (access(client_fifo_name, F_OK) == -1)
	{
		if (mkfifo(client_fifo_name, 0777) != 0)		//创建读管道
		{
			fprintf(stderr, "Could not create fifo %s\n", client_fifo_name);
			exit(EXIT_FAILURE);
		}
	}

	sprintf(ps_fifo.str, "hi, I'm %d.", ps_fifo.pid);
	printf("%d sent: \'%s\'.\n", ps_fifo.pid, ps_fifo.str);
	write(fifo_handle, &ps_fifo, sizeof(ps_fifo));			//Write to write_pipe:fifo_handle,
	
	close(fifo_handle);

	fifo_client = open(client_fifo_name, O_RDONLY);			//打开读管道
	if (fifo_client != -1)
	{
		if (read(fifo_client, &ps_fifo, sizeof(ps_fifo)) > 0)	//从读管道中读取消息
			printf("received from %d: %s\n", ps_fifo.pid, ps_fifo.str);
		close(fifo_client);
	}
	
//	close(fifo_handle);
	unlink(client_fifo_name);	
	exit(EXIT_SUCCESS);
}










server.c

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

#define FIFO_HANDLE_NAME	"/tmp/fifo_handle "
#define FIFO_CLIENT_NAME	"/tmp/fifo_client_%d"

struct ps_fifo_struct{
	pid_t pid;
	char str[64];
};

int main()
{
	int fifo_handle, fifo_client;		//fifo_handle为要读的管道
	struct ps_fifo_struct ps_fifo;
	int read_len;
	char client_fifo_name[64];
	char answer_str[64];
	
	if (access(FIFO_HANDLE_NAME, F_OK) == -1)
	{
		if (mkfifo(FIFO_HANDLE_NAME, 0777) != 0)
		{
			fprintf(stderr, "Could not create fifo %s\n", FIFO_HANDLE_NAME);
			exit(EXIT_FAILURE);
		}
	}

	fifo_handle = open(FIFO_HANDLE_NAME, O_RDONLY);
	if (fifo_handle = -1)
	{
		fprintf(stderr, "Handle fifo failure\n");
		exit(EXIT_FAILURE);
	}

	do {
		read_len = read(fifo_handle, &ps_fifo, sizeof(ps_fifo));		//从读管道中读取数据
		memset(answer_str, 0, 64);
		if (read_len > 0)
		{
			sprintf(answer_str, "hi, %d, I have received the string: '%s'.", ps_fifo.pid, ps_fifo.str);
			printf("received from %d:%s\n", ps_fifo.pid, ps_fifo.str);
			memset(client_fifo_name, 0, 64);
			sprintf(client_fifo_name, FIFO_CLIENT_NAME, ps_fifo.pid);
			fifo_client = open(client_fifo_name, O_WRONLY);				//打开client要读取的文件,即向其写入
			ps_fifo.pid = getpid();
			strcpy(ps_fifo.str, answer_str);
			//sprintf(ps_fifo.str, answer_str);
			if (fifo_client != -1)
			{
				write(fifo_client, &ps_fifo, sizeof(ps_fifo));	
				close(fifo_client);
			}			
		}
	} while(read_len > 0);

	close(fifo_handle);
	unlink(FIFO_HANDLE_NAME);
	exit(EXIT_SUCCESS);
}






好的,关于您所提到的问题,我能够给您一些基本的思路和实现方法。管道通信Linux系统中一种非常常见的进程间通信方式,可以实现不同进程之间数据的传输和共享。下面是一份基于Linux C语言的管道通信例程,您可以参考一下: 1. 父进程创建一个管道,通过fork()函数创建一个子进程来读取管道中的数据。 ``` #include <stdio.h> #include <stdlib.h> #include <unistd.h> int main(){ int fd[2]; pid_t pid; char buffer[20]; //创建管道 if(pipe(fd)<0){ printf("Create pipe error!\n"); exit(1); } //创建子进程 pid = fork(); if(pid < 0){ printf("Create process error!\n"); exit(1); }else if(pid == 0){ //子进程读取管道中的数据 close(fd[1]); int n = read(fd[0], buffer, sizeof(buffer)); printf("Child process received: %s\n", buffer); close(fd[0]); }else{ //父进程向管道中写入数据 close(fd[0]); char* data = "Hello, child process!"; write(fd[1], data, sizeof(data)); printf("Parent process sent: %s\n", data); close(fd[1]); } return 0; } ``` 2. 父进程向管道中写入数据,子进程进行读取并输出。 以上面的代码为例,首先父进程通过pipe()函数创建了一个管道fd,接着通过fork()函数创建了一个子进程,并通过pid变量来判断当前进程是否为父进程或子进程。在父进程中,我们先关闭了管道的读端,然后通过write()函数向管道中写入了数据"data",并输出了发送成功的信息。在子进程中,我们先关闭了管道的写端,然后通过read()函数从管道中读取数据到buffer缓冲区中,并输出读取的结果。 这就是一个简单的基于Linux C语言的管道通信例程实现方法。当然,具体实现方法还需要根据实际情况进行调整,但是我们通过这个例子可以清晰地了解到管道通信的基础原理和实现方法,希望能够帮到您。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值