1> 使用多进程完成两个文件的拷贝,父进程拷贝前一半,子进程拷贝后一半,父进程回收子进程的资源
#include<myhead.h>
int main(int argc, const char *argv[])
{int fd1=-1,fd2=-1;if((fd1=open("./ggb.bmp",O_RDONLY,0664))==-1){perror("open error");return -1;}if((fd2=open("./ggb1.bmp",O_WRONLY|O_CREAT|O_TRUNC,0664))==-1){perror("open error");return -1;}int maxsize=lseek(fd1,0,SEEK_END);//fd1的光标最大值char buf[5];pid_t pid=-1;pid=fork();if(pid>0){int loca=0;lseek(fd1,loca,SEEK_SET);lseek(fd2,loca,SEEK_SET);while(loca<(maxsize/2)){int rew=read(fd1,buf,sizeof(buf));if(rew<=0)break;write(fd2,buf,rew);loca=lseek(fd1,0,SEEK_CUR);}}else if(pid==0){lseek(fd1,0,SEEK_SET);lseek(fd2,0,SEEK_SET);int loca=maxsize/2;while(loca<maxsize){int rew=read(fd1,buf,sizeof(buf));if(rew<0)break;write(fd2,buf,rew);loca=lseek(fd1,0,SEEK_CUR);}exit(EXIT_SUCCESS);}else if(pid<0){perror("fork error");return -1;}int loca=maxsize/2;lseek(fd1,loca,SEEK_SET);while(loca<maxsize){int rew=read(fd1,buf,sizeof(buf));if(rew<=0)break;write(fd2,buf,rew);loca=lseek(fd1,0,SEEK_CUR);}wait(NULL);close(fd1);close(fd2);return 0;
}