int main(int argc, const char *argv[])
20 {
21 umask(0);
22 int fd=open("1.png",O_RDONLY);
23 int fd1=open("2.png",O_WRONLY|O_CREAT|O_TRUNC,0777);
24 off_t size=lseek(fd,0,SEEK_END);
25 printf("%ld\n",size);
26 lseek(fd,0,SEEK_SET);
27 pid_t pid=fork();
28 if(pid>0)
29 {
30 sleep(2);
31
32 char c=0;
33 off_t count=size/2;
34 lseek(fd,0,SEEK_SET);
35 lseek(fd1,0,SEEK_SET);
36
37 for(off_t i=1;i<=count;i++)
38 {
39 c=0;
40 if( read(fd,&c,1)<0)
41 ERR("read");
42 if( write(fd1,&c,1)<0)
43 ERR("write");
44 }
45 close(fd);
46 close(fd1);
47
48 }
49 else if(pid==0)
50 {
51
52 char c=0;
53 off_t count=size/2;
54 lseek(fd,count,SEEK_SET);
55 lseek(fd1,count,SEEK_SET);
56 for(off_t i=count+1;i<=size;i++)
57 {
58 c=0;
59 if( read(fd,&c,1)<0)
60 ERR("read");
61 if( write(fd1,&c,1)<0)
62 ERR("write");
63 }
64 close(fd);
65 close(fd1);
66
67 }
12.8作业
最新推荐文章于 2024-07-19 17:37:28 发布