#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
int main(int argc, const char *argv[])
{
int a=open("./1.jpg",O_RDONLY);
if(a<0)
{
perror("open");
return -1;
}
int b=open("./2.jpg",O_WRONLY | O_CREAT | O_TRUNC,0775);
if(b<0)
{
perror("open");
return -1;
}
off_t size = lseek(a, 0, SEEK_END);
lseek(a, 0, SEEK_SET);
pid_t pid=fork();
if(pid > 0)
{
sleep(5);
lseek(a, 0, SEEK_SET);
lseek(b, 0, SEEK_SET);
char buf;
for(int i=0; i<size/2; i++)
{
read(a,&buf,1);
write(b,&buf,1);
}
}
else if(pid == 0)
{
lseek(a,size/2,SEEK_SET);
lseek(b,size/2,SEEK_SET);
char buf;
for(int i=0; i<size/2+1; i++)
{
read(a,&buf,1);
write(b,&buf,1);
}
}
else
{
perror("fork");
return -1;
}
close(a);
close(b);
return 0;
}