1 Star 1 Fork 0

小徐 / HITSZ-xv6-operating-system

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
commit.patch 7.25 KB
一键复制 编辑 原始数据 按行查看 历史
小徐 提交于 2024-03-10 14:38 . 生成了diff文件
diff --git a/Makefile b/Makefile
index cac799d..e4d4d19 100644
--- a/Makefile
+++ b/Makefile
@@ -154,6 +154,9 @@ UPROGS=\
$U/_grind\
$U/_wc\
$U/_zombie\
+ $U/_sleep\
+ $U/_pingpong\
+ $U/_find
ifeq ($(LAB),syscall)
diff --git a/kernel/main.c b/kernel/main.c
index 8a3dc2e..06aea69 100644
--- a/kernel/main.c
+++ b/kernel/main.c
@@ -10,6 +10,7 @@ volatile static int started = 0;
void main() {
if (cpuid() == 0) {
// consoleinit();
+ printf("[200110609] enter main, init kernel\n");
// printfinit();
printf("\n");
printf("xv6 kernel is booting\n");
diff --git a/kernel/proc.c b/kernel/proc.c
index 1607145..9354a3e 100644
--- a/kernel/proc.c
+++ b/kernel/proc.c
@@ -183,6 +183,7 @@ uchar initcode[] = {0x17, 0x05, 0x00, 0x00, 0x13, 0x05, 0x45, 0x02, 0x97, 0x05,
// Set up first user process.
void userinit(void) {
+ printf("[200110609] enter userinit\n");
struct proc *p;
p = allocproc();
@@ -197,6 +198,7 @@ void userinit(void) {
p->trapframe->epc = 0; // user program counter
p->trapframe->sp = PGSIZE; // user stack pointer
+ printf("[200110609] copy initcode to first user process\n");
safestrcpy(p->name, "initcode", sizeof(p->name));
p->cwd = namei("/");
diff --git a/kernel/start.c b/kernel/start.c
index f704fee..792eb99 100644
--- a/kernel/start.c
+++ b/kernel/start.c
@@ -52,8 +52,8 @@ void start() {
// init uart and printf
consoleinit();
printfinit();
+ printf("[200110609] in start, init driver, interrupts and change mode\n");
}
-
// switch to supervisor mode and jump to main().
asm volatile("mret");
}
diff --git a/user/find.c b/user/find.c
new file mode 100644
index 0000000..959c453
--- /dev/null
+++ b/user/find.c
@@ -0,0 +1,81 @@
+#include "kernel/types.h"
+#include "kernel/stat.h"
+#include "user/user.h"
+#include "kernel/fs.h"
+
+void find(char *path, char *fName);
+
+// 在目录树中查找名称与字符串匹配的所有文件,输出文件的相对路径。
+// 命令格式:find path file_name
+int main(int argc,char* argv[]){
+ if (argc != 3){
+ printf("usage:find dirName fileName");
+ exit(1);
+ }
+ find(argv[1],argv[2]);
+ exit(0);
+}
+
+// 关键的查找函数
+void find(char *path, char *fName){
+ char buffer[512], *p;
+ int fd;
+ struct dirent de;
+ struct stat st;
+
+ if((fd = open(path, 0)) < 0){ // 首先打开文件
+ fprintf(2, "find: cannot open %s\n", path);
+ return;
+ }
+ if(fstat(fd, &st) < 0){ // 获取文件描述符的信息
+ printf("find: cannot stat %s\n", path);
+ close(fd);
+ return;
+ }
+
+ if (st.type != T_DIR){ // 检测输入的dir是否为目录
+ printf("find:%s is not a directory\n", path);
+ close(fd);
+ return;
+ }
+
+ if(strlen(path) + 1 + DIRSIZ + 1 > sizeof buffer){ // 判断路径长度是否符合要求
+ printf("find: directory too long\n");
+ close(fd);
+ return;
+ }
+
+ strcpy(buffer,path); // 复制path到buffer
+ p = buffer + strlen(buffer); // 将p指针指向buffer的最后一位
+ *p = '/'; // 将最后一位字符赋值为‘/’
+ p++; // 指针指向下一位,用来接收之后的信息
+ // 从文件描述符(fd)对应的文件中读取数据,写到de中
+ while(read(fd, &de, sizeof(de)) == sizeof(de)){ // 读取目录下的信息
+ if(de.inum == 0)
+ continue;
+
+ // strcmp(),当两个字符串相等时,返回0
+ if(!strcmp(de.name,".") || !strcmp(de.name,"..")){ // 不要递归'.','..'
+ continue;
+ }
+
+ // memmove(),将de.name移动到p的位置
+ memmove(p, de.name, DIRSIZ);
+ p[DIRSIZ] = 0; // 在缓冲区buffer的末尾添加一个空字符,以确保字符串以空字符结尾。
+ if(stat(buffer, &st) < 0){
+ printf("find: cannot stat %s\n", buffer);
+ continue;
+ }
+ switch(st.type){ // 判断读入的信息为目录还是文件
+ case T_FILE:
+ if(!strcmp(de.name,fName)){
+ printf("%s\n",buffer);
+ }
+ break;
+ case T_DIR:
+ find(buffer,fName);
+ break;
+ }
+ }
+ close(fd);
+}
\ No newline at end of file
diff --git a/user/init.c b/user/init.c
index 9ca8790..02f3d65 100644
--- a/user/init.c
+++ b/user/init.c
@@ -23,13 +23,14 @@ int main(void) {
for (;;) {
printf("init: starting sh\n");
+ printf("[200110609] start sh through execve\n");
pid = fork();
if (pid < 0) {
printf("init: fork failed\n");
exit(1);
}
if (pid == 0) {
- exec("sh", argv);
+ exec("sh", argv); // 子进程执行shell程序,且不会返回此调用进程
printf("init: exec sh failed\n");
exit(1);
}
diff --git a/user/pingpong.c b/user/pingpong.c
new file mode 100644
index 0000000..bb96f26
--- /dev/null
+++ b/user/pingpong.c
@@ -0,0 +1,53 @@
+#include "kernel/types.h"
+#include "user.h" // 导入xv6自带的库函数
+
+int main(int argc,char* argv[]){
+ int p1[2], p2[2];
+ pipe(p1);
+ pipe(p2);
+ char buffer[4];
+ int size = 4;
+ // 管道创建完成之后,0为读,1为写
+
+ // // 方法一:使用两个pipe管道
+ // // 创建子进程
+ // if(fork() == 0){
+ // // 子进程,fork()返回0
+ // close(p1[1]); // 关闭子进程p1写端
+ // read(p1[0], buffer, size); // 读取
+ // printf("%d: received %s\n", getpid(),buffer);
+ // close(p1[0]);
+
+ // close(p2[0]); // 关闭子进程p2读端
+ // write(p2[1], "pong", strlen("pong")); // 写入
+ // close(p2[1]);
+ // exit(0);
+ // }else{
+ // // 父进程,fork()返回子进程的pid
+ // close(p1[0]); // 关闭父进程p1读端
+ // write(p1[1], "ping", strlen("ping")); // 写入
+ // close(p1[1]);
+
+ // close(p2[1]);
+ // read(p2[0], buffer, size);
+ // printf("%d: received %s\n", getpid(),buffer);
+ // close(p2[0]);
+ // exit(0);
+ // }
+
+ // 方法二:使用一个pipe管道
+ // 创建子进程
+ if(fork() == 0){
+ read(p1[0],buffer,size);
+ printf("%d: received %s\n", getpid(),buffer);
+ write(p1[1],"pong",strlen("pong"));
+ exit(0);
+ }
+ else{
+ write(p1[1],"ping",strlen("ping"));
+ wait(0); // 等待子进程完成读写操作
+ read(p1[0],buffer,size);
+ printf("%d: received %s\n",getpid(),buffer);
+ exit(0);
+ }
+}
\ No newline at end of file
diff --git a/user/sleep.c b/user/sleep.c
new file mode 100644
index 0000000..76ef77b
--- /dev/null
+++ b/user/sleep.c
@@ -0,0 +1,13 @@
+#include "kernel/types.h"
+#include "user.h" // 导入xv6自带的库函数
+
+int main(int argc,char* argv[]){
+ if(argc != 2){
+ printf("Sleep needs one argument!\n"); //检查参数数量是否正确
+ exit(-1);
+ }
+ int ticks = atoi(argv[1]); //将字符串参数转为整数
+ sleep(ticks); //使用系统调用sleep
+ printf("(nothing happens for a little while)\n");
+ exit(0); //确保进程退出
+}
\ No newline at end of file
1
https://gitee.com/xu-yingjie2021/New-hitsz-xv6-OperatingSystem.git
git@gitee.com:xu-yingjie2021/New-hitsz-xv6-OperatingSystem.git
xu-yingjie2021
New-hitsz-xv6-OperatingSystem
HITSZ-xv6-operating-system
util

搜索帮助