#include <stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<sys/stat.h>
#include<fcntl.h>
int main(int argc, const char *argv[])
{
int fd=open(“./1.png”,O_RDONLY);
if(fd<0){
printf(“fd”);
perror(“open”);
return -1;
}
struct stat x;;
stat("./1.png",&x);
off_t a=x.st_size;
char y;
int i=0;
pid_t c =fork();
if(c>0)//父进程中为真
{
int cp=open("./cp.png",O_WRONLY|O_CREAT,0777);
if(cp<0){
printf("cp");
perror("open");
return -1;
}
printf("父进程");
while(i<a/2){
read(fd,&y,1);
write(cp,&y,1);
i++;
}
}else if(c==0)//子进程中为真
{
int cp1=open("./cp1.png",O_WRONLY|O_CREAT,0777);
if(cp1<0){
printf("cp");
perror("open");
return -1;
}
printf("子进程");
while(i<a/2){
read(fd,&y,1);
write(cp1,&y,1);
i++;
}
}else{
perror("fork");
return -1;
}
return 0;
}