进程A和进程B随时通信

进程A代码

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <pthread.h>
#include <string.h>

//线程A负责读数据
void* pthreadA_read(void* argv){
    int fdA = *((int*)argv);
    char bufA[128] = "";
    int res;
    while(1){
        res = read(fdA, bufA, sizeof(bufA));
        if(-1 == res){
            perror("read");
            return NULL;
        }else if(0 == res){
            fprintf(stderr, "管道fifo1中所有写端均关闭 __%d__\n", __LINE__);
            return NULL;
        }

        if(!strcmp("quit", bufA)){
            pthread_exit(NULL);
        }

        //打印读取到的数据
        fprintf(stdout, "收到数据:%s\n", bufA);                                      
        memset(bufA, 0, sizeof(bufA));
    }
}

//线程B负责写数据
void* pthreadB_write(void* argv){
    int fdB = *((int*)argv);
    char bufB[128] = "";
    while(1){
        //从终端获取数据
        fgets(bufB, sizeof(bufB), stdin);
        bufB[strlen(bufB)-1] = '\0'; //将\n替换为\0字符
        if(-1 == write(fdB, bufB, sizeof(bufB))){
            perror("write");
            return NULL;
        }
        if(!strcmp("quit", bufB)){
            pthread_exit(NULL);
        }
        printf("发送数据成功\n");
        memset(bufB, 0, sizeof(bufB));
    }
}
int main(int argc, const char *argv[]){
    //创建有名管道fifo1
    if(-1 == mkfifo("./fifo1", 0777)){
        if(errno != 17){
            perror("mkfifo");
            return -1;
        }
    }

    //创建有名管道fifo2
    if(-1 == mkfifo("./fifo2", 0777)){
        if(errno != 17){
            perror("mkfifo");
            return -1;
        }
    }

    //以读的方式打开管道文件fifo1
    int fdA = open("./fifo1", O_RDONLY);
    if(-1 == fdA){
        perror("open");
        return -1;
    }

    //以写的方式打开管道文件fifo2
    int fdB = open("./fifo2", O_WRONLY);
    if(-1 == fdB){
        perror("open");
        return -1;
    }

    //创建两个线程
    pthread_t tidA, tidB;
    if(pthread_create(&tidA, NULL, pthreadA_read, (void*)&fdA) != 0){
        fprintf(stderr, "pthread_create failed __%d__\n", __LINE__);
        return -1;
    }

    if(pthread_create(&tidB, NULL, pthreadB_write, (void*)&fdB) != 0){
        fprintf(stderr, "pthread_create failed __%d__\n", __LINE__);
        return -1;
    }

    //主线程阻塞,监测子线程退出
    pthread_detach(tidA);
    pthread_join(tidB, NULL);
    close(tidA);
    close(tidB);

    return 0;
}
                                                                                     
                                                                                     

进程B代码

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <pthread.h>
#include <string.h>

//线程A负责读数据
void* pthreadA_read(void* argv){
    int fdA = *((int*)argv);
    char bufA[128] = "";                                                                                      
    int res;
    while(1){
        res = read(fdA, bufA, sizeof(bufA));
        if(-1 == res){
            perror("read");
            return NULL;
        }else if(0 == res){
            fprintf(stderr, "管道fifo1中所有写端均关闭 __%d__\n", __LINE__);
            return NULL;
        }

        if(!strcmp("quit", bufA)){
            pthread_exit(NULL);
        }

        //打印读取到的数据
        fprintf(stdout, "收到数据:%s\n", bufA);
        memset(bufA, 0, sizeof(bufA));
    }
}

//线程B负责写数据
void* pthreadB_write(void* argv){
    int fdB = *((int*)argv);
    char bufB[128] = "";
    //从终端获取数据
    while(1){
        fgets(bufB, sizeof(bufB), stdin);
        bufB[strlen(bufB)-1] = '\0'; //将\n替换为\0字符
        if(-1 == write(fdB, bufB, sizeof(bufB))){
            perror("write");
            return NULL;
        }
        if(!strcmp("quit", bufB)){
            pthread_exit(NULL);
        }
        printf("发送数据成功\n");
        memset(bufB, 0, sizeof(bufB));
    }
}

int main(int argc, const char *argv[]){
    //创建有名管道fifo1
    if(-1 == mkfifo("./fifo1", 0777)){
        if(errno != 17){
            perror("mkfifo");
            return -1;
        }
    }

    //创建有名管道fifo2
    if(-1 == mkfifo("./fifo2", 0777)){
        if(errno != 17){
            perror("mkfifo");
            return -1;
        }
    }

    //以写的方式打开管道文件fifo1
    int fdA = open("./fifo1", O_WRONLY);
    if(-1 == fdA){
        perror("open");
        return -1;
    }

    //以读的方式打开管道文件fifo2
    int fdB = open("./fifo2", O_RDONLY);
    if(-1 == fdB){
        perror("open");
        return -1;
    }

    //创建两个线程
    pthread_t tidA, tidB;
    if(pthread_create(&tidA, NULL, pthreadA_read, (void*)&fdB) != 0){
        fprintf(stderr, "pthread_create failed __%d__\n", __LINE__);
        return -1;
    }

    if(pthread_create(&tidB, NULL, pthreadB_write, (void*)&fdA) != 0){
        fprintf(stderr, "pthread_create failed __%d__\n", __LINE__);
        return -1;
    }

    //主线程阻塞,监测子线程退出
    pthread_detach(tidA);
    pthread_join(tidB, NULL);
    close(tidA);
    close(tidB);

    return 0;
}
                                                                                                              
                                                                                                              

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值