首页 > 其他分享 >嵌入式开发C语言学习day28-华清作业8.5

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

时间:2024-08-05 20:57:27浏览次数:13  
标签:return day28 int 华清 fd printf 8.5 NULL buf

思维导图

作业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;
}

标签:return,day28,int,华清,fd,printf,8.5,NULL,buf
From: https://blog.csdn.net/weixin_64005144/article/details/140936769

相关文章

  • 8.5 模拟赛
    总结输在语文上了。t1签到题。t2神秘dp,问题主要在处理二次函数的限制上,考虑直接拆开或者差分后面的思路就容易了。t3语文题,要将集合的关系转移到图上,部分分给了非常多的正解启示。t4dp,卡特兰数,格路计数。题解road二分答案,转化为最少添加几个传送锚点,对于每两个点对......
  • 8.5日每日总结之双板升级下载
    之前搞BOOTLOAD双区升级时忘记记录了,现在补充上。keil软件使用时,配置h文件路径,./表示进入文件夹;最好是把所有文件放到一个新的文件夹里,以防复制工程时会打开上一次的文件,新复制文件最好重新编译一下。双板升级时,一款板子做主板,内存大的优先,另一块做副板,由主板和WIFI通信控制两块......
  • 【笔记】非传统题选讲 2024.8.5
    今天睡着了。发了只是为了完整性。[CF1672E]notepad.exe先二分得到总长度\(\suml_i+n-1\)记为\(w_1\),然后考虑其它行数\(h\),最优的情况只能是每一行都用换行顶替一个空格,此时面积为\(w_h\cdoth=w_1-h+1\),所以\(w_h=\left\lfloorw_1/h\right\rfloor\)为唯一可能更新答......
  • 8.5今日复习
    1:在堆区申请10个连续空间,手动输入10个数(递增),输入关键字key,采用折半查找方式查找关键字是否存在,存在给出位置,不存在,输出查找失败。注意:main函数在main.c输入函数,输出函数,查找函数,在find.c main函数代码:#include<myhead.h>#include"find.h"#defineMAX10intmain(int......
  • 学习笔记 韩顺平 零基础30天学会Java(2024.8.5)
    P460八大Wrapper类     黄色的父类是number,黑色的是自己独立的P461装箱和拆箱     手动装箱示例:                             intn1=100;                Intergerinterger=newInterger(n1);//......
  • 2024.8.5 test
    A你可以花费\(x^2\)的代价使\(A_i\)加上\(x\),\(x\ge0\),最后再加上代价为\(c\sum|A_i-A_{i-1}|\),问最小代价。\(n\le10^5\)。我们可以把序列分成若干“山峰”以及“山谷”,山峰是不会加的。考虑从山谷开始做,即每次取出最小值。设一开始处理\(A_i\),发现\(A_i\)最多是......
  • 8.5模考总结
    省流:坠机了,但没完全坠。\(T1\)水,直接枚举比较即可,赛事\(15min\)\(AC\),实际\(5min\),\(10min\)再打缺省源,最终得分\(100pts\)。\(T2\)模拟每一个括号,维护一个深度,当深度大于\(L\)或小于\(0\)时,累计答案即可,赛事\(50min\)\(AC\),最终得分\(100pts\)。\(T3\)一个......
  • 云原生周刊:Knative 1.15 版本发布|2024.8.5
    开源项目推荐helm-secretshelm-secrets是一个Helm插件,用于动态解密加密的Helm值文件。TofuControllerTofuController(以前称为WeaveTF-Controller)是Flux的一个控制器,用于以GitOps方式协调OpenTofu和Terraform资源。TracetestTracetest是一个使用OpenTelem......
  • 8.5第四周周一总结
    1dijkstra堆优化练习1)邮寄员寄信题目多次运用最短路#include<bits/stdc++.h>usingnamespacestd;intn,m;constintmaxn=1e3+10;structnode{ intu,w; //顺序好像不能错};boolvis[maxn];intdist[maxn];vector<node>g[maxn];voiddijk(ints){ priority_qu......
  • 嵌入式软件--C语言高级 DAY 8.5 相关函数
    递归函数在嵌入式中应用不常见,但对于学习C语言的我们,也要时刻记得它的作用和用法。此外还要记住sprintf尤其重要!还有时间戳!一、递归函数1.概念一个函数在函数体内又调用了本身。但必须满足两个条件:具有明显的结束条件;趋近于结束条件的趋势。2.递归原理#include<stdio.h>......