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;
}
比较简单,这里就直接贴代码了,代码中有注释:
进程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)
相关文章推荐
- 进程间通信---在父进程跟子进程之间利用管道进行通信。一个简单的例子
- 进程间利用管道进行通信:打印斐波那契数列
- 利用管道进行通信
- 利用管道进行通信
- 利用管道进行进程的通信示例
- Linux初学,利用管道文件进行通信,实现3个人之间的群聊
- 线程利用管道字节(字符)流进行通信
- 利用管道进行通信
- 在libevent中使用线程池(采用管道来进行线程的通信)
- 利用三层交换进行VLAN间的通信
- 使用命名管道通过网络在进程之间进行通信
- 利用amfext和actionscript进行通信
- 进程间通信 3 -- 使用管道进行通信
- [ZZ]使用命名管道通过网络在进程之间进行通信
- 使用命名管道通过网络在进程之间进行通信(C#)
- 客户端和应用服务器之间利用HttpClient和Servlet交互JSON对象进行通信
- 利用命名管道实现进程之间的通信 .........
- vb 利用管道使进程间互相通信
- [Silverlight学习笔记]关于利用WCF RIA Service进行通信并在客户端获取数据
- SPCAMLEditor使用系列(3)--利用SPCamlEditor实现 时间字段跟当前时间进行实时比较计算