题目1:
思路只需要先用父进程拷贝前半部分,然后再用子进程拷贝后半部分即可,拷贝后半部分只用子进程去拷贝
头文件:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <dirent.h>
#include <time.h>
#include <fcntl.h>
代码:
int fd = open("/home/ubuntu/1.jpg",O_RDONLY);
int fa = open("./2.jpg",O_WRONLY|O_CREAT,775);
char res[10];
struct stat buf;
stat("/home/ubuntu/1.jpg",&buf);
off_t off = buf.st_size; //图片大小
ssize_t r;
for(int i=0;(off/2)-1;i++){
r = read(fd,res,sizeof(res));
if(r == 0){
break;
}
else if(r == -1){
perror("read");
return -1;
}
else {
write(fa,res,r);
}
}
pid_t pid = fork();
if(pid == 0){
for(int i=off/2;off;i++){
r = read(fd,res,sizeof(res));
if(r == 0){
break;
}
else if(r == -1){
perror("read");
return -1;
}
else {
write(fa,res,r);
}
}
}
效果:
编辑
编辑
题目二:
照着题目做就好了没什么特别的,用到stat,然后搞个localtime时间出来,不然得到的是秒数
代码:
DIR * dir = opendir("../文件io/");
FILE * fp = fopen("file.c","w");
if(fp == NULL){
perror("fp");
return -1;
}
if(dir == NULL){
perror("opendir");
return -1;
}
struct dirent *rp;
struct stat buf;
struct tm* time;
while(1){
rp = readdir(dir);
if(rp == NULL){
if(0 == errno){
printf("目录读取完毕\n");
break;
}
else
{
perror("readdir");
return -1;
}
}
if(rp->d_name[0] =='.'){
continue;
}
else{
stat(rp->d_name,&buf);
time = localtime(&buf.st_ctime);
fprintf(fp,"%s:%o %02d-%02d-%02d %d:%d:%d\n",rp->d_name,buf.st_mode,time->tm_year+1900,time->tm_mon+1,time->tm_mday,time->tm_hou
}
}
fclose(fp);
效果: