操作系统实验——文件复制
前期准备
-
安装gcc
编译器
-
md5sum
命令检查
-
解压linux
压缩包并获取程序运行的总时间
实验内容
1. c语言文件如下
#include
#include
#include
#include
#include
#include
#include
#include
typedef struct {
int threadId;
} threadParm_t;
pthread_mutex_t barrier_mutex;
void copyFile(char sourcePath[], char destinationPath[]) {
pthread_mutex_lock(&barrier_mutex);
char buffer[1024];
FILE *in, *out;
if ((in = fopen(sourcePath, "r")) == NULL) {
printf("源⽂件打开失败!\n");
exit(1);
}
if ((out = fopen(destinationPath, "w")) == NULL) {
printf("⽬标⽂件创建失败!\n");
exit(1);
}
int len;
while ((len = fread(buffer, 1, 1024, in)) > 0) {
fwrite(buffer, 1, len, out);
}
fclose(out);
fclose(in);
pthread_mutex_unlock(&barrier_mutex);
}
int is_dir(char *file_name) {
struct stat info;
stat(file_name, &info);
if (S_ISDIR(info.st_mode))
return 1;
else
return 0;
}
int endWith(char *s, char c) {
if (s[strlen(s) - 1] == c) {
return 1;
} else {
return 0;
}
}
void copyFolder(char *sourcePath, char *destinationPath) {
if (opendir(destinationPath) == NULL)
{
if (mkdir(destinationPath, 0777) == -1) {
printf("创建错误");
exit(-1);
}
}
DIR *src_dp = opendir(sourcePath);
struct dirent *ep_src;
char address[108] = {0};
char totalAddress[108] = {0};
while ((ep_src = readdir(src_dp)))
{
sprintf(address, "%s%s", sourcePath, "/");
sprintf(address, "%s%s", address, ep_src->d_name);
sprintf(totalAddress, "%s%s", destinationPath, "/");
sprintf(totalAddress, "%s%s", totalAddress, ep_src->d_name);
if (endWith(address, '.') == 1) {
continue;
}
else if ((is_dir(address) != 1)) {
copyFile(address, totalAddress);
}
else {
copyFolder(address, totalAddress);
}
memset(address, 108, 1);
memset(totalAddress, 108, 1);
}
}
char sourcePath[100][108];
char dstPath[100][108];
void *ThreadFunc(void *arg) {
threadParm_t *p = (threadParm_t *) arg;
int r = p->threadId;
char *source = sourcePath[r];
char *dst = dstPath[r];
printf("线程 %d 正在复制 %s 到 %s\n", r, source, dst);
copyFolder(sourcePath[r], dstPath[r]);
printf("线程 %d 复制成功\n", r);
pthread_exit(nullptr);
}
int main(int argc, char *argv[]) {
if (argc != 3)
{
printf("%s 缺少参数!!\n", argv[0]);
exit(1);
}
printf(" main function\n");
pthread_mutex_init(&barrier_mutex, NULL);
DIR *src_dp = opendir(argv[1]);
struct dirent *ep_src;
int thread_num = 0;
char address[108] = {0};
char totalAddress[108] = {0};
while ((ep_src = readdir(src_dp)))
{
sprintf(address, "%s%s", argv[1], "/");
sprintf(address, "%s%s", address, ep_src->d_name);
sprintf(totalAddress, "%s%s", argv[2], "/");
sprintf(totalAddress, "%s%s", totalAddress, ep_src->d_name);
if (endWith(address, '.') == 1) {
continue;
} else if (is_dir(address) != 1) {
copyFile(address, totalAddress);
continue;
}
strcpy(sourcePath[thread_num], address);
strcpy(dstPath[thread_num], totalAddress);
memset(address, 108, 1);
memset(totalAddress, 108, 1);
thread_num++;
}
pthread_t thread[thread_num];
threadParm_t threadParm[thread_num];
for (int i = 0; i < thread_num; i++) {
threadParm[i].threadId = i;
pthread_create(&thread[i], nullptr, ThreadFunc, (void *) &threadParm[i]);
}
for (int i = 0; i < thread_num; i++) {
pthread_join(thread[i], nullptr);
}
printf("此文件复制成功\n");
return 0;
}
2. 实验验证
-
生成目标程序
-
新建目标的空文件
- 开始复制
4. 复制成功
5. 开启验证,使用diff
命令进行验证
6. 验证之后无差别,实验成功