IO进程线程day4

 两个线程拷贝文件

#include <myhead.h>

/*****************定义求文件长度函数********************/
int getlen(const char *srcfile, const char *dstfile)
{
    //定义两个文件描述符
    int fd1, fd2;
    //以只读的形式打开源文件
    if((fd1=open(srcfile, O_RDONLY)) == -1)
    {
        perror("srcfile open error");
        return -1;
    }

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

    //求源文件的长度
    int len = lseek(fd1, 0, SEEK_END);    

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

    return len;
}

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

    //以只写的形式打开目标文件
    if((fd2 = open(dstfile, O_RDWR|O_CREAT|O_APPEND)) == -1)
    {
        perror("dstfile open error");
        return ;
    }

    //将光标定位在开始拷贝的位置
    lseek(fd1, start, SEEK_SET);
    lseek(fd2, start, SEEK_SET);

    //开始进行拷贝工作
    char buf[128] = "";        //数据搬运工
    int sum = 0;        //记录搬运的总个数
    
    while(1)
    {
        //从源文件中读取数据
        int ret = read(fd1, buf, sizeof(buf));
        sum += ret;               //记录每次搬运的个数

        if(ret==0 || sum>len)
        {
            write(fd2, buf, ret-(sum-len));        //将最后一次不满128的数据拷贝到新文件中
            break;
        }

        //将数据写入目标文件
        write(fd2, buf, ret);
    }

    printf("拷贝成功\n");
}
/**********************定义线程体函数**************************/
//定义线程体函数
void *task1(void *arg)
{
	int len = * (int *)arg;
	copy_file("./work12.c","./testwork.c",0,len/2);
	pthread_exit(NULL);
}

void *task2(void *arg)
{
	sleep(1);
	int len =* (int *)arg;
	copy_file("./work12.c","./testwork.c",len/2,len-len/2);
	pthread_exit(NULL);
}

/**********************主函数********************************/

int main(int argc, const char *argv[])
{
/*	if(argc != 3)
	{
		printf("input file error\n");
		printf("usage:./a.out srcfile dstfile\n");
		return -1;
	}*/
	//求文件的长度
	int len = getlen("./work12.c","./testwork.c");

	//定义线程号
	pthread_t tid1;
	pthread_t tid2;
	//创建线程1
	if(pthread_create(&tid1,NULL,task1,&len))
	{
		printf("pthread_create error");
		return -1;
	}
	//创建线程2
	if(pthread_create(&tid2,NULL,task2,&len))
	{
		printf("pthread_create error");
		return -1;

	}
	pthread_join(tid1,NULL);
	pthread_join(tid2,NULL);

	return 0;
}

使用三个线程拷贝文件

#include <myhead.h>

/*****************定义求文件长度函数********************/
int getlen(const char *srcfile, const char *dstfile)
{
    //定义两个文件描述符
    int fd1, fd2;
    //以只读的形式打开源文件
    if((fd1=open(srcfile, O_RDONLY)) == -1)
    {
        perror("srcfile open error");
        return -1;
    }

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

    //求源文件的长度
    int len = lseek(fd1, 0, SEEK_END);    

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

    return len;
}

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

    //以只写的形式打开目标文件
    if((fd2 = open(dstfile, O_RDWR|O_CREAT|O_APPEND)) == -1)
    {
        perror("dstfile open error");
        return ;
    }

    //将光标定位在开始拷贝的位置
    lseek(fd1, start, SEEK_SET);
    lseek(fd2, start, SEEK_SET);

    //开始进行拷贝工作
    char buf[128] = "";        //数据搬运工
    int sum = 0;        //记录搬运的总个数
    
    while(1)
    {
        //从源文件中读取数据
        int ret = read(fd1, buf, sizeof(buf));
        sum += ret;               //记录每次搬运的个数

        if(ret==0 || sum>len)
        {
            write(fd2, buf, ret-(sum-len));        //将最后一次不满128的数据拷贝到新文件中
            break;
        }

        //将数据写入目标文件
        write(fd2, buf, ret);
    }

    printf("拷贝成功\n");
}
/**********************定义线程体函数**************************/
//定义线程体函数
void *task1(void *arg)
{
	int len = * (int *)arg;
	copy_file("./work12.c","./testwork.c",0,len/3);
	pthread_exit(NULL);
}

void *task2(void *arg)
{
	sleep(1);
	int len =* (int *)arg;
	copy_file("./work12.c","./testwork.c",len/3,len/3*2-len/3);
	pthread_exit(NULL);
}
void *task3(void *arg)
{
	sleep(2);
	int len =* (int *)arg;
	copy_file("./work12.c","./testwork.c",len/3*2,len-len/3*2);
	pthread_exit(NULL);
}

/**********************主函数********************************/

int main(int argc, const char *argv[])
{
	//求文件的长度
	int len = getlen("./work12.c","./testwork.c");

	//定义线程号
	pthread_t tid1;
	pthread_t tid2;
	pthread_t tid3;
	//创建线程1
	if(pthread_create(&tid1,NULL,task1,&len))
	{
		printf("pthread_create error");
		return -1;
	}
	//创建线程2
	if(pthread_create(&tid2,NULL,task2,&len))
	{
		printf("pthread_create error");
		return -1;
	}
	//创建线程3
	if(pthread_create(&tid3,NULL,task3,&len))
	{
		printf("pthread_create error");
		return -1;
	}
	sleep(3);
	pthread_detach(tid1);
	pthread_detach(tid2);
	pthread_detach(tid3);

	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值