嵌入式开发C语言学习day28-华清作业8.5

思维导图

作业1:

pipe.c
// 使用有名管道实现 一个进程用于给另一个进程发消息
// 另一个进程收到消息后 展示到终端上 并且将消息保存到文件上一份
#include <myhead.h>

int main(int argc, char const *argv[])
{
    // 创建一个有名管道
    if (mkfifo("./linux", 0664) == -1)
    {
        perror("创建有名管道失败");
        return -1;
    }
    printf("创建有名管道成功\n");
    getchar();
    return 0;
}
send.c
// 写端
// 使用有名管道实现 一个进程用于给另一个进程发消息
// 另一个进程收到消息后 展示到终端上 并且将消息保存到文件上一份
#include <myhead.h>

int main(int argc, char const *argv[])
{
    int wfd = open("./linux", O_WRONLY);
    if (wfd == -1)
    {
        printf("管道文件写端打开失败");
        return -1;
    }
    printf("管道文件写端打开成功\n");
    char wbuf[128] = "";
    while (1)
    {
        printf("请输入>>>");
        fgets(wbuf, sizeof(wbuf), stdin);
        write(wfd, wbuf, strlen(wbuf));
        if (strcmp(wbuf, "quit") == 0)
        {
            break;
        }
    }
    close(wfd);
    return 0;
}
receive.c
// 读端
// 使用有名管道实现 一个进程用于给另一个进程发消息
// 另一个进程收到消息后 展示到终端上 并且将消息保存到文件上一份
#include <myhead.h>

int main(int argc, char const *argv[])
{
    int rfd = open("./linux", O_RDONLY);
    if (rfd == -1)
    {
        perror("管道文件读端打开失败");
    }
    printf("管道文件读端打开成功\n");
    int fd = open("./text.txt", O_WRONLY | O_APPEND | O_CREAT, 0664);
    if (fd == -1)
    {
        perror("打开写入文件失败\n");
        return -1;
    }
    char rbuf[128] = "";
    while (1)
    {
        bzero(rbuf, sizeof(rbuf));
        read(rfd, rbuf, sizeof(rbuf));
        if (strcmp(rbuf, "quit") == 0)
        {
            break;
        }
        printf("收到消息为:%s\n", rbuf);
        if (write(fd, rbuf, strlen(rbuf)) == 0)
        {
            break;
        }
    }
    close(rfd);
    return 0;
}

作业2

pipe1.c
//使用有名管道实现两个进程间的通信
//用锁来实现
#include <myhead.h>

int main(int argc, char const *argv[])
{
    //创建一个有名管道
    if (mkfifo ("./pipe1",0664) == -1)
    {
        perror("有名管道1创建失败\n");
        return -1;
    }
    printf("创建有名管1道成功\n");
    getchar();    
    return 0;
}
pipe2.c
//使用有名管道实现两个进程间的通信
//用锁来实现
#include <myhead.h>

int main(int argc, char const *argv[])
{
    //创建一个有名管道
    if (mkfifo ("./pipe2",0664) == -1)
    {
        perror("有名管道2创建失败\n");
        return -1;
    }
    printf("创建有名管2道成功\n");
    getchar();    
    return 0;
}
chat1.c
// 父进程的线程
// 子进程的线程

#include <myhead.h>
// 父进程的线程函数,向 pipe1 写入
void *chat1write(void *arg)
{
    char buf[1024] = "";
    int fd = open("./pipe1", O_WRONLY | O_APPEND);
    if (fd == -1)
    {
        perror("chat1 pipe1 open error");
        return NULL;
    }
    while (1)
    {
        printf("请输入>>>");
        fgets(buf, sizeof(buf), stdin);
        getchar();
        buf[strlen(buf) - 1] = 0;
        write(fd, buf, strlen(buf));
        if (strcmp(buf, "quit") == 0)
        {
            printf("write over\n");
            break;
        }
    }
    if (close(fd) == -1)
    {
        perror("chat1 pipe1 close error");
        return NULL;
    }
    pthread_exit(NULL);
}

