有名管道全双工通信

clienta.c

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <semaphore.h>
// 定义信息量全局变量

sem_t sem_send;
sem_t sem_get;
// 开两个线程,分别进行接收和发送:
void *send(void *argv)
{
    char *buffer_send = (char *)argv;
    // 定义fifo(管道)路径
    char *FIFO_PATH = "/home/hqyj/Myfifo/client_a";
    // 打开文件流,此端为发送端
    int fd = open(FIFO_PATH, O_WRONLY);
    if (-1 == fd)
    {
        perror("open");
        pthread_exit(NULL);
    }
    puts("myfifoA open success!");
    while (1)
    {
        fgets(buffer_send, 256, stdin);
        buffer_send[strlen(buffer_send) - 1] = '\0';

        write(fd, buffer_send, strlen(buffer_send));
    }
    close(fd);
}
void *get(void *argv)
{
    char *buffer_get = (char *)argv;
    // 定义fifo(管道)路径
    char *FIFO_PATH = "/home/hqyj/Myfifo/client_b";
    // 打开文件流,此端为收端
    int fd = open(FIFO_PATH, O_RDONLY);
    if (-1 == fd)
    {
        perror("open");
        pthread_exit(NULL);
    }
    puts("myfifoB open success!");
    while (1)
    {
        memset(buffer_get, 0, 256);
        read(fd, buffer_get, 256);

        printf("RECV:%s\n", buffer_get);
    }
    close(fd);
}
int main()
{
    // 定义缓冲区
    char buffer_send[256];
    char buffer_get[256];

    pthread_t tid_send, tid_get;
    // 创建线程
    if (0 != pthread_create(&tid_send, NULL, send, buffer_send))
    {
        perror("tid creat");
    }
    if (0 != pthread_create(&tid_get, NULL, get, buffer_get))
    {
        perror("tid creat");
    }
    // 同步进程线程
    if (0 != pthread_join(tid_send, NULL))
    {
        perror("join");
    }

    if (0 != pthread_join(tid_get, NULL))
    {
        perror("join");
    }

    return 0;
}

client.b

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <semaphore.h>
#include <pthread.h>
sem_t sem_send;
sem_t sem_get;
// 开两个线程,分别进行接收和发送:
void *send(void *argv)
{
    char *buffer_send = (char *)argv;
    // 定义fifo(管道)路径
    char *FIFO_PATH = "/home/hqyj/Myfifo/client_b";
    // 打开文件流,此端为发送端
    int fd = open(FIFO_PATH, O_WRONLY);
    if (-1 == fd)
    {
        perror("open");
        pthread_exit(NULL);
    }
    puts("myfifoA open success!");
    while (1)
    {
        fgets(buffer_send, 256, stdin);

        buffer_send[strlen(buffer_send) - 1] = '\0';

        write(fd, buffer_send, strlen(buffer_send));
    }
    close(fd);
}
void *get(void *argv)
{
    char *buffer_get = (char *)argv;
    // 定义fifo(管道)路径
    char *FIFO_PATH = "/home/hqyj/Myfifo/client_a";
    // 打开文件流,此端为收端
    int fd = open(FIFO_PATH, O_RDONLY);
    if (-1 == fd)
    {
        perror("open");
        pthread_exit(NULL);
    }
    puts("myfifoB open success!");
    while (1)
    {
        // 定义缓冲区
        memset(buffer_get, 0, 256);
        read(fd, buffer_get, 256);
        printf("RECV:%s\n", buffer_get);
    }
    close(fd);
}

int main()
{ // 定义缓冲区
    char buffer_send[256];
    char buffer_get[256];

    pthread_t tid_send, tid_get;
    // 创建线程
    if (0 != pthread_create(&tid_send, NULL, send, buffer_send))
    {
        perror("tid creat");
    }
    if (0 != pthread_create(&tid_get, NULL, get, buffer_get))
    {
        perror("tid creat");
    }
    // 同步进程线程
    if (0 != pthread_join(tid_send, NULL))
    {
        perror("join");
    }

    if (0 != pthread_join(tid_get, NULL))
    {
        perror("join");
    }

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值