linux 练习七 线程键用有名管道FIFO通信

时间:2021-07-19 15:13:25
题目:利用多线程与有名管道技术,实现两个进程之间发送即时消息,实现聊天功能

思路:关键在于建立两个有名管道,利用多线程技术,进程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$