1> 使用多个进程实现文件拷贝
#include<myhead.h>
int main(int argc, const char *argv[])
{
pid_t pid;
pid=fork();
int fdr;
char buf;
if((fdr=open(argv[1],O_RDONLY))==-1)
{
perror("open error");
return -1;
}
int len=lseek(fdr,0,SEEK_END)-lseek(fdr,0,SEEK_SET);
int num=len/2;
if(pid>0)
{
int num1,fdw;
num1=0;
if((fdw=open(argv[2],O_WRONLY|O_CREAT|O_TRUNC,0664))==-1)
{
perror("open error");
return -1;
}
lseek(fdr,0,SEEK_SET);
while(num1<num)
{
int res=read(fdr,&buf,sizeof(buf));
write(fdw,&buf,res);
num1+=res;
}
close(fdw);
}
else if(pid==0)
{
int fdw;
if((fdw=open(argv[2],O_WRONLY|O_CREAT|O_APPEND,0664))==-1)
{
perror("open error");
return -1;
}
lseek(fdr,len-num-1,SEEK_SET);
lseek(fdw,0,SEEK_END);
int res;
while((res=read(fdr,&buf,sizeof(buf)))>0)
{
write(fdw,&buf,res);
}
close(fdw);
}
close(fdr);
return 0;
}