linux 练习七 线程键用有名管道FIFO通信
来源:互联网 发布:优盘数据恢复 编辑:程序博客网 时间:2024/05/16 09:48
题目:利用多线程与有名管道技术,实现两个进程之间发送即时消息,实现聊天功能
思路:关键在于建立两个有名管道,利用多线程技术,进程A中线程1向管道A写数据,进程B中线程2从管道A读数据,进程A线程2从管道B中读数据,进程B中线程1往管道B中写数据
//线程和FIFO练习
//pthreadtest.c#include <stdio.h>#include <pthread.h>#include <stdlib.h>#include <unistd.h>#include <sys/types.h>#include <fcntl.h>#include <sys/stat.h>#include <errno.h>#include <string.h>int global = 2;int fd;#define FIFO_NAME "/home/book/watchman/fifotest"void* pthread1_handler(void* temp){ int fd = open(FIFO_NAME,O_WRONLY|O_NONBLOCK,0);// int ret; if(fd < 0) { perror("pthread1 open"); printf("pthread1 open err\n"); pthread_exit((void*)-1); } // ret = getchar(); printf("pthread1 going write fifo\n"); write(fd,"test1",5); printf("pthread1 write fifo ok\n"); global++; getchar(); write(fd, "test",4); printf("i'm phread1,global=%d!\n",global); pthread_exit((void *)5); printf("i'm pthread1,printf twice\n");}void * pthread2_handler(void* temp){ char buf[10]; int ret; /* // int fd = open(FIFO_NAME,O_RDONLY,0); if((fd <0) &&(errno!=EEXIST)) { perror("pthread2 open"); printf("pthread2 open err\n"); pthread_exit((void *)-1); } */ printf("i'm read fifo\n"); ret = read(fd,buf,5); if(ret < 0) { printf("pthread2 read"); pthread_exit((void *)-1); } printf("%s\n",buf); printf("i'm read fifo2\n"); memset(buf,0,sizeof(buf)); ret = read(fd,buf,5); if(ret < 0) { printf("pthread2 read"); pthread_exit((void *)-1); } printf("%s\n",buf); global++; printf("i'm pthread2,global=%d\n",global);}int * pthread3_handler(void * parg){ printf("i'm pthread3\n");}int main(int argc,char * argv[]){ pthread_t pthread1; pthread_t pthread2; pthread_t pthread3; int ret; umask(0); ret = mkfifo(FIFO_NAME,0777); if((ret < 0)&&(errno != EEXIST)) { perror("mkfifo"); printf("mkfifo err\n"); exit(1); } fd = open(FIFO_NAME,O_RDONLY|O_NONBLOCK,0); if(fd < 0) { perror("main process open"); exit(1); } ret = pthread_create(&pthread1,NULL,pthread1_handler,NULL); if(ret < 0) { perror("pthread_create"); exit(1); } printf("wait for pthread1\n"); ret = 0; sleep(1); pthread_join(pthread1,(void **)&ret); printf("pthread1 ret=%d\n",ret); global++; printf("i'm in parent pthread global=%d\n",global); ret = pthread_create(&pthread2,NULL,pthread2_handler,NULL); if(ret < 0) { perror("pthread_create2"); exit(1); } printf("wait for pthread2\n"); if((ret = pthread_join(pthread2,NULL)) < 0) { printf("pthread_join pthread2 err\n"); } printf("wait for pthread2 over\n"); /* ret = pthread_create(&pthread3,NULL,pthread3_handler,NULL); if(ret < 0) { perror("pthread_create3"); exit(1); }*/}编译
book@ubuntu:~/watchman$ gcc -pthread -o pthreadtest.o pthreadtest.c book@ubuntu:~/watchman$
阅读全文
0 0
- linux 练习七 线程键用有名管道FIFO通信
- linux进程通信----FIFO(有名管道)
- linux 有名管道FIFO
- linux fifo 有名管道
- 【七】 进程间通信——(FIFO)有名管道
- Linux--进程间通信(管道及有名管道FIFO)
- Linux--进程间通信(管道及有名管道FIFO)
- Linux--进程间通信(管道及有名管道FIFO)
- [IPC]通信有名管道FIFO
- 【IPC通信】有名管道FIFO
- 进程通信-有名管道FIFO
- linux进程通信--有名管道(FIFO)(含实例代码)
- linux进程间通信-有名管道(FIFO)
- linux进程间通信-有名管道(FIFO)
- Linux进程间通信二--有名管道FIFO
- linux编程---进程间通信---FIFO---有名管道
- Linux进程间通信——有名管道 FIFO 详解
- linux 有名管道(FIFO)
- 再谈CGO!
- samba 案例
- 2017-12-7继续解决问题
- libusb
- 我最常用的Intellij IDEA快捷键
- linux 练习七 线程键用有名管道FIFO通信
- ajax页面传参 json数据渲染页面
- DIY仪表24位ADC,锂电池供电可充电,变态级精度、线性度和...
- main函数里的参数
- Bulls and Cows:比较两数组位置与元素完全相同、仅元素相同的的情况
- 全球最杰出的14位程序员
- 最好用的 IntelliJ 插件 Top 10
- 怎么找到文件或目录损坏且无法读取F盘的文件
- android 文件目录