Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Origin/ddd #132

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions 003_copy/jiang-yuntao/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
all: mycp
./mycp ../README.md file

mycp: mycp.c
gcc -o $@ $<

clean:
rm -rf mycp

.PHONY:all
.PHONY:clean
17 changes: 17 additions & 0 deletions 003_copy/jiang-yuntao/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
### 1. 如果 `open` 或者另外几个相关的系统调用执行出错,,你如何找出出错的原因?

答:通过gdb调试或者man 2查看相关文档看错误信息。

### 2. 你觉得学习本篇有什么困难?

答: 还行,自己做起来有点照猫画虎。

### 3. 你觉得掌握文件 IO 还能做些什么事情?

答:能够更加精确的实现文件的读写操作,许多类似的问题可以更好解决。

### 4. 在你的终端执行命令 `ps`,你会看到你当前 bash 的 PID 号(第一行),记下这个 PID,然后再执行 `lsof -p ${PID}`,注意了,请把 ${PID} 替换成你看到的 PID。接下来你会
看到 `lsof` 执行的输出结果。描述你看到的现象,你发现了什么?

答: 看到两个PID,一个是bash,一个是ps;
会显示相关的信息进程信息,会显示目录文件描述符等相关信息。
37 changes: 37 additions & 0 deletions 003_copy/jiang-yuntao/mycp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include<unistd.h>
#include<stdio.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/stat.h>
#define MAXSIZE 4096

int main(int argc, char* argv[]) {
int src = open(argv[1], O_RDONLY);
if (src == -1) {
printf("Error : reading the file\n");
perror("Error ");
return -1;
}
int dst = open(argv[2], O_WRONLY|O_CREAT, 0666);
if (dst == -1) {
printf("Error : creating the file\n");
perror("Error");
close(src);
return -1;
}
char buffer[MAXSIZE] = "";
int len = 0;
while ((len = read(src, buffer, MAXSIZE)) > 0) {
if (write(dst, buffer, len) != len) {
perror("Error copying file!\n");
break;
}
}
if (len == 0)
printf("End copy!\n");
else if (len < 0)
perror("read file error!\n");
close(dst);
close(src);
return 0;
}