8.21作业

双线程拷贝

#include<myhead.h>
typedef struct
{
    const char *src;  // argv[1]
    const char *dst; // argv[1]
    int start;        //光标开始位置
    int len;          //需要向后读取个数
}Node;

//计算要拷贝文件字符个数
int getlen(const char *srcfile, const char *dstfile)
{
    int fd1, fd2;
    int len = 0;

    //只读打开源文件
    if ((fd1 = open(srcfile, O_RDONLY)) == -1)
    { 
        printf("open srcfile error");
    }

    //只写打开目标文件
    if ((fd2 = open(dstfile, O_WRONLY | O_CREAT | O_TRUNC, 0664)) == -1)
    {
        
        printf("open dstfile error");
    }

    //读取目标要拷贝的文件的字节数
    len = lseek(fd1, 0, SEEK_END);

    close(fd1); //关闭文件
    close(fd2);

    return len;
}

//双线程拷贝文件
int copy_file(const char *srcfile, const char *dstfile, int start, int len)
{
    int fd1, fd2;
    char buf[128] = {0};   //临时存放数组
    int ret = 0;

    //记录已经读取字节数
    int sum = 0;

    //打开目标要拷贝的文件
    if ((fd1 = open(srcfile, O_RDONLY)) == -1)
    { 
        printf("open srcfile error");
    }

    //打开存放拷贝的文件
    if ((fd2 = open(dstfile, O_WRONLY)) == -1)
    { 
        printf("open dstfile error");
    }
    //光标从头开始,移动start
    lseek(fd1, start, SEEK_SET);
    lseek(fd2, start, SEEK_SET);

    while (1)
    {
        ret = read(fd1, buf, sizeof(buf));

        sum += ret; //记录已经读取字节数
        //结尾||读多了
        if (ret == 0 || sum > len)
        {
            write(fd2, buf, ret - (sum - len));
            break;
        }
        write(fd2, buf, ret);
    }
    close(fd1);
    close(fd2);
    return 0;
}


//线程体函数
void *task(void *arg)
{
    Node data = *(Node *)arg;

    /*双线程拷贝文件*/
    copy_file(data.src, data.dst, data.start, data.len);
    pthread_exit(NULL); //退出线程
}

int main(int argc, const char *argv[])
{
    
    if (argc != 3)
    {
     
        fprintf(stderr, "input error,try again\n");
        fprintf(stderr, "usage:./a.out srcfile,destfile\n");
        return -1;
    }
  
    int len= getlen(argv[1], argv[2]);

    //结构体赋值
    Node data[] = {
        {argv[1], argv[2], 0, len / 2},
        {argv[1], argv[2], len / 2, (len - len / 2)},
    };


    //新建子线程
    pthread_t tid1, tid2; 
    if (pthread_create(&tid1, NULL, task, (void *)&data[0]))
        printf("thread 1 create error");

    if (pthread_create(&tid2, NULL, task, (void *)&data[1]))
        printf("thread 2 create error");

	//回收
    pthread_join(tid1, NULL);
    pthread_join(tid2, NULL);

    return 0;
}

三线程拷贝

#include<myhead.h>
typedef struct
{
    const char *src;  // argv[1]
    const char *dst; // argv[1]
    int start;        //光标开始位置
    int len;          //需要向后读取个数
}Node;

//计算要拷贝文件字符个数
int getlen(const char *srcfile, const char *dstfile)
{
    int fd1,fd2;
    int len = 0;

    //只读打开源文件
    if ((fd1 = open(srcfile, O_RDONLY)) == -1)
    { 
        printf("open srcfile error");
    }

    //只写打开目标文件
    if ((fd2 = open(dstfile, O_WRONLY | O_CREAT | O_TRUNC, 0664)) == -1)
    {
        
        printf("open dstfile error");
    }

    //读取目标要拷贝的文件的字节数
    len = lseek(fd1, 0, SEEK_END);

    close(fd1); //关闭文件
    close(fd2);

    return len;
}

//三线程拷贝文件
int copy_file(const char *srcfile, const char *dstfile, int start, int len)
{
    int fd1,fd2;
    char buf[128] = {0};   //临时存放数组
    int ret = 0;

    //记录已经读取字节数
    int sum = 0;

    //打开目标要拷贝的文件
    if ((fd1 = open(srcfile, O_RDONLY)) == -1)
    { //只读
        printf("open srcfile error");
    }
    //打开存放拷贝的文件
    if ((fd2 = open(dstfile, O_WRONLY)) == -1)
    { //读写
        printf("open dstfile error");
    }
    //光标从头开始,移动start
    lseek(fd1, start, SEEK_SET);
    lseek(fd2, start, SEEK_SET);

    while (1)
    {
        ret = read(fd1, buf, sizeof(buf));

        sum += ret; //记录已经读取字节数
        //结尾||读多了
        if (ret == 0 || sum > len)
        {
            write(fd2, buf, ret - (sum - len));
            break;
        }
        write(fd2, buf, ret);
    }
    close(fd1);
    close(fd2);
    return 0;
}


//线程体函数
void *task(void *arg)
{
    Node data = *(Node *)arg;

    /*三线程拷贝文件*/
    copy_file(data.src, data.dst, data.start, data.len);
    pthread_exit(NULL); //退出线程
}

int main(int argc, const char *argv[])
{
    
    if (argc != 3)
    {
     
        fprintf(stderr, "input error,try again\n");
        fprintf(stderr, "usage:./a.out srcfile,destfile\n");
        return -1;
    }
  
    int len= getlen(argv[1], argv[2]);

    //结构体赋值
    Node data[] = {
        {argv[1], argv[2], 0, len/3},
        {argv[1], argv[2], len/3, len*2/3},
		{argv[1], argv[2], len*2/3,len-len*2/3}
    };


    //新建子线程
    pthread_t tid1, tid2, tid3; 
    if (pthread_create(&tid1, NULL, task, (void *)&data[0]))
	   { 
		   printf("pthread_create error\n");
	   }

    if (pthread_create(&tid2, NULL, task, (void *)&data[1]))
        { 
			printf("pthread_create error\n");
		}

	if (pthread_create(&tid3, NULL, task, (void *)&data[2]))
	   {
	        printf("pthread_create error\n");
	   }

	//回收
    pthread_join(tid1, NULL);
    pthread_join(tid2, NULL);
	pthread_join(tid3, NULL);

    return 0;
}

 

猜你喜欢

转载自blog.csdn.net/JunCool02/article/details/132416506