今日任务
代码:
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
-
- /*
- * function: 复制图片
- * @param [ in]
- * @param [out]
- * @return
- */
-
- int main(int argc, const char *argv[])
- {
- //打开文件
- int fd=open("./DSCN7970.JPG",O_RDONLY);
- if(fd==-1){
- puts("fd open failed");
- return -1;
- }
- int dest=open("./copy.JPG",O_WRONLY|O_CREAT|O_TRUNC,0664);
- if(dest==-1){
- puts("dest open failed");
- return -1;
- }
-
- //创建子进程,父进程拷贝前一半,子进程拷贝后一半
- int size=lseek(fd,0,SEEK_END);//获取总字节大小
- printf("size=%d\n",size);
- pid_t pid=fork();
- char c;
- //父进程
- if(pid>0){
- wait(NULL);
- lseek(fd,0,SEEK_SET);
- lseek(dest,0,SEEK_SET);
-
- puts("parent process copy start");
- for (int i=0; i
2; i++) - {
- read(fd,&c,1);
- write(dest,&c,1);
- }
- puts("parent process copy end");
- }
- else if(pid==0){
- lseek(fd,size/2,SEEK_SET);
- lseek(dest,size/2,SEEK_SET);
- puts("son process copy start");
- for (int i=size/2; i
- {
- read(fd,&c,1);
- write(dest,&c,1);
- }
- puts("son process copy end");
- }else{
- perror("fork");
- return -1;
- }
- puts("copy success");
-
- //关闭文件
- close(dest);
- close(fd);
- return 0;
- }
今日思维