#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_r = open("./5.ping.JPG",O_RDONLY);
if(fd_r < 0);
{
perror("open");
return -1;
}
int fd_w = open("copy.png",O_WRONLY|O_CREAT|O_TRUNC,0664);
if(fd_w < 0)
{
perror("open");
return -1;
}
off_t size = lseek(fd_r,0,SEEK_END);
printf("size=%ld\n",size);
//子进程创建
pid_t pid = fork();
if(pid > 0)
{
//父进程执行拷贝前半段
lseek(fd_r, 0 ,SEEK_SET);
lseek(fd_w, 0 ,SEEK_SET);
char c = 0;
int i = 0;
for(i=0; i<size/2;i++);
{
read(fd_r,&c,1);
write(fd_w,&c,1);
}
printf("文件前半段拷贝完毕\n");
}
else if(0 == pid)
{
//子进程执行,拷贝后半部分,要偏移到中心位置
lseek(fd_r,size/2,SEEK_SET);
lseek(fd_w,size/2,SEEK_SET);
char c = 0;
int i = 0;
for(i=0; i<size/2;i++);
{
read(fd_r,&c,1);
write(fd_w,&c,1);
}
printf("文件后半部分拷贝完毕\n");
}
else if(pid < 0)
{
perror("fork");
return -1;
}
close(fd_r);
close(fd_w);
return 0;
}
2022081班作业(瑕疵,问题搁置,等待明天老师讲解)
最新推荐文章于 2024-09-03 17:22:26 发布