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

嵌入式开发C语言学习day26-华清作业8.1

时间:2024-08-01 20:55:54浏览次数:9  
标签:8.1 temp int day26 华清 source 线程 pthread buf

思维导图

作业

// 使用两个线程完成两个文件的拷贝,分支线程1拷贝前一半,分支线程2拷贝后一半,主线程回收两个分支线程的资源
#include <myhead.h>
#define MAX 1024

struct Buf
{
    char file1[20];
    char file2[20];
    int size;
};

// 进程 1 拷贝前半内容
void *copypart1(void *arg)
{
    sleep(1);
    struct Buf *buf = (struct Buf *)arg;

    int source = open(buf->file1, O_RDONLY);
    if (source == -1)
    {
        perror("线程 1 中文件打开源文件失败");
        pthread_exit(NULL);
    }

    int dest = open(buf->file2, O_WRONLY | O_CREAT | O_TRUNC, 0666);
    if (dest == -1)
    {
        perror("线程 1 中文件打开目标文件失败");
        close(source);
        pthread_exit(NULL);
    }

    char temp[MAX];
    int number = 0;
    int x = 0;
    // 指针偏移
    lseek(source, 0, SEEK_SET);
    read(source, temp, buf->size / 2);
    write(dest, temp, buf->size / 2);

    close(source);
    close(dest);

    pthread_exit(NULL);
}

// 进程 2 拷贝后半内容
void *copypart2(void *arg)
{
    sleep(2);
    struct Buf *buf = (struct Buf *)arg;

    int source = open(buf->file1, O_RDONLY);
    if (source == -1)
    {
        perror("线程 2 中文件打开源文件失败");
        pthread_exit(NULL);
    }

    int dest = open(buf->file2, O_WRONLY | O_APPEND);
    if (dest == -1)
    {
        perror("线程 2 中文件打开目标文件失败");
        close(source);
        pthread_exit(NULL);
    }

    // 指针偏移
    lseek(source, buf->size - buf->size / 2, SEEK_SET);
    char temp[MAX];
    int number = 0;
    int x = 0;
    while ((number = read(source, temp, sizeof(temp))) > 0)
    {
        write(dest, temp, number);
        x += number;
    }

    close(source);
    close(dest);

    pthread_exit(NULL);
}

int main(int argc, char const *argv[])
{
    // 判断文件名是否合法
    if (argc != 3)
    {
        printf("输入的文件名不合法\n");
        return -1;
    }

    // 打开源文件
    int fd = open(argv[1], O_RDONLY);
    if (fd == -1)
    {
        perror("文件打开失败");
        return -1;
    }

    // 计算文件的总大小
    int filesize = 0;
    int number = 0;
    char temp[MAX];
    while ((number = read(fd, &temp, sizeof(temp))) > 0)
    {
        filesize += number;
    }
    printf("文件的大小 = %d \n", filesize);
    printf("读取文件的大小成功\n");
    close(fd);

    // 数据传输
    struct Buf buf1;
    strcpy(buf1.file1, argv[1]);
    strcpy(buf1.file2, argv[2]);
    buf1.size = filesize;

    struct Buf buf2;
    strcpy(buf2.file1, argv[1]);
    strcpy(buf2.file2, argv[2]);
    buf2.size = filesize;

    // 定义存放线程号的容器
    pthread_t tid1, tid2;

    // 创建线程 1
    if (pthread_create(&tid1, NULL, copypart1, &buf1) != 0)
    {
        printf("线程 1 创建失败\n");
        return -1;
    }

    // 创建线程 2
    if (pthread_create(&tid2, NULL, copypart2, &buf2) != 0)
    {
        printf("线程 2 创建失败\n");
    }

    // 回收线程 1
    if (pthread_join(tid1, NULL) != 0)
    {
        printf("线程 1 回收失败\n");
        return -1;
    }

    // 回收线程 2
    if (pthread_join(tid2, NULL) != 0)
    {
        printf("线程 2 回收失败\n");
        return -1;
    }

    sleep(5);
    return 0;
}

标签:8.1,temp,int,day26,华清,source,线程,pthread,buf
From: https://blog.csdn.net/weixin_64005144/article/details/140856897

