2024.8.1 作业

使用两个线程完成两个文件的拷贝,分支线程1拷贝前一半,分支线程2拷贝后一半,主线程回收两个分支线程的资源

代码:

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

文件名:threadwork.c

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

#include <myhead.h>
//创建传输信息的结构体
struct TXT
{
    char *a;
    char *b;
    int key;
};
//定义求源文件大小的函数
int get_file_len(const char *srcfile, const char *destfile)
{
    //以只读的形式打开源文件
    int sfd = open(srcfile, O_RDONLY);
    if (sfd == -1)
    {
        perror("open srcfile error");
        return -1;
    }

    //以创建的形式打开目标文件
    int dfd = open(destfile, O_WRONLY | O_CREAT | O_TRUNC, 0664);
    if (dfd == -1)
    {
        perror("open destfile error");
        return -1;
    }

    int size = lseek(sfd, 0, SEEK_END);

    //关闭文件
    close(sfd);
    close(dfd);

    return size; //将文件大小返回
}

//定义文件拷贝函数
void copy_file(const char *srcfile, const char *destfile, int start, int len)
{
    //以只读的形式打开源文件
    int sfd = open(srcfile, O_RDONLY);
    if (sfd == -1)
    {
        perror("open srcfile error");
        return;
    }

    //以创建的形式打开目标文件
    int dfd = open(destfile, O_WRONLY);
    if (dfd == -1)
    {
        perror("open destfile error");
        return;
    }

    //移动光标位置
    lseek(sfd, start, SEEK_SET);
    lseek(dfd, start, SEEK_SET);

    //定义搬运工
    char buf[128] = "";
    int sum = 0; //记录拷贝的总个数
    while (1)
    {
        //从源文件中读取数据
        int res = read(sfd, buf, sizeof(buf));
        sum += res; //将读取的个数累加

        if (res == 0 || sum > len) //表示文件读取结束
        {
            write(dfd, buf, res - (sum - len)); //父进程将最后一次拷贝结束
            break;
        }
        //写入到目标文件中
        write(dfd, buf, res);
    }

    //关闭文件
    close(sfd);
    close(dfd);
}
void *task(void *arg)
{
    if (arg == NULL)
    {
        return NULL;
    }
    // 将arg转换为struct TXT指针
    struct TXT *txt_ptr = (struct TXT *)arg;
    int len = get_file_len(txt_ptr->a, txt_ptr->b);
    pid_t pid = fork();
    if (txt_ptr->key == 0)
    {
        //线程1拷贝前一半
        copy_file(txt_ptr->a, txt_ptr->b, 0, len / 2);
        pthread_exit(NULL);
    }
    else if (txt_ptr->key == 1)
    {
        //线程2拷贝后一半
        copy_file(txt_ptr->a, txt_ptr->b, len / 2, len - len / 2);
        pthread_exit(NULL);
    }
    else
    {
        printf("load error\n");
    }
}
int main(int argc, char const *argv[])
{
    //判断是否是三个文件
    if (argc != 3)
    {
        write(2, "input file error\n", sizeof("input file error\n"));
        return -1;
    }
    // 使用strdup复制argv中的字符串,因为argv中的字符串是常量
    char *source = strdup(argv[1]);
    char *destination = strdup(argv[2]);
    if (!source || !destination)
    {
        perror("strdup failed");
        // 释放之前可能已经分配的内存
        free(source);
        free(destination);
        return -1;
    }
    //创建结构体接收字符信息
    struct TXT txt1 = {source, destination, 0};
    struct TXT txt2 = {source, destination, 1};
    //创建两个线程
    pthread_t tid1, tid2;
    if (pthread_create(&tid1, NULL, task, &txt1) != 0)
    {
        printf("thread create error\n");
        return -1;
    }
    if (pthread_create(&tid2, NULL, task, &txt2) != 0)
    {
        printf("thread create error\n");
        return -1;
    }
    //阻塞等待线程结束
    pthread_join(tid1, NULL);
    pthread_join(tid2, NULL);
    return 0;
}

结果:

思维导图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值