2024.8.5 作业

使用有名管道实现,一个进程用于给另一个进程发消息,另一个进程收到消息后,展示到终端上,并且将消息保存到文件上一份

代码:

/*******************************************/

文件名:create.c

/*******************************************/

#include <myhead.h>
int main(int argc, char const *argv[])
{
    //创建有名管道文件
    if (mkfifo("./linux", 0664) == -1)
    {
        perror("mkfifo error");
        return -1;
    }
    getchar();
    system("rm linux");
    return 0;
}

/*******************************************/

文件名:send.c

/*******************************************/

#include <myhead.h>
int main(int argc, char const *argv[])
{
    //以写的形式打开管道文件
    int wfd = open("./linux", O_WRONLY);
    if (wfd == -1)
    {
        perror("open error");
        return -1;
    }
    printf("管道文件已经打开\n");
    //发送数据
    char wbuf[128] = "";
    while (1)
    {
        printf("请输入>>>>");
        fgets(wbuf, sizeof(wbuf), stdin);
        wbuf[strlen(wbuf) - 1] = 0;
        //将数据发送给到管道中
        write(wfd, wbuf, strlen(wbuf));
        //判断数据
        if (strcmp(wbuf, "quit") == 0)
        {
            break;
        }
    }
    //关闭文件描述符
    close(wfd);
    return 0;
}

/*******************************************/

文件名:get.c

/*******************************************/

#include <myhead.h>
int main(int argc, char const *argv[])
{
    //以读的形式打开文件
    int rfd = open("./linux", O_RDONLY);
    if (rfd == -1)
    {
        perror("open error");
        return -1;
    }
    int cfd = open("./h.txt", O_WRONLY);
    if (cfd == -1)
    {
        perror("open error");
        return -1;
    }
    printf("管道文件读端打开\n");
    //定义接受容器
    char rbuf[128] = "";
    while (1)
    {
        bzero(rbuf, sizeof(rbuf));
        //读取数据
        read(rfd, rbuf, sizeof(rbuf));
        if (strcmp(rbuf, "quit") == 0)
        {
            break;
        }
        printf("收到消息为:%s\n", rbuf);
        write(cfd,rbuf,strlen(rbuf));
    }
    //关闭文件描述符
    close(rfd);
    close(cfd);
    return 0;
}

结果:

使用有名管道实现两个进程间相互通信

代码:

/*******************************************/

文件名:create1.c

/*******************************************/

#include <myhead.h>
int main(int argc, char const *argv[])
{
    //创建有名管道文件
    if (mkfifo("./linux_first", 0664) == -1)
    {
        perror("mkfifo error");
        return -1;
    }
    getchar();
    system("rm linux_first");
    return 0;
}

/*******************************************/

文件名:create2.c

/*******************************************/

#include <myhead.h>
int main(int argc, char const *argv[])
{
    //创建第二个有名管道文件
    if (mkfifo("./linux_next", 0664) == -1)
    {
        perror("mkfifo error");
        return -1;
    }
    getchar();
    system("rm linux_next");
    return 0;
}

/*******************************************/

文件名:one.c

/*******************************************/

#include <myhead.h>
sem_t sem;
void *task1(void *arg)
{
    //只读的形式打开管道文件2
    int rfdo = open("./linux_next", O_RDONLY);
    if (rfdo == -1)
    {
        perror("open error");
        return NULL;
    }
    //定义接收容器
    char rbufo[128] = "";
    while (1)
    {
        bzero(rbufo, sizeof(rbufo));
        //读取数据
        read(rfdo, rbufo, sizeof(rbufo));
        if (strcmp(rbufo, "quit") == 0)
        {
            sem_post(&sem);
            break;
        }
        printf("对方的消息>>>%s\n", rbufo);
        sem_post(&sem);
    }
    //关闭文件描述符
    close(rfdo);
    pthread_exit(NULL); //退出线程
}
void *task2(void *arg)
{
    //只写的形式打开管道文件1
    int wfdo = open("./linux_first", O_WRONLY);
    if (wfdo == -1)
    {
        perror("open error");
        return NULL;
    }
    //发送数据
    char wbufo[128] = "";
    while (1)
    {
        sem_wait(&sem);
        printf("对话>>>");
        fgets(wbufo, sizeof(wbufo), stdin);
        wbufo[strlen(wbufo) - 1] = 0;
        write(wfdo, wbufo, strlen(wbufo));
        if (strcmp(wbufo, "quit") == 0)
        {
            break;
        }
    }
    //关闭文件描述符
    close(wfdo);
}
int main(int argc, char const *argv[])
{
    //初始化无名信号量
    sem_init(&sem, 0, 1);
    //定义变量存储线程号
    pthread_t tido = -1;
    if (pthread_create(&tido, NULL, task1, NULL) != 0)
    {
        printf("pthread_create error\n");
        return -1;
    }
    //定义变量存储线程号
    pthread_t tido2 = -1;
    if (pthread_create(&tido2, NULL, task2, NULL) != 0)
    {
        printf("pthread_create error\n");
        return -1;
    }
    //阻塞回收线程的资源
    pthread_join(tido, NULL);
    pthread_join(tido2, NULL);
    //销毁无名信号量
    sem_destroy(&sem);
    return 0;
}

/*******************************************/

文件名:another.c

/*******************************************/

#include <myhead.h>
sem_t sem;
void *task1(void *arg)
{
    //只写的形式打开管道文件2
    int wfda = open("./linux_next", O_WRONLY);
    if (wfda == -1)
    {
        perror("open error");
        return NULL;
    }
    //发送数据
    char wbufa[128] = "";
    while (1)
    {
        sem_wait(&sem);
        printf("对话>>>");
        fgets(wbufa, sizeof(wbufa), stdin);
        wbufa[strlen(wbufa) - 1] = 0;
        write(wfda, wbufa, strlen(wbufa));
        if (strcmp(wbufa, "quit") == 0)
        {
            break;
        }
    }
    //关闭文件描述符
    close(wfda);
    pthread_exit(NULL); //退出线程
}
void *task2(void *arg)
{
    //只读的形式打开管道文件1
    int rfda = open("./linux_first", O_RDONLY);
    if (rfda == -1)
    {
        perror("open error");
        return NULL;
    }
    //定义接收容器
    char rbufa[128] = "";
    while (1)
    {
        bzero(rbufa, sizeof(rbufa));
        //读取数据
        read(rfda, rbufa, sizeof(rbufa));
        if (strcmp(rbufa, "quit") == 0)
        {
            sem_post(&sem);
            break;
        }
        printf("对方的消息>>>%s\n", rbufa);
        sem_post(&sem);
    }
    //关闭文件描述符
    close(rfda);
    pthread_exit(NULL); //退出线程
}
int main(int argc, char const *argv[])
{
    //初始化无名信号量
    sem_init(&sem, 0, 0);
    //定义变量存储线程号
    pthread_t tida = -1;
    if (pthread_create(&tida, NULL, task1, NULL) != 0)
    {
        printf("pthread_create error\n");
        return -1;
    }
    //定义变量存储线程号
    pthread_t tida2 = -1;
    if (pthread_create(&tida2, NULL, task2, NULL) != 0)
    {
        printf("pthread_create error\n");
        return -1;
    }
    pthread_join(tida, NULL);
    pthread_join(tida2, NULL);
    //销毁无名信号量
    sem_destroy(&sem);
    return 0;
}

结果:

思维导图:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值