// 子进程的线程函数,从 pipe2 读取
void *chat1read(void *arg)
{
    char buf[1024] = "";
    int fd = open("./pipe2", O_RDONLY);
    if (fd == -1)
    {
        perror("chat1 pipe2 open error");
        return NULL;
    }
    while (1)
    {
        ssize_t numRead = read(fd, buf, sizeof(buf));
        if (numRead <= 0)
        {
            perror("Read error or end of file");
            break;
        }
        buf[numRead] = '\0';
        printf("Read from pipe2: %s\n", buf);
    }
    if (close(fd) == -1)
    {
        perror("chat1 pipe2 close error");
        return NULL;
    }
    pthread_exit(NULL);
}

int main(int argc, char const *argv[])
{
    // 创建子进程
    pid_t pid = fork();
    // 创建父线程的子线程
    if (pid == -1)
    {
        perror("chat1 pid error");
        return -1;
    }

    // 父进程
    if (pid > 0)
    {
        pthread_t tid1;
        if (pthread_create(&tid1, NULL, chat1write, NULL) == -1)
        {
            printf("chat1 create tid1 error\n");
            return -1;
        }
        // 回收父进程的线程
        if (pthread_join(tid1, NULL) != 0)
        {
            printf("chat1 tid1 join error\n");
            return -1;
        }
    }

    // 子进程
    else if (pid == 0)
    {
        pthread_t tid2;
        if (pthread_create(&tid2, NULL, chat1read, NULL) == -1)
        {
            printf("chat1 create tid2 error\n");
            return -1;
        }
        // 回收子进程的线程
        if (pthread_join(tid2, NULL) != 0)
        {
            printf("chat1 tid2 join error\n");
            return -1;
        }
    }
    return 0;
}
chat2.c
#include <myhead.h>

// 父进程的读线程函数,从 pipe1 读取
void *chat2read(void *arg)
{
    char buf[1024] = "";
    int fd = open("./pipe1", O_RDONLY);
    if (fd == -1)
    {
        perror("chat2 pipe1 open error");
        return NULL;
    }
    while (1)
    {
        sleep(1);
        bzero(buf, sizeof(buf));
        ssize_t numRead = read(fd, buf, sizeof(buf));
        if (numRead <= 0)
        {
            perror("Read error or end of file");
            break;
        }
        buf[numRead] = '\0';
        if (strcmp(buf, "quit") == 0)
        {
            printf("read over\n");
            break;
        }
        printf("输出:%s", buf);
    }
    if (close(fd) == -1)
    {
        perror("chat2 pipe1 close error");
    }
    pthread_exit(NULL);
}

// 子进程的写线程函数,向 pipe2 写入
void *chat2write(void *arg)
{
    char buf[1024] = "";
    int fd = open("./pipe2", O_WRONLY | O_APPEND);
    if (fd == -1)
    {
        perror("chat2 pipe2 open error");
        return NULL;
    }
    while (1)
    {
        printf("请输入>>>");
        fgets(buf, sizeof(buf), stdin);
        getchar();
        buf[strlen(buf) - 1] = 0;
        write(fd, buf, strlen(buf));
        if (strcmp(buf, "quit") == 0)
        {
            printf("write over\n");
            break;
        }
    }
    if (close(fd) == -1)
    {
        perror("chat2 pipe2 close error");
    }
    pthread_exit(NULL);
}

int main(int argc, char const *argv[])
{
    pid_t pid = fork();

    if (pid == -1)
    {
        perror("chat2 pid error");
        return -1;
    }
    // 子进程
    if (pid == 0)
    {
        pthread_t tid2;
        if (pthread_create(&tid2, NULL, chat2write, NULL) == -1)
        {
            printf("chat2 create tid2 error\n");
            return -1;
        }
        // 回收子进程的线程
        if (pthread_join(tid2, NULL) != 0)
        {
            printf("chat2 tid2 join error\n");
            return -1;
        }
    }
    // 父进程
    else if (pid > 0)
    {
        pthread_t tid1;
        if (pthread_create(&tid1, NULL, chat2read, NULL) == -1)
        {
            printf("chat2 create tid1 error\n");
            return -1;
        }
        // 回收父进程的线程
        if (pthread_join(tid1, NULL) != 0)
        {
            printf("chat2 tid1 join error\n");
            return -1;
        }
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值