#include "head.h"
int main(int argc, char *argv[])
{
int fds, fdt, len, nbyte;
pid_t pid;
char buf[64];
if ((fds = open("./jpg.jpg", O_RDONLY)) < 0)
{
perror("fail to open");
exit(-1);
}
if ((fdt = open("./jpg1.jpg", O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0)
{
perror("fail to open");
exit(-1);
}
len = lseek(fds, 0, SEEK_END);
len /= 2;
if ((pid = fork()) < 0)
{
perror("fail to fork");
exit(-1);
}
else if (pid == 0)
{
close(fds);
close(fdt);
fds = open("./jpg.jpg", O_RDONLY);
fdt = open("./jpg1.jpg", O_WRONLY);
lseek(fds, len, SEEK_SET);
lseek(fdt, len, SEEK_SET);
while ((nbyte = read(fds, buf, 64)) > 0)
{
write(fdt, buf, nbyte);
}
close(fds);
close(fdt);
}
else
{
lseek(fds, 0, SEEK_SET);
while (len > 0)
{
nbyte = read(fds, buf, 64);
write(fdt, buf, nbyte);
len -= nbyte;
}
close(fds);
close(fdt);
wait(NULL);
}
return 0;
}