diff --git a/commit.patch b/commit.patch new file mode 100644 index 0000000000000000000000000000000000000000..0ceccdcc0dece7d24231a2ed5360d6455fe2c421 --- /dev/null +++ b/commit.patch @@ -0,0 +1,302 @@ +diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json +new file mode 100644 +index 0000000..ee68b3d +--- /dev/null ++++ b/.vscode/c_cpp_properties.json +@@ -0,0 +1,17 @@ ++{ ++ "configurations": [ ++ { ++ "name": "Linux", ++ "includePath": [ ++ "${workspaceFolder}/**", ++ "${workspaceFolder}" ++ ], ++ "defines": [], ++ "compilerPath": "/usr/bin/gcc", ++ "cStandard": "c17", ++ "cppStandard": "gnu++17", ++ "intelliSenseMode": "linux-gcc-arm64" ++ } ++ ], ++ "version": 4 ++} +\ No newline at end of file +diff --git a/.vscode/settings.json b/.vscode/settings.json +new file mode 100644 +index 0000000..cfa3082 +--- /dev/null ++++ b/.vscode/settings.json +@@ -0,0 +1,6 @@ ++{ ++ "files.associations": { ++ "types.h": "c", ++ "user.h": "c" ++ } ++} +\ No newline at end of file +diff --git a/Makefile b/Makefile +index 323d089..dfd49cb 100644 +--- a/Makefile ++++ b/Makefile +@@ -157,6 +157,9 @@ UPROGS=\ + $U/_grind\ + $U/_wc\ + $U/_zombie\ ++ $U/_sleep\ ++ $U/_pingpong\ ++ $U/_find\ + + ifeq ($(LAB),syscall) + UPROGS += \ +diff --git a/user/command.gdb b/user/command.gdb +new file mode 100644 +index 0000000..5383d55 +--- /dev/null ++++ b/user/command.gdb +@@ -0,0 +1,6 @@ ++b exec ++b kernel/sysfile.c:384 ++c ++p cpus[$tp]->proc->name ++c ++p cpus[$tp]->proc->name +diff --git a/user/find.c b/user/find.c +new file mode 100644 +index 0000000..a5280d0 +--- /dev/null ++++ b/user/find.c +@@ -0,0 +1,105 @@ ++#include "kernel/types.h" ++#include "kernel/stat.h" ++#include "user/user.h" ++#include "kernel/fs.h" ++#include "kernel/fcntl.h" ++ ++// 函数声明 ++void find(const char *path, const char *name); ++ ++// 去除文件åå‰çš„路径,åªä¿ç•™æ–‡ä»¶å部分 ++char * ++fmtname(char *path) { ++ static char buf[DIRSIZ+1]; ++ char *p; ++ ++ // æ‰¾åˆ°è·¯å¾„ä¸æœ€åŽçš„ '/' å—符 ++ for(p = path + strlen(path); p >= path && *p != '/'; p--) ++ ; ++ p++; ++ ++ // æ‹·è´æœ€åŽçš„æ–‡ä»¶å部分 ++ if(strlen(p) >= DIRSIZ) ++ return p; ++ memmove(buf, p, strlen(p)); ++ buf[strlen(p)] = 0; ++ return buf; ++} ++ ++// 递归查找目录下的文件和目录 ++void ++find(const char *path, const char *name) { ++ char buf[512], *p; ++ int fd; ++ struct dirent de; ++ struct stat st; ++ ++ // 打开指定路径 ++ if((fd = open(path, O_RDONLY)) < 0){ ++ fprintf(2, "find: cannot open %s\n", path); ++ return; ++ } ++ ++ // èŽ·å–æ–‡ä»¶çš„状æ€ä¿¡æ¯ ++ if(fstat(fd, &st) < 0){ ++ fprintf(2, "find: cannot stat %s\n", path); ++ close(fd); ++ return; ++ } ++ ++ // **å¢žåŠ å½“å‰ç›®å½•åçš„åŒ¹é…æ£€æŸ¥** ++ if(strcmp(fmtname((char *)path), name) == 0) { ++ printf("%s\n", path); // 输出匹é…的路径 ++ } ++ ++ // 如果当å‰è·¯å¾„是目录,递归查找 ++ switch(st.type){ ++ case T_FILE: ++ // 如果是文件并且å称匹é…,已ç»è¾“出,跳过 ++ break; ++ ++ case T_DIR: ++ if(strlen(path) + 1 + DIRSIZ + 1 > sizeof(buf)){ ++ printf("find: path too long\n"); ++ break; ++ } ++ strcpy(buf, path); ++ p = buf + strlen(buf); ++ *p++ = '/'; ++ ++ // é历目录ä¸çš„æ¯ä¸ªæ–‡ä»¶æˆ–å目录 ++ while(read(fd, &de, sizeof(de)) == sizeof(de)){ ++ if(de.inum == 0) ++ continue; ++ ++ // 忽略 "." å’Œ ".." ++ if(strcmp(de.name, ".") == 0 || strcmp(de.name, "..") == 0) ++ continue; ++ ++ memmove(p, de.name, DIRSIZ); ++ p[DIRSIZ] = 0; ++ ++ // 递归查找å目录 ++ if(stat(buf, &st) < 0){ ++ printf("find: cannot stat %s\n", buf); ++ continue; ++ } ++ ++ find(buf, name); // 递归调用 ++ } ++ break; ++ } ++ close(fd); ++} ++ ++int ++main(int argc, char *argv[]) { ++ if(argc != 3){ ++ fprintf(2, "Usage: find <path> <name>\n"); ++ exit(1); ++ } ++ ++ find(argv[1], argv[2]); ++ ++ exit(0); ++} +\ No newline at end of file +diff --git a/user/pingpong.c b/user/pingpong.c +new file mode 100644 +index 0000000..7d9047d +--- /dev/null ++++ b/user/pingpong.c +@@ -0,0 +1,70 @@ ++#include "kernel/types.h" ++#include "kernel/stat.h" ++#include "user/user.h" ++ ++ ++int ++main(void) ++{ ++ int f2c[2]; // 父到åçš„ç®¡é“ ++ int c2f[2]; // ååˆ°çˆ¶çš„ç®¡é“ ++ char buf[1]; // ç”¨äºŽä¼ è¾“çš„ç¼“å†²åŒº ++ int ppid = getpid(); ++ int pid; ++ ++ // åˆ›å»ºä¸¤ä¸ªç®¡é“ ++ if (pipe(f2c) < 0 || pipe(c2f) < 0) { ++ fprintf(2, "pipe failed\n"); ++ exit(1); ++ } ++ ++ if ((pid = fork()) < 0) { ++ fprintf(2, "fork failed\n"); ++ exit(1); ++ } ++ ++ if (pid == 0) { // å进程 ++ close(f2c[1]); // å…³é—父到å的写端 ++ ++ // ä»Žçˆ¶è¿›ç¨‹è¯»å–æ•°æ® ++ if (read(f2c[0], buf, 1) != 1) { ++ fprintf(2, "child: read error\n"); ++ exit(1); ++ } ++ printf("%d: received ping from pid %d\n", getpid(), ppid); ++ ++ // å‘父进程å‘逿•°æ® ++ if (write(c2f[1], "p", 1) != 1) { ++ fprintf(2, "child: write error\n"); ++ exit(1); ++ } ++ ++ close(f2c[0]); // å…³é—父到å的读端 ++ close(c2f[1]); // å…³é—å到父的写端 ++ ++ } else { // 父进程 ++ close(f2c[0]); // å…³é—父到å的读端 ++ close(c2f[1]); // å…³é—å到父的写端 ++ ++ // å‘å进程å‘逿•°æ® ++ if (write(f2c[1], "p", 1) != 1) { ++ fprintf(2, "parent: write error\n"); ++ exit(1); ++ } ++ ++ // 从åè¿›ç¨‹è¯»å–æ•°æ® ++ if (read(c2f[0], buf, 1) != 1) { ++ fprintf(2, "parent: read error\n"); ++ exit(1); ++ } ++ printf("%d: received pong from pid %d\n", getpid(), pid); ++ ++ close(f2c[1]); // å…³é—父到å的写端 ++ close(c2f[0]); // å…³é—å到父的读端 ++ ++ // ç‰å¾…åè¿›ç¨‹ç»“æŸ ++ wait(0); ++ } ++ ++ exit(0); ++} +\ No newline at end of file +diff --git a/user/sleep.c b/user/sleep.c +index db812c4..7f6a0dd 100644 +--- a/user/sleep.c ++++ b/user/sleep.c +@@ -1 +1,17 @@ +-please change it! +\ No newline at end of file ++#include "user/user.h" ++ ++int ++main(int argc, char *argv[]) ++{ ++ if (argc != 2) { ++ fprintf(2, "Usage: sleep ticks\n"); ++ exit(1); ++ } ++ int ticks = atoi(argv[1]); ++ if (ticks < 0) { ++ fprintf(2, "sleep: invalid number of ticks\n"); ++ exit(1); ++ } ++ sleep(ticks); ++ exit(0); ++} +diff --git a/user/user.c b/user/user.c +new file mode 100644 +index 0000000..a95be7d +--- /dev/null ++++ b/user/user.c +@@ -0,0 +1,10 @@ ++#include "kernel/types.h" ++#include "user.h" ++#include "kernel/fcntl.h" ++#include "kernel/syscall.h" ++ ++int ++sleep(int ticks) ++{ ++ return syscall(SYS_sleep, ticks); ++} +diff --git a/user/user.h b/user/user.h +index 739d534..219e608 100644 +--- a/user/user.h ++++ b/user/user.h +@@ -1,3 +1,5 @@ ++#include "kernel/types.h" ++ + struct stat; + struct rtcdate; +