相关文章

  • 旧笔记本安装Win8.1实录
    昨天发现一台尘封已久的LenovoideapadY550,给它装上了Windows10然后第二天系统挂掉了挂的原因是半夜万恶之源Windows更新开始造孽了刚好没电文件全坏了真解除封印因为文件已经没了我索性直接重装系统,降级到Win8.1真香!系统是Win8.1withupdate的精简版,开始菜单有关......
  • 2024.8.1 作业
    使用两个线程完成两个文件的拷贝,分支线程1拷贝前一半,分支线程2拷贝后一半,主线程回收两个分支线程的资源代码:/*******************************************/文件名:threadwork.c/*******************************************/#include<myhead.h>//创建传输信息的结构体......
  • C高级(学习)2024.8.1
    目录shell命令数组数组的赋值数组的调用遍历数组函数函数的定义方式函数调用分文件编程源文件头文件include引用时“”和<>的区别编译工具gcc编译工具gdb调试make工具定义Makefile格式Makefile管理多个文件Makefile变量自定义变量预定义变量自动变量Ma......
  • 【笔记】杂题选讲 2024.8.1 下午
    [AGC018F]TwoTrees[AGC018F]TwoTrees-洛谷|计算机科学教育新生态(luogu.com.cn)先判一下奇偶性。考虑做一个很强的钦定,奇数都填\(\pm1\),偶数都填\(0\)。对于同一棵树的一棵子树,考虑对子树内两个奇数点做匹配,要求匹配上的点一个\(+1\)一个\(-1\),这样就能在子树的根......
  • 【笔记】字符串选讲 2024.8.1
    [COCI2015-2016#5]OOP(Trie)P6727[COCI2015-2016#5]OOP-洛谷|计算机科学教育新生态(luogu.com.cn)正反串分别建Trie,可以搞出两个dfn区间,加之长度限制,三维数点。有\(O(n\logn)\)做法。将字典串\(S[1..m]\),对所有\(1\leqi\leqm\),将\(S[i+1,m]\)的hash值插入......
  • 8.1第三周周四学习总结
    1cfdiv951位运算(异或)https://www.luogu.com.cn/problem/CF1979B思路[l,r]=[l1,r1]^(x^y)也就是是找x^y异或一个区间后仍然能够连续,对于x^y可以写成xxxx1000,xxxx设为A,为一段01序列,那么就是区间[0,x^y-A-1]能够保证连续。因为第一个1右侧都是0,都不进位,1000+0000,100......
  • docker 拉取镜像超时:error pulling image configuration: download failed after atte
    之前是正常的,今天就罢工了,可能原因是国外镜像不稳定,被针对了吧。errorpullingimageconfiguration:downloadfailedafterattempts=6:dialtcp168.143.171.189:443:i/otimeout那就改为国内镜像:1.创建/etc/docker目录(已有的跳过)sudomkdir-p/etc/docker 2.修改......
  • VMware Aria Suite 8.18 发布 - 云管理解决方案
    VMwareAriaSuite8.18发布-云管理解决方案UnifiedCloudManagementforVMwareCloudFoundation请访问原文链接:https://sysin.org/blog/vmware-aria-suite/,查看最新版。原创作品,转载请保留出处。云管理套包VMwareAriaSuite(以前称为vRealizeSuite)和vCloudSuiteVM......
  • 类型提示在 pycharm 2018.1 中并不总是有效?
    我今天开始使用类型提示。在阅读了有关类型提示的文档后,我尝试编写一些愚蠢的示例来检查它是如何工作的,但被困在像这样简单的事情上。a:int=7.33我没有收到任何警告或错误。一切都正常,就像我没有使用类型提示一样。我期待一个警告,说浮点数不能分配给intvar。我尝......
  • 基于Docker安装elasticsearch和kibana 8.14.3
    需要先安装好Docker和DockerCompose安装的是单机版本的elasticsearch一、安装elasticsearch8.14.3复制下面的内容到elasticsearch-compose.yaml中services:elasticsearch:image:docker.elastic.co/elasticsearch/elasticsearch:8.14.3container_name:ela......