#include<th.h>
#include<sys/wait.h>
int main(int argc, const char *argv[])
{
int src,des;
if((src = open("th.txt",O_RDONLY)) == -1)
{
perror("open error");
return -1;
}
if((des = open("ah.txt",O_WRONLY | O_CREAT | O_TRUNC,0664)) == -1)
{
perror("open error");
return -1;
}
pid_t pid;
pid = fork();
char buf[233];
if(pid > 0)
{
int sz = lseek(src,0,SEEK_END)/2;
lseek(src,0,SEEK_SET);
int ret = 0;
if((ret = read(src,buf,sz)) > 0)
{
write(des,buf,ret);
}
close(src);
close(des);
wait(NULL);
}
else if(pid == 0)
{
int len = lseek(src,0,SEEK_END);
int len1;
if(len % 2 == 0)
{
len1 = len / 2;
lseek(src,len1,SEEK_SET);
}
else
{
len1 = len / 2 + 1;
lseek(src,len1 - 1,SEEK_SET);
}
int i = 0;
if((i = read(src,buf,len1)) > 0)
{
write(des,buf,i);
}
close(src);
close(des);
}
else
{
perror("fork error");
return -1;
}
return 0;
}
#include<th.h>
#include<sys/wait.h>
int main(int argc, const char *argv[])
{
int src,des;
if((src = open("th.txt",O_RDONLY)) == -1)
{
perror("open error");
return -1;
}
if((des = open("ah.txt",O_WRONLY | O_CREAT | O_TRUNC,0664)) == -1)
{
perror("open error");
return -1;
}
pid_t pid;
pid = fork();
char buf[233];
if(pid > 0)
{
pid_t pid1 = fork();
if(pid1 > 0)
{
int sz = lseek(src,0,SEEK_END)/3;
lseek(src,0,SEEK_SET);
int ret = 0;
if((ret = read(src,buf,sz)) > 0)
{
write(des,buf,ret);
}
close(src);
close(des);
wait(NULL);
wait(NULL);
}
else if(pid1 == 0)
{
int sz1 = lseek(src,0,SEEK_END)/3;
lseek(src,sz1,SEEK_SET);
int j = 0;
if((j = open(src,buf,sz1)) > 0)
{
write(des,buf,j);
}
}
else
{
perror("fork error");
return -1;
}
}
else if(pid == 0)
{
int len = lseek(src,0,SEEK_END)/3;
lseek(src,len * 2,SEEK_SET);
int i = 0;
if((i = read(src,buf,len)) > 0)
{
write(des,buf,i);
}
close(src);
close(des);
}
else
{
perror("fork error");
return -1;
}
return 0;
}