-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
commit c2e6081 Author: focused_xy <[email protected]> Date: Sun Oct 20 14:47:25 2024 +0800 perf: 优化lec3 commit 2269fb3 Author: M4yGem1ni <[email protected]> Date: Fri Oct 18 11:03:12 2024 +0800 修改: Lectures/Lecture3/Lecture3/index.html 重命名: Lectures/Lecture3/Lecture3/static/img/0cdbbaccab90465ba58c685485c2cd30.svg -> Lectures/Lecture3/Lecture3/static/img/28cab9315eb8415d91c72125ec5498f9.svg 新文件: Lectures/Lecture3/Lecture3/static/img/3821eff8ab1042fa84d270292f8447d3.png 新文件: Lectures/Lecture3/Lecture3/static/img/39d64db107574af69f5a5751958a99d3.png 新文件: Lectures/Lecture3/Lecture3/static/img/428ab8cd97b0484cb9a4151044b96995.png 重命名: Lectures/Lecture3/Lecture3/static/img/4f288e5a8a334bd8b4f74e798822dfd7.jpg -> Lectures/Lecture3/Lecture3/static/img/659edf639b8b4773a821480ae131503e.jpg 重命名: Lectures/Lecture3/Lecture3/static/img/730c1f2b36514d8ca1b5378bc4c615a1.svg -> Lectures/Lecture3/Lecture3/static/img/737c3e4cd73d45769f2ccbab4214edc2.svg 新文件: Lectures/Lecture3/Lecture3/static/img/7d095c82692149b6b75ae09e0110272e.png 新文件: Lectures/Lecture3/Lecture3/static/img/91b46e2025084cf3b01832a9f80ea78a.png 新文件: Lectures/Lecture3/Lecture3/static/img/9f46b159c5e64324a64c99543e0f9597.png 重命名: Lectures/Lecture3/Lecture3/static/img/c3ae71a350e34f17ae8aac4cea7c375e.svg -> Lectures/Lecture3/Lecture3/static/img/ce8b08f96ce3479da9b6596c9701c6e0.svg 新文件: Lectures/Lecture3/Lecture3/static/img/favicon.ico 删除: Lectures/Lecture3/Lecture3/static/img/favicon.png 修改: Lectures/Lecture3/lecture3.md 新文件: Lectures/Lecture3/static/AIRTH.png 新文件: Lectures/Lecture3/static/Adder.png 新文件: Lectures/Lecture3/static/Airth.png 新文件: Lectures/Lecture3/static/FA.png 新文件: Lectures/Lecture3/static/alu.png 新文件: Lectures/Lecture3/static/cpu.png 新文件: Notes/week3/static/AIRTH.png 新文件: Notes/week3/static/Adder.png 新文件: Notes/week3/static/Airth.png 新文件: Notes/week3/static/FA.png 新文件: Notes/week3/static/alu.png 新文件: Notes/week3/static/cpu.png 修改: Notes/week3/week3.md commit e3ea1e0 Author: focused_xy <[email protected]> Date: Thu Oct 17 15:32:32 2024 +0800 init: lec3 init commit fd136b3 Author: focused_xy <[email protected]> Date: Fri Oct 18 08:03:12 2024 +0800 init: lec3 note init commit b4cbce7 Author: focused_xy <[email protected]> Date: Fri Oct 18 08:00:17 2024 +0800 init: lec3 init
- Loading branch information
1 parent
1ea584b
commit db288bd
Showing
37 changed files
with
1,324 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
.DS_Store | ||
.vscode | ||
__pycache__ | ||
*.pyc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
CC := gcc | ||
CFLAGS := -Wall -Wextra -Werror -g -std=c11 | ||
TARGET := stack | ||
|
||
all: $(TARGET) | ||
|
||
$(TARGET): %:%.c | ||
$(CC) $(CFLAGS) -o $@ $< | ||
|
||
clean: | ||
-rm -rf $(TARGET) | ||
|
||
.PHONY: all clean |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
all: minimal static | ||
|
||
minimal.o: minimal.c | ||
gcc -c -o $@ $< | ||
|
||
minimal: minimal.o | ||
ld -o $@ $< | ||
|
||
minimal-asm: minimal | ||
objdump -d $< | ||
|
||
static: minimal.o | ||
gcc -static -o $@ $< | ||
|
||
static-asm: static | ||
objdump -d $< | ||
|
||
clean: | ||
-rm -f minimal.o minimal static | ||
|
||
.PHONY: all minimal-asm static-asm clean |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#include <sys/syscall.h> | ||
#include <unistd.h> | ||
|
||
const char str[] = "\033[01;31mHello, OS World\033[0m\n"; | ||
const int str_size = sizeof str - 1; | ||
|
||
int main(void) { | ||
asm volatile("movl %0, %%eax\n\t" // 系统调用号:SYS_write | ||
"movl $1, %%edi\n\t" // 文件描述符:STDOUT_FILENO (标准输出) | ||
"movq %2, %%rsi\n\t" // 字符串地址 | ||
"movl %3, %%edx\n\t" // 字符串长度 | ||
"syscall" // 调用系统调用 | ||
: | ||
: "r"(SYS_write), "r"(STDOUT_FILENO), "r"(str), "r"(str_size) | ||
: "eax", "edi", "rsi", "edx"); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include "os.h" | ||
|
||
/* | ||
In this model, we have a mini Operating System that can run multiple processes. | ||
Each process has a context that contains two variables: remainingStep and charToOutput. | ||
remainingStep is the number of steps that a process will run and then exit by doing a syscall EXIT. | ||
charToOutput is the character that a process will write to the console by doing a syscall WRITE. | ||
在这个模型中,我们模拟了一个简单的操作系统,可以运行多个进程. | ||
每个进程有一个上下文,包含两个变量:remainingStep 和 charToOutput. | ||
程序将运行,并不断发起系统调用,请求操作系统向终端中写入字符 charToOutput. | ||
在写入了 remainingStep 次后, 进程将会使用系统调用 SYS_EXIT 退出。 | ||
*/ | ||
|
||
|
||
void run() { | ||
Process *current; | ||
while (process_count()) { | ||
// The Operating System will randomly choose a process to run | ||
// 操作系统会随机选择一个进程来运行 | ||
current = process_schedule(); | ||
|
||
// Switch process context and run it until a syscall | ||
// 切换进程上下文并运行,直到发起系统调用 | ||
Syscall call = process_step(current); | ||
|
||
if (call.syscall == SYS_EXIT) { | ||
// Process exits | ||
// 进程退出 | ||
process_exit(current); | ||
} else if (call.syscall == SYS_WRITE) { | ||
// Write the character from syscall arg to the console | ||
// 将系统调用参数中的字符写入终端 | ||
putchar(call.arg); | ||
} | ||
} | ||
putchar('\n'); | ||
} | ||
|
||
int main() { | ||
// Initialize the Operating System | ||
// 初始化操作系统 | ||
init(); | ||
|
||
// spawn processes | ||
// 创建进程 | ||
spawn_process(5, 'A'); | ||
spawn_process(5, 'B'); | ||
spawn_process(5, 'C'); | ||
|
||
// start running | ||
// 开始运行 | ||
run(); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
CC=gcc | ||
CFLAGS=-Wall -Wextra -Werror -std=c11 | ||
TARGET=main | ||
|
||
$(TARGET): main.c os.c os.h | ||
$(CC) -o $(TARGET) main.c os.c $(CFLAGS) | ||
|
||
all: $(TARGET) | ||
|
||
run: $(TARGET) | ||
./$(TARGET) | ||
|
||
clean: | ||
rm -f $(TARGET) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#include "os.h" | ||
#include <stdlib.h> | ||
#include <time.h> | ||
|
||
void init() { | ||
srand(time(NULL)); | ||
} | ||
|
||
Process procArray[MAX_PROC]; | ||
Process *runningProcess[MAX_PROC]; | ||
|
||
unsigned int procCount = 0; | ||
|
||
int process_count() { | ||
return procCount; | ||
} | ||
|
||
int spawn_process(int var1, char var2) { | ||
if (procCount >= MAX_PROC) { | ||
return -1; | ||
} else { | ||
// Initialize process | ||
Process *proc = &procArray[procCount]; | ||
proc->remainingStep = var1; // A process will run var1 step(s) | ||
proc->charToOutput = var2; // A process will write this character to console | ||
runningProcess[procCount] = proc; | ||
procCount ++; | ||
return 0; | ||
} | ||
} | ||
|
||
Process *process_schedule() { | ||
int i = rand() % procCount; | ||
return runningProcess[i]; | ||
} | ||
|
||
Syscall process_step(Process *proc) { | ||
Syscall call; | ||
if (proc->remainingStep > 0) { | ||
proc->remainingStep --; | ||
call.syscall = SYS_WRITE; | ||
call.arg = proc->charToOutput; | ||
} else { | ||
call.syscall = SYS_EXIT; | ||
call.arg = 0; | ||
} | ||
return call; | ||
} | ||
|
||
void process_exit(Process *proc) { | ||
for (unsigned int i = 0; i < procCount; i++) { | ||
if (runningProcess[i] == proc) { | ||
for (unsigned int j = i; j < procCount - 1; j++) { | ||
runningProcess[j] = runningProcess[j + 1]; | ||
} | ||
runningProcess[procCount - 1] = NULL; | ||
procCount --; | ||
break; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#ifndef __OS_H__ | ||
#define __OS_H__ | ||
|
||
#define MAX_PROC 4 | ||
|
||
typedef struct { | ||
int remainingStep; // A process will run remainingStep steps | ||
char charToOutput; // A process will write this character to console by doing a syscall | ||
} Process; // Process's Context | ||
|
||
enum { | ||
SYS_EXIT, // Process exits | ||
SYS_WRITE, // Write to console with a character | ||
SYS_EXEC, // Execute a new process with a character | ||
}; | ||
// If you know what we are doing, you can implement the exec syscall by yourself | ||
// You can also implement other syscalls if you want | ||
// You can do this as a lab exercise | ||
|
||
typedef struct { | ||
unsigned int syscall; | ||
char arg; | ||
} Syscall; | ||
|
||
void init(); | ||
int process_count(); | ||
int spawn_process(int var1, char var2); | ||
Process *process_schedule(); | ||
Syscall process_step(Process *proc); | ||
void process_exit(Process *proc); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
int main(void) { | ||
int x = 1; | ||
void *y = malloc(sizeof(int)); | ||
printf("stack: %p\n", (void *)&x); | ||
printf("heap : %p\n", y); | ||
free(y); | ||
return 0; | ||
} |
Oops, something went wrong.