1> 使用两个线程完成两个文件的拷贝,主线程拷贝前一半内容,子线程拷贝后一半内容,并且主线程要阻塞回收子线程资源
#include <myhead.h>
typedef struct
{
const char *fd1;
const char *fd2;
int start;
int len;
}Date,*DatePtr;
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_WRONLY))==-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));
break;
}
write(fd2,buf,ret);
}
printf("拷贝成功\n");
}
void *task1(void *arg)
{
Date data=*(Date *)arg;
copy_file(data.fd1,data.fd2,data.start,data.len);
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
if(argc!=3)
{
printf("input file error\n");
printf("usage:./a.out filename1 filename2\n");
return -1;
}
int len=getlen(argv[1],argv[2]);
Date data1={argv[1],argv[2],len/2,len-len/2};
Date data={argv[1],argv[2],0,len/2};
pthread_t tid1;
if(pthread_create(&tid1,NULL,task1,&data1))
{
printf("线程创建失败\n");
return -1;
}
copy_file(argv[1],argv[2],data.start,data.len);
pthread_join(tid1, NULL);
return 0;
}
2> 使用三个进程完成两个文件的拷贝,主线程拷贝前三分之一,子线程1拷贝中间三分之一,子线程2拷贝后三分之一,主线程要设置两个子线程为分离态
#include <myhead.h>
int main(int argc, const char *argv[])
{
system("ls -l");
system("sl");
system("rm ./a.out");
return 0;
}
ubuntu@ubuntu:5$ cat 12.c
#include <myhead.h>
typedef struct
{
const char *fd1;
const char *fd2;
int start;
int len;
}Date,*DatePtr;
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_WRONLY))==-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));
break;
}
write(fd2,buf,ret);
}
printf("拷贝成功\n");
}
void *task1(void *arg)
{
sleep(1);
Date data=*(Date *)arg;
copy_file(data.fd1,data.fd2,data.start,data.len);
pthread_exit(NULL);
}
void *task2(void *arg)
{
sleep(2);
Date data=*(Date *)arg;
copy_file(data.fd1,data.fd2,data.start,data.len);
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
if(argc!=3)
{
printf("input file error\n");
printf("usage:./a.out filename1 filename2\n");
return -1;
}
int len=getlen(argv[1],argv[2]);
Date data1={argv[1],argv[2],len/3,(len-len/3)-(len-len/3*2)};
Date data2={argv[1],argv[2],len/3*2,len-len/3*2};
Date data={argv[1],argv[2],0,len/3};
pthread_t tid1;
if(pthread_create(&tid1,NULL,task1,&data1))
{
printf("线程创建失败\n");
return -1;
}
pthread_t tid2;
if(pthread_create(&tid2,NULL,task2,&data2))
{
printf("线程创建失败\n");
return -1;
}
copy_file(argv[1],argv[2],data.start,data.len);
sleep(3);
pthread_detach(tid1);
pthread_detach(tid2);
return 0;
}
思维导图: