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

思维导图

作业

// 使用两个线程完成两个文件的拷贝,分支线程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;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值