#include<myhead.h>
int main(int argc, const char *argv[])
{
pid_t ID;//子父进程在 fork之后独立运行
ID = fork();//在main函数中创建子进程
int fd;
if(ID == 0)
{
//将1.txt内容拷贝到2.txt中
fd = open("./1.txt",O_RDONLY);
if(fd == -1)
{
perror("open");
return -1;
}
int len = lseek(fd,0,SEEK_END);//len是文件的字节数
char buff[2000]="";
lseek(fd,0,SEEK_SET);
int readlen = read(fd,buff,len);//读取
printf("len = %d\n",len);
printf("readlen = %d\n",readlen);
printf("%s\n",buff);
printf("\n");
close(fd);
fd = open("./2.txt",O_WRONLY|O_CREAT|O_TRUNC,0664);
if(fd == -1)
{
perror("open");
return -1;
}
write(fd,buff,len);//写入
close(fd);
}
else if(ID>0)
{
//将3.txt的内容拷贝到4.txt中
fd = open("./3.txt",O_RDONLY);
if(fd == -1)
{
perror("open");
return -1;
}
int len = lseek(fd,0,SEEK_END);//len是文件的字节数
char buff[2000]="";
lseek(fd,0,SEEK_SET);
int readlen = read(fd,buff,len);//读取
printf("len = %d\n",len);
printf("readlen = %d\n",readlen);
printf("%s\n",buff);
printf("\n");
close(fd);
fd = open("./4.txt",O_WRONLY|O_CREAT|O_TRUNC,0664);
if(fd == -1)
{
perror("open");
return -1;
}
write(fd,buff,len);//写入
close(fd);
}
else
{
perror("fork");
return -1;
}
while(1);
return 0;
}