2024-2-21-多线程基础作业

 作业:

源代码:

#include <myhead.h>
#define MAXSIZE 64
//定义要传递的结构体类型
struct Info
{
    const char *src;
    const char *dest;
    int len;
};
int get_file_len(const char *srcfile, const char *destfile)
{

    //以只读的形式打开源文件
    int srcfd, destfd;
    if ((srcfd = open(srcfile, O_RDONLY)) == -1)
    {
        perror("open srcfile error");
        return -1;
    }
    //以只写创建的形式打开目标文件
    if ((destfd = open(destfile, O_WRONLY | O_CREAT | O_TRUNC, 0664)) == -1)
    {
        perror("open destfile error");
        return -1;
    }

    //求源文件的大小
    int len = lseek(srcfd, 0, SEEK_END);

    //关闭两个文件
    close(srcfd);
    close(destfd);

    return len;
}
void copy_file(const char *srcfile, const char *destfile, int start, int len)
{
    int srcfd = -1, destfd = -1;
    //处理主线程中传过来的数据

    if ((srcfd = open(srcfile, O_RDONLY)) == -1)
    {
        printf("open error\n");
        return;
    }
    if ((destfd = open(destfile, O_WRONLY | O_CREAT | O_TRUNC, 0664)) == -1)
    {
        printf("open error\n");
        return;
    }
    lseek(srcfd, start, SEEK_SET);
    lseek(destfd, start, SEEK_SET);

    char buf[MAXSIZE] = "";
    int sum = 0;
    while (1)
    {
        int res = read(srcfd, buf, sizeof(buf));
        sum += res; //将每次读取的数据放入sum中

        if (sum >= len || res - (sum - len))
        {
            write(destfd, buf, res - (sum - len)); //将最后一次的内容写入
            break;
        }
        //将读取的数据写入目标文件
        write(destfd, buf, res);
        close(srcfd);
        close(destfd);

        return;
    }
}

//定义分支线线程1
void *task1(void *arg)
{
    struct Info buf = *((struct Info *)arg);
    copy_file(buf.src, buf.dest, 0, buf.len);
}

void *task2(void *arg)
{

    //处理主线程中传过来的数据
    struct Info buf = *((struct Info *)arg);
    copy_file(buf.src, buf.dest, buf.len / 2, buf.len);
}

int main(int argc, const char *argv[])
{

    // if (argc != 4)
    // {
    //     printf("input file error\n");
    //     printf("usage:./a.out srcfile destfile\n");
    //     return -1;
    // }
    // int len = get_file_len(argv[3], argv[4]);
    // sleep(1);
    char *srcfile="./1.txt";
    char *destfile="./2.txt";

    int len = get_file_len(srcfile, destfile);
    //定义一个结构体变量
    struct Info buf = {srcfile, destfile, len};
    sleep(1);



    pthread_t tid1, tid2;
    //创建线程1
    if (pthread_create(&tid1, NULL, task1, &buf) != 0) //向分支线程传递一个数据
    {
        printf("tid create error\n");
        return -1;
    }

    //创建线程2
    if (pthread_create(&tid2, NULL, task2, &buf) != 0) //向分支线程传递一个数据
    {
        printf("tid create error\n");
        return -1;
    }
    //回收资源
    pthread_join(tid1, NULL);
    pthread_join(tid2, NULL);

    sleep(5);
    printf("拷贝成功\n");

    return 0;
}

效果图:

思维导图:

  • 9
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值