C++多进程利用管道进行实时通信

声明:本博文用于学习总结及工作心


比较简单,这里就直接贴代码了,代码中有注释:


进程1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
//创建2个线程分别进行读写

void mkfifo1()
{
    mkfifo("fifo1", 0666);//创建有名管道 管道名称 八进制权限
    sleep(1);
    return;

}
//读fifo2
void *myread(void *arg)
{
	printf("thread begin\n");
	int *rec = (int *)malloc(sizeof(int));
		char buf[1024];
		memset(buf, 0, sizeof(buf));
		int fd = open("./fifo2", O_RDONLY);
		if(fd < 0)
		{
			printf("open fifo error is %s\n", strerror(errno));
			*rec = -1;
			pthread_exit((void *)rec);
			return NULL;
		}
		while(1)
		{
			if(read(fd, buf, sizeof(buf)) > 0)
			{
				if(strncmp(buf, "end", 3) == 0)
				{
					pthread_t thr = *((pthread_t *)arg);
					pthread_cancel(thr);//关闭写线程
					break;
				}
				printf("%s", buf);
				memset(buf, 0, sizeof(buf));
			}
			usleep(1000);
		}
		close(fd);
		*rec = 0;
		printf("thread end\n");
		pthread_exit((void *)rec);
		return NULL;
}
//写fifo1
void *mywrite(void *arg)
{

	int *rec = (int *)malloc(sizeof(int));
	char buf[1024];
	memset(buf, 0, sizeof(buf));
	int fd = open("./fifo1", O_WRONLY);
	if(fd < 0)
	{
		printf("open fifo error is %s\n", strerror(errno));
		*rec = -1;
		pthread_exit((void *)rec);
		return NULL;
	}
	while(1)
	{
		read(STDIN_FILENO, buf, sizeof(buf)-1);
		write(fd, buf, sizeof(buf));
		if(strncmp(buf, "end", 3) == 0)//输入end结束
		{
			pthread_t thr = *((pthread_t *)arg);
			int n =pthread_cancel(thr);//关闭读线程
			printf("cancel :%d\n", n);//返回0 取消线程成功
			break;
		}
		memset(buf, 0, sizeof(buf));
	}
	printf("write end\n");
	close(fd);
	*rec = 0;
//	pthread_exit((void *)rec);
	return NULL;
}
int main(int arg, char*args[])
{
	printf("%s start\n", args[0]);
	//创建fifo
	mkfifo1();

	pthread_t thrd1, thrd2;
	pthread_create(&thrd1, NULL, myread, &thrd2);
	pthread_create(&thrd2, NULL, mywrite, &thrd1);
	int *p1 = NULL;
	int *p2 = NULL;
	pthread_join(thrd1, (void **)&p1);//等待thrd1结束
	pthread_join(thrd2, (void **)&p2);//等待thrd2结束

	printf("%s end\n", args[0]);
	return EXIT_SUCCESS;
}

进程2
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <pthread.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
//创建2个线程分别进行读写

void createfifo2()
{
	mkfifo("fifo2", 0666);
	sleep(1);
	return;
}

//读fifo1
void *myread(void *arg)
{
	printf("thread begin\n");
	int *rec = (int *)malloc(sizeof(int));
	char buf[1024];
	memset(buf, 0, sizeof(buf));
	int fd = open("./fifo1", O_RDONLY);
	if(fd < 0)
	{
		printf("open fifo error is %s\n", strerror(errno));
		*rec = -1;
		pthread_exit((void *)rec);
		return NULL;
	}
		while(1)
		{
			int len = read(fd, buf, sizeof(buf));
			if( len> 0)
			{
				if(strncmp(buf, "end", 3) == 0)
				{
					pthread_t thr = *((pthread_t *)arg);
					pthread_cancel(thr);
					break;
				}
				printf("%s", buf);
				memset(buf, 0, sizeof(buf));
			}
			usleep(1000);
		}
		close(fd);
		*rec = 0;
		printf("thread end\n");
		pthread_exit((void *)rec);
		return NULL;
}
//写fifo2
void *mywrite(void *arg)
{

	int *rec = (int *)malloc(sizeof(int));
	char buf[1024];
	memset(buf, 0, sizeof(buf));
	int fd = open("./fifo2", O_WRONLY);
	if(fd < 0)
	{
		printf("open fifo error is %s\n", strerror(errno));
		*rec = -1;
		pthread_exit((void *)rec);
		return NULL;
	}
	while(1)
	{
		read(STDIN_FILENO, buf, sizeof(buf)-1);
		write(fd, buf, sizeof(buf));
		if(strncmp(buf, "end", 3) == 0)//输入end结束
		{
			pthread_t thr = *((pthread_t *)arg);
			pthread_cancel(thr);
			break;
		}
		memset(buf, 0, sizeof(buf));
	}
	close(fd);
	*rec = 0;
//	pthread_exit((void *)rec);
	return NULL;
}
int main(int arg, char*args[])
{
	printf("%s start\n", args[0]);
	createfifo2();
	pthread_t thrd1, thrd2;
	pthread_create(&thrd1, NULL, myread, &thrd2);
	pthread_create(&thrd2, NULL, mywrite, &thrd1);
	int *p1 = NULL;
	int *p2 = NULL;
	pthread_join(thrd1, (void **)&p1);//等待thrd1结束
	pthread_join(thrd2, (void **)&p2);//等待thrd2结束
	printf("%s end\n", args[0]);
	return EXIT_SUCCESS;
}

makefile文件
.SUFFIXES:.c .o

CC=gcc
SRCS1=thrfifo1.c
SRCS2=thrfifo2.c
OBJS1=$(SRCS1:.c=.o)
OBJS2=$(SRCS2:.c=.o)
EXEC1=thrfifo1
EXEC2=thrfifo2
start: $(OBJS1) $(OBJS2)
	$(CC) -o $(EXEC1) $(OBJS1) -lpthread
	$(CC) -o $(EXEC2) $(OBJS2) -lpthread
	@echo '-----------OK-----------'
.c.o:
	$(CC) -Wall -g -o $@ -c $<
clean:
	rm -rf $(OBJS1)
	rm -rf $(OBJS2)




  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值