1,思维导图
2,用两个进程分别复制文件的上下两部分到另一个文件
#include<myhead.h>
int main(int argc, const char *argv[])
{
int fp=open("./1.txt",O_RDONLY);
if(fp==-1)
{
perror("open");
return -1;
}
int count=lseek(fp,0,SEEK_END);
close(fp);
pid_t pid1;
pid1=fork();
if(pid1==0)
{
char str1[5];
int fp1=open("1.txt",O_RDONLY);
int fp2=open("2.txt",O_WRONLY|O_CREAT|O_TRUNC,0664);
int siz_read1=0;
int siz_read2=0;
while(siz_read2<count/2&&((siz_read1=read(fp1,str1,sizeof(str1)))>0))
{
siz_read2+=siz_read1;
write(fp2,str1,siz_read1);
}
close(fp1);
close(fp2);
exit(EXIT_SUCCESS);
}
else if(pid1>0)
{
pid_t pid2;
pid2=fork();
if(pid2==0)
{
char str1[5];
int fp1=open("1.txt",O_RDONLY);
int fp2=open("2.txt",O_WRONLY);
lseek(fp1,count/2,SEEK_SET);
lseek(fp2,count/2,SEEK_SET);
int siz_read1=0;
while((siz_read1=read(fp1,str1,sizeof(str1)))>0)
{
write(fp2,str1,siz_read1);
}
close(fp1);
close(fp2);
exit(EXIT_SUCCESS);
}
else if(pid2>0)
{
waitpid(pid1,NULL,0);
waitpid(pid2,NULL,0);
exit(EXIT_SUCCESS);
}
}
return 0;
}