您的位置:首页 > 编程语言 > C语言/C++

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

2016-12-02 20:56 441 查看
声明:本博文用于学习总结及工作心

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

进程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)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C C++