思维导图:
1.使用父子进程实现一个图片的拷贝
要求父进程拷贝前一部分
子进程拷贝后一部分
使用diff查看两个文件是否相同
#include <head.h>
int main(int argc, const char *argv[])
{
int fd1=open("/home/ubuntu/3.6/xiaoxin.bmp",O_RDONLY);
int fd2=open("/home/ubuntu/3.6/cp.bmp",O_WRONLY | O_CREAT | O_TRUNC,0644);
struct stat st;
if(stat("/home/ubuntu/3.6/xiaoxin.bmp",&st)==-1)
{
PRINT_ERROR("stat error");
}
if(fd1==0||fd2==0)
PRINT_ERROR("open error");
//获取文件大小
off_t size=st.st_size;
off_t half=size/2;
pid_t pid=fork();
if(pid>0)
{
char *buf=malloc(half);
read(fd1,buf,half);
write(fd2,buf,half);
free(buf);
close(fd2);
wait(NULL);
printf("parent done\n");
}
else if(pid==0)
{
char *buf=malloc(size-half);
lseek(fd1,half,SEEK_SET);
lseek(fd2,half,SEEK_SET);
read(fd1,buf,size-half);
write(fd2,buf,size-half);
free(buf);
close(fd1);
close(fd2);
printf("child done\n");
}
return 0;
}