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

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

思路:关键在于建立两个有名管道,利用多线程技术,进程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
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值