6.27HW

#include 
struct info
{
	const char *srcfile;
	const char *dfdfile;
	int start;
	int size;
};
//len函数
int file_len(const char *srcfile,const char*dfdfile)
{
	int src,dfd,len;
	if((src=open(srcfile,O_RDONLY))==-1)
	{
		perror("open error");
		return -1;
	}
	if((dfd=open(dfdfile,O_WRONLY|O_CREAT|O_TRUNC),0664)==-1)
	{
		perror("open error");
		return -1;
	}
	len=lseek(src,0,SEEK_END);
	close(src);
	close(dfd);
	return len;
}
//线程体函数
void *file_copy(void *arg)
{
	struct info *n=(struct info*)arg;
	int src,dfd;
	if((src=open(n->srcfile,O_RDONLY))==-1)
	{
		perror("open error");
		return NULL;
	}
	if((dfd=open(n->dfdfile,O_WRONLY|O_CREAT|O_TRUNC),0664)==-1)
	{
		perror("open error");
		return NULL;
	}
	lseek(src,n->start,SEEK_SET);
	lseek(dfd,n->start,SEEK_SET);
	char buf[128]="";
	int count =0;
	while(1)
	{
		int ret=read(src,buf,sizeof(buf));
		count+=ret;
		if(ret==0||count>n->size)
		{
			write(dfd,buf,ret-(count-n->size));
			break;
		}
		write(dfd,buf,sizeof(buf));
	}
}


int main(int argc, const char *argv[])
{
	//计算长度
	int len;
	len=file_len(argv[1],argv[2]);
	printf("len=%d",len);
	//定义一个结构体数组
	struct info s[2]=
	{
		{argv[1],argv[2],0,len/2},
		{argv[1],argv[2],len/2,len-len/2}
	};
	//定义一个线程号
	pthread_t tid,tid1;
	//线程1
	if(pthread_create(&tid,NULL,file_copy,&s[0]))
	{
		perror("create error");
		return -1;
	}
	//线程2
	if(pthread_create(&tid1,NULL,file_copy,&s[1]))
	{
		perror("create error");
		return -1;
	}
	while(1);
	return 0;
}

你可能感兴趣的:(java,算法,数据结构)