├── README.md
├── lab1
├── README.md
├── bin
│ ├── hello_world.exe
│ ├── test_exec.exe
│ ├── test_pid.exe
│ ├── test_system.exe
│ ├── thread1.exe
│ └── thread2.exe
├── images
│ ├── process
│ │ ├── 1.png
│ │ ├── 2.png
│ │ ├── 3.png
│ │ ├── 4.png
│ │ ├── exec1.png
│ │ ├── exec2.png
│ │ ├── exec3.png
│ │ ├── exec4.png
│ │ ├── sys1.png
│ │ ├── sys2.png
│ │ ├── sys3.png
│ │ └── sys4.png
│ └── thread
│ │ ├── 1.png
│ │ ├── 10.png
│ │ ├── 11.png
│ │ ├── 12.png
│ │ ├── 13.png
│ │ ├── 14.png
│ │ ├── 15.png
│ │ ├── 16.png
│ │ ├── 17.png
│ │ ├── 2.png
│ │ ├── 3.png
│ │ ├── 4.png
│ │ ├── 5.png
│ │ ├── 6.png
│ │ ├── 7.png
│ │ ├── 8.png
│ │ ├── 9.png
│ │ ├── exec.png
│ │ └── sys.png
├── process
│ ├── hello_world.c
│ ├── test_exec.c
│ ├── test_pid.c
│ └── test_system.c
└── thread
│ ├── test_thread1.c
│ └── test_thread2.c
├── lab2
├── README.md
├── images
│ ├── mem
│ │ ├── alloc1.png
│ │ ├── alloc10.png
│ │ ├── alloc11.png
│ │ ├── alloc12.png
│ │ ├── alloc13.png
│ │ ├── alloc14.png
│ │ ├── alloc15.png
│ │ ├── alloc16.png
│ │ ├── alloc17.png
│ │ ├── alloc18.png
│ │ ├── alloc2.png
│ │ ├── alloc3.png
│ │ ├── alloc4.png
│ │ ├── alloc5.png
│ │ ├── alloc6.png
│ │ ├── alloc7.png
│ │ ├── alloc8.png
│ │ └── alloc9.png
│ ├── pipe
│ │ ├── pipe_with_lock.png
│ │ └── pipe_without_lock.png
│ └── sig
│ │ ├── sig_alarm1.png
│ │ ├── sig_alarm2.png
│ │ ├── sig_quit1.png
│ │ └── sig_quit2.png
├── memory
│ ├── .gitignore
│ ├── CMakeLists.txt
│ ├── Example.md
│ ├── include
│ │ ├── Allocator.h
│ │ └── VarSizeAllocMngr.h
│ └── src
│ │ ├── Allocator.cpp
│ │ ├── VarSizeAllocMngr.cpp
│ │ └── main.cpp
├── pipe
│ ├── pipe.c
│ ├── pipe.exe
│ ├── pipe_lock.c
│ ├── pipe_lock.exe
│ └── shared.txt
└── sig
│ ├── sig_receive.c
│ └── sig_receive.exe
└── lab3
├── CTP
├── .gitignore
├── CMakeLists.txt
├── Core
│ ├── CMakeLists.txt
│ ├── include
│ │ └── CTP
│ │ │ ├── ThreadPool.hpp
│ │ │ ├── details
│ │ │ └── ThreadPool.inl
│ │ │ └── function2
│ │ │ └── function2.hpp
│ └── src
│ │ └── ThreadPool.cpp
├── README.md
└── Test
│ ├── CMakeLists.txt
│ ├── include
│ ├── test.h
│ └── test_func.h
│ └── test.cpp
├── FileSystem
├── .clang-format
├── .gitignore
├── CMakeLists.txt
├── include
│ ├── Ext2.h
│ ├── Log.h
│ ├── Shell.h
│ └── Test.h
└── src
│ ├── Ext2.cpp
│ ├── Shell.cpp
│ └── main.cpp
├── README.md
└── images
├── ctp
├── 0.png
├── 1.png
├── a.png
├── ans.png
├── ans0.png
├── err.png
├── res0.png
├── res1.png
└── xxx.png
└── fs
├── entry.png
├── ext2.png
├── impl0.png
├── impl1.png
├── inode.png
├── loc.png
├── run.png
└── str.png
/README.md:
--------------------------------------------------------------------------------
1 | # OS_Lab
--------------------------------------------------------------------------------
/lab1/README.md:
--------------------------------------------------------------------------------
1 | # OS_Lab1
2 |
3 | ## Part 1 Process
4 |
5 | ---
6 |
7 | 运行课本 p103 3.7 的程序,结果如下
8 |
9 |
10 |
11 | 可以看出父进程和子进程是并发执行的,在父进程中 `fork()` 的返回值是子进程的pid,而在子进程中 `fork()` 的返回值是0
12 |
13 | 去除 `wait(NULL)` 后,并让父进程 `sleep(20)`, 然后运行程序,可以看到,子进程运行结束后由于父进程没有执行 `wait()`, 因此其变成了僵尸进程(Z+)
14 |
15 |
16 |
17 | ---
18 |
19 | 在程序中添加一个全局变量
20 | ```cpp
21 | int global = 0;
22 | ```
23 |
24 | 在父进程中
25 | ```cpp
26 | global += 2;
27 | ```
28 |
29 | 在子进程中
30 | ```cpp
31 | global += 1;
32 | ```
33 |
34 | 并打印输出 `global` 的值如下:
35 |
36 |
37 |
38 | 可以看到父进程和子进程中的值并不相同,即这两个 `global` 并不是同一个变量。但是它们的地址却一致,这是因为此地址仅为 `global` 在其各自的进程地址空间内的相对地址。而子进程是从父进程 `fork()` 而来的,因此他们的 `global` 的相对地址也一致.
39 |
40 | ---
41 |
42 | 在 `return` 前对 `global` 操作
43 | ```cpp
44 | global += 1;
45 | printf("final: global = %d\n", global);
46 | ```
47 |
48 |
49 |
50 | 可以看出父子进程分别对其各自的变量进行了 +1 操作.
51 |
52 | ---
53 |
54 | ### **exec**
55 |
56 | 在子进程中开始处调用 `execl("./bin/hello_world.exe",NULL);`, 此处的 `"./bin/hello_world.exe"` 的作用是输出 helloworld 和进程的 pid 值.
57 |
58 |
59 | 结果如下:
60 |
61 |
62 |
63 | 可以看到子进行直接去执行了 `hello_world.exe`,不会执行源程序中打印 pid 的部分.
64 |
65 | 如果我们换成在子进程的末尾处调用,则:
66 |
67 |
68 | 结果如下:
69 |
70 |
71 |
72 | 则子进程会把原来进程中打印 pid 的部分执行完再去执行 `hello_world.exe`, 这两项工作都是在同一个进程(子进程)中进行的(两次打印的 pid 值相同)
73 |
74 | ---
75 |
76 | ### **system**
77 |
78 | 同 exec,在子进行的开始处调用 `system("./bin/hello_world.exe");`, `hello_world.exe`的作用同上。
79 |
80 |
81 | 结果如下:
82 |
83 |
84 |
85 | 可以看到子进程先执行了 `hello_world.exe`,在执行了源代码中的打印 pid 值。不同之处在于, `system(...)` 内实际上调用了一次 `fork()`,也就是子进程创建出来一个自己的子进程,父进程的"子子进程"去执行 `hello_world.exe` (因为 `hello_world.exe` 中输出的 pid 不同于 子进程执行源程序时输出的 pid).
86 |
87 | 把 `system("./bin/hello_world.exe");` 放到子进程末尾
88 |
89 |
90 | 结果如下:
91 |
92 |
93 |
94 | 区别不大,仅仅是 `hello_world` 和打印 pid 的顺序变了而已, 这是理所当然的.
95 |
96 | ---
97 |
98 | ## Part 2 Thread
99 |
100 | ---
101 | 创建两个线程,并在其中对全局变量 `sum` 执行 5000 次不同操作
102 |
103 |
104 |
105 |
106 |
107 | 调用 `pthread_join(tid, NULL)` 对两个线程进行阻塞等待,并在结束后分别打印 `sum` 的值,结果如下:
108 |
109 |
110 |
111 | 可以看到 `sum` 在有时候并没有按预期被加到 15000.
112 | 再将程序修改为在两个线程都结束后再打印 `sum`.
113 |
114 |
115 |
116 | 运行结果还是在有时候加不到 15000.
117 |
118 |
119 |
120 | 初步分析原因在于两个线程都要修改全局变量 `sum`, 即就是进入临界区,但是由于没有进行互斥,因此对 `sum` 的操作有些并没有生效. 因此考虑给两个线程加锁如下:
121 |
122 |
123 |
124 | 再次运行后可以看到每次输出都是两个 15000 了
125 |
126 |
127 |
128 | ---
129 |
130 | 修改 Part1 中 exec 和 system 部分的代码改为在线程中执行.
131 |
132 | ### exec
133 |
134 |
135 |
136 | 线程中执行以下代码,输出 tid 值, 然后执行进程切换去执行 `hello_world.exe`, 其作用是输出 pid 和 hello_world.
137 |
138 |
139 |
140 | 运行结果如下, 可以看到子进程创建了一个线程.
141 |
142 |
143 |
144 | 同 Part1,我们把线程创建和执行放到源程序的开头.
145 |
146 |
147 |
148 | 运行结果如下, 可以看到在子进程的线程中进行了进程切换, 因此源代码中两个打印 `pid` 的内容就不会被执行了.
149 |
150 |
151 |
152 | ---
153 |
154 | ### system
155 |
156 | 同 `exec`. 在子进程中创建一个线程.
157 |
158 |
159 |
160 | 线程中执行以下代码.
161 |
162 |
163 |
164 | 运行结果如下, 可以看到在子进程中进行了 `system(...)` 的调用后, 子进程调用 `fork()` 创建了一个自己的子进程,然后让这进程执行进程切换去执行 `hello_world.exe`. 可以看到在 `hello_world.exe` 中输出的 pid 和 子进程中 pid 并不相同.
165 |
166 |
167 |
168 | 同 Part1,我们把线程创建和执行放到源程序的开头. 但对于 `system()` 来说,并不会像 `exec()` 一样,只是输出的顺序改变了.
169 |
170 |
171 |
172 |
173 |
174 | ---
175 |
176 | ## Part 3 遇到的问题
177 |
178 | 在使用 vscode-remote 连接华为云时, 连接失败. 查看 vscode-remote 的输出日志可以看出一直在报这句错:
179 |
180 | ```shell
181 | open failed: administratively prohibited: open failed
182 | ```
183 |
184 | 最后根据这篇 StackOverflow 上的回答解决了.
185 |
186 | [vs-code-remote-ssh-failed-to-set-up-socket-other-clients-are-ok.](https://stackoverflow.com/questions/64941796/vs-code-remote-ssh-failed-to-set-up-socket-other-clients-are-ok)
187 |
188 | ---
189 |
190 | ## Part 4 分析和思考
191 |
192 | * 计算机科学与技术即重科学也重技术,在课本上学来的理论知识,只有通过实际 Code 才能深入理解,掌握其用法
193 |
194 | * 多线程可以充分利用 CPU 资源,可以使得有及时的响应不至于让用户一直进行等待, 无论是在 Graphics 领域加速运算还是在Web开发中处理传来的 request 并给出相应的 response 都有很大的作用。但是在 Part2 中可以看到多线程在没有考虑互斥和同步或者考虑不当的情况下往往会出现意想不到的结果,而且难以调试。即使现在有很多多线程的库,但在使用多线程时还是要多加小心,合适处理同步和互斥。
195 |
196 | ---
197 |
198 | ## Part 5 Score
199 |
200 | 自评分: 20
201 |
202 |
203 |
--------------------------------------------------------------------------------
/lab1/bin/hello_world.exe:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YichenWu11/OS_Lab/6d7322bb59542aa0c2b72f62dcc41bc53d288417/lab1/bin/hello_world.exe
--------------------------------------------------------------------------------
/lab1/bin/test_exec.exe:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YichenWu11/OS_Lab/6d7322bb59542aa0c2b72f62dcc41bc53d288417/lab1/bin/test_exec.exe
--------------------------------------------------------------------------------
/lab1/bin/test_pid.exe:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YichenWu11/OS_Lab/6d7322bb59542aa0c2b72f62dcc41bc53d288417/lab1/bin/test_pid.exe
--------------------------------------------------------------------------------
/lab1/bin/test_system.exe:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YichenWu11/OS_Lab/6d7322bb59542aa0c2b72f62dcc41bc53d288417/lab1/bin/test_system.exe
--------------------------------------------------------------------------------
/lab1/bin/thread1.exe:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YichenWu11/OS_Lab/6d7322bb59542aa0c2b72f62dcc41bc53d288417/lab1/bin/thread1.exe
--------------------------------------------------------------------------------
/lab1/bin/thread2.exe:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YichenWu11/OS_Lab/6d7322bb59542aa0c2b72f62dcc41bc53d288417/lab1/bin/thread2.exe
--------------------------------------------------------------------------------
/lab1/images/process/1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YichenWu11/OS_Lab/6d7322bb59542aa0c2b72f62dcc41bc53d288417/lab1/images/process/1.png
--------------------------------------------------------------------------------
/lab1/images/process/2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YichenWu11/OS_Lab/6d7322bb59542aa0c2b72f62dcc41bc53d288417/lab1/images/process/2.png
--------------------------------------------------------------------------------
/lab1/images/process/3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YichenWu11/OS_Lab/6d7322bb59542aa0c2b72f62dcc41bc53d288417/lab1/images/process/3.png
--------------------------------------------------------------------------------
/lab1/images/process/4.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YichenWu11/OS_Lab/6d7322bb59542aa0c2b72f62dcc41bc53d288417/lab1/images/process/4.png
--------------------------------------------------------------------------------
/lab1/images/process/exec1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YichenWu11/OS_Lab/6d7322bb59542aa0c2b72f62dcc41bc53d288417/lab1/images/process/exec1.png
--------------------------------------------------------------------------------
/lab1/images/process/exec2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YichenWu11/OS_Lab/6d7322bb59542aa0c2b72f62dcc41bc53d288417/lab1/images/process/exec2.png
--------------------------------------------------------------------------------
/lab1/images/process/exec3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YichenWu11/OS_Lab/6d7322bb59542aa0c2b72f62dcc41bc53d288417/lab1/images/process/exec3.png
--------------------------------------------------------------------------------
/lab1/images/process/exec4.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YichenWu11/OS_Lab/6d7322bb59542aa0c2b72f62dcc41bc53d288417/lab1/images/process/exec4.png
--------------------------------------------------------------------------------
/lab1/images/process/sys1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YichenWu11/OS_Lab/6d7322bb59542aa0c2b72f62dcc41bc53d288417/lab1/images/process/sys1.png
--------------------------------------------------------------------------------
/lab1/images/process/sys2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YichenWu11/OS_Lab/6d7322bb59542aa0c2b72f62dcc41bc53d288417/lab1/images/process/sys2.png
--------------------------------------------------------------------------------
/lab1/images/process/sys3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YichenWu11/OS_Lab/6d7322bb59542aa0c2b72f62dcc41bc53d288417/lab1/images/process/sys3.png
--------------------------------------------------------------------------------
/lab1/images/process/sys4.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YichenWu11/OS_Lab/6d7322bb59542aa0c2b72f62dcc41bc53d288417/lab1/images/process/sys4.png
--------------------------------------------------------------------------------
/lab1/images/thread/1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YichenWu11/OS_Lab/6d7322bb59542aa0c2b72f62dcc41bc53d288417/lab1/images/thread/1.png
--------------------------------------------------------------------------------
/lab1/images/thread/10.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YichenWu11/OS_Lab/6d7322bb59542aa0c2b72f62dcc41bc53d288417/lab1/images/thread/10.png
--------------------------------------------------------------------------------
/lab1/images/thread/11.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YichenWu11/OS_Lab/6d7322bb59542aa0c2b72f62dcc41bc53d288417/lab1/images/thread/11.png
--------------------------------------------------------------------------------
/lab1/images/thread/12.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YichenWu11/OS_Lab/6d7322bb59542aa0c2b72f62dcc41bc53d288417/lab1/images/thread/12.png
--------------------------------------------------------------------------------
/lab1/images/thread/13.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YichenWu11/OS_Lab/6d7322bb59542aa0c2b72f62dcc41bc53d288417/lab1/images/thread/13.png
--------------------------------------------------------------------------------
/lab1/images/thread/14.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YichenWu11/OS_Lab/6d7322bb59542aa0c2b72f62dcc41bc53d288417/lab1/images/thread/14.png
--------------------------------------------------------------------------------
/lab1/images/thread/15.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YichenWu11/OS_Lab/6d7322bb59542aa0c2b72f62dcc41bc53d288417/lab1/images/thread/15.png
--------------------------------------------------------------------------------
/lab1/images/thread/16.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YichenWu11/OS_Lab/6d7322bb59542aa0c2b72f62dcc41bc53d288417/lab1/images/thread/16.png
--------------------------------------------------------------------------------
/lab1/images/thread/17.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YichenWu11/OS_Lab/6d7322bb59542aa0c2b72f62dcc41bc53d288417/lab1/images/thread/17.png
--------------------------------------------------------------------------------
/lab1/images/thread/2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YichenWu11/OS_Lab/6d7322bb59542aa0c2b72f62dcc41bc53d288417/lab1/images/thread/2.png
--------------------------------------------------------------------------------
/lab1/images/thread/3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YichenWu11/OS_Lab/6d7322bb59542aa0c2b72f62dcc41bc53d288417/lab1/images/thread/3.png
--------------------------------------------------------------------------------
/lab1/images/thread/4.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YichenWu11/OS_Lab/6d7322bb59542aa0c2b72f62dcc41bc53d288417/lab1/images/thread/4.png
--------------------------------------------------------------------------------
/lab1/images/thread/5.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YichenWu11/OS_Lab/6d7322bb59542aa0c2b72f62dcc41bc53d288417/lab1/images/thread/5.png
--------------------------------------------------------------------------------
/lab1/images/thread/6.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YichenWu11/OS_Lab/6d7322bb59542aa0c2b72f62dcc41bc53d288417/lab1/images/thread/6.png
--------------------------------------------------------------------------------
/lab1/images/thread/7.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YichenWu11/OS_Lab/6d7322bb59542aa0c2b72f62dcc41bc53d288417/lab1/images/thread/7.png
--------------------------------------------------------------------------------
/lab1/images/thread/8.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YichenWu11/OS_Lab/6d7322bb59542aa0c2b72f62dcc41bc53d288417/lab1/images/thread/8.png
--------------------------------------------------------------------------------
/lab1/images/thread/9.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YichenWu11/OS_Lab/6d7322bb59542aa0c2b72f62dcc41bc53d288417/lab1/images/thread/9.png
--------------------------------------------------------------------------------
/lab1/images/thread/exec.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YichenWu11/OS_Lab/6d7322bb59542aa0c2b72f62dcc41bc53d288417/lab1/images/thread/exec.png
--------------------------------------------------------------------------------
/lab1/images/thread/sys.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YichenWu11/OS_Lab/6d7322bb59542aa0c2b72f62dcc41bc53d288417/lab1/images/thread/sys.png
--------------------------------------------------------------------------------
/lab1/process/hello_world.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | #include
5 |
6 | int main() {
7 | printf("\nHello World\n");
8 |
9 | pid_t pid;
10 |
11 | pid = getpid();
12 | printf("hello_world: pid = %d\n\n",pid); /* C */
13 |
14 | return 0;
15 | }
16 |
--------------------------------------------------------------------------------
/lab1/process/test_exec.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | #include
5 |
6 | int main()
7 | {
8 | pid_t pid, pid1;
9 |
10 | /* fork a child process */
11 | pid = fork();
12 | if (pid < 0) {
13 | /* error occurred */
14 | fprintf(stderr, "Fork Failed");
15 | return 1;
16 | }
17 | else if (pid == 0) { /* child process */
18 | pid1 = getpid();
19 | printf("child: pid = %d\n",pid); /* A */
20 | printf("child: pid1 = %d\n",pid1); /* B */
21 | execl("../bin/hello_world.exe",NULL);
22 | }
23 | else { /* parent process */
24 | pid1 = getpid();
25 | printf("parent: pid = %d\n",pid); /* C */
26 | printf("parent: pid1 = %d\n",pid1); /* D */
27 | wait(NULL);
28 | }
29 |
30 | return 0;
31 | }
32 |
--------------------------------------------------------------------------------
/lab1/process/test_pid.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | #include
5 |
6 | int global = 0;
7 |
8 | int main()
9 | {
10 | pid_t pid, pid1;
11 |
12 | /* fork a child process */
13 | pid = fork();
14 | if (pid < 0) {
15 | /* error occurred */
16 | fprintf(stderr, "Fork Failed");
17 | return 1;
18 | }
19 | else if (pid == 0) { /* child process */
20 | pid1 = getpid();
21 | printf("child: pid = %d\n",pid); /* A */
22 | printf("child: pid1 = %d\n",pid1); /* B */
23 | global += 1;
24 | printf("child: global = 1\n");
25 | printf("child: global_address = %x\n", &global);
26 | }
27 | else { /* parent process */
28 | pid1 = getpid();
29 | printf("parent: pid = %d\n",pid); /* C */
30 | printf("parent: pid1 = %d\n",pid1); /* D */
31 | global += 2;
32 | printf("parent: global = 2\n");
33 | printf("parent: global_address = %x\n", &global);
34 | wait(NULL);
35 | }
36 |
37 | global += 1;
38 | printf("final: global = %d\n", global);
39 |
40 | return 0;
41 | }
42 |
--------------------------------------------------------------------------------
/lab1/process/test_system.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | #include
5 |
6 | int main()
7 | {
8 | pid_t pid, pid1;
9 |
10 | /* fork a child process */
11 | pid = fork();
12 | if (pid < 0) {
13 | /* error occurred */
14 | fprintf(stderr, "Fork Failed");
15 | return 1;
16 | }
17 | else if (pid == 0) { /* child process */
18 | pid1 = getpid();
19 | printf("child: pid = %d\n",pid); /* A */
20 | printf("child: pid1 = %d\n",pid1); /* B */
21 | system("../bin/hello_world.exe");
22 | }
23 | else { /* parent process */
24 | pid1 = getpid();
25 | printf("parent: pid = %d\n",pid); /* C */
26 | printf("parent: pid1 = %d\n",pid1); /* D */
27 | wait(NULL);
28 | }
29 |
30 | return 0;
31 | }
32 |
--------------------------------------------------------------------------------
/lab1/thread/test_thread1.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 |
5 | pthread_mutex_t mutex;
6 |
7 | int sum; /* this data is shared by the thread(s) */
8 |
9 | void *func1(void *); /* the thread1 */
10 | void *func2(void *); /* the thread2 */
11 |
12 | int main() {
13 | pthread_mutex_init(&mutex, NULL);
14 |
15 | pthread_t tid1, tid2; /* the thread identifier */
16 | pthread_attr_t attr1, attr2; /* set of attributes for the thread */
17 |
18 | /* get the default attributes */
19 | pthread_attr_init(&attr1);
20 | pthread_attr_init(&attr2);
21 |
22 | /* create the thread */
23 | pthread_create(&tid1, &attr1, func1, NULL);
24 | pthread_create(&tid2, &attr2, func2, NULL);
25 |
26 | /* now wait for the thread to exit */
27 | // 第二个参数表示接收到的返回值
28 | pthread_join(tid1,NULL);
29 | printf("sum = %d\n",sum);
30 |
31 | pthread_join(tid2,NULL);
32 | printf("sum = %d\n",sum);
33 |
34 | pthread_mutex_destroy(&mutex);
35 |
36 | return 0;
37 | }
38 |
39 | void *func1(void *params) {
40 |
41 | for (int _i = 0; _i < 5000; _i++) {
42 | pthread_mutex_lock(&mutex);
43 | sum++;
44 | pthread_mutex_unlock(&mutex);
45 | }
46 |
47 | pthread_exit(0);
48 | }
49 |
50 | void *func2(void *params) {
51 | for (int _i = 0; _i < 5000; _i++) {
52 | pthread_mutex_lock(&mutex);
53 | sum += 2;
54 | pthread_mutex_unlock(&mutex);
55 | }
56 |
57 | pthread_exit(0);
58 | }
59 |
--------------------------------------------------------------------------------
/lab1/thread/test_thread2.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | #include
5 | #include
6 | #include
7 |
8 | void *func(void *);
9 |
10 | int main()
11 | {
12 | pid_t pid, pid1;
13 |
14 | /* fork a child process */
15 | pid = fork();
16 | if (pid < 0) {
17 | /* error occurred */
18 | fprintf(stderr, "Fork Failed");
19 | return 1;
20 | }
21 | else if (pid == 0) { /* child process */
22 | // exec
23 | pthread_t tid;
24 | pthread_attr_t attr;
25 |
26 | pthread_attr_init(&attr);
27 |
28 | pthread_create(&tid, &attr, func, NULL);
29 |
30 | pthread_join(tid,NULL);
31 |
32 | pid1 = getpid();
33 | printf("child: pid = %d\n",pid); /* A */
34 | printf("child: pid1 = %d\n",pid1); /* B */
35 | }
36 | else { /* parent process */
37 | pid1 = getpid();
38 | printf("parent: pid = %d\n",pid); /* C */
39 | printf("parent: pid1 = %d\n",pid1); /* D */
40 | wait(NULL);
41 | }
42 |
43 | return 0;
44 | }
45 |
46 | void *func(void *params) {
47 | pthread_t tid = pthread_self();
48 | printf("Inner thread: tid = %ld\n", tid);
49 | // execlp("../bin/hello_world.exe", NULL);
50 | system("../bin/hello_world.exe");
51 |
52 | pthread_exit(0);
53 | }
54 |
--------------------------------------------------------------------------------
/lab2/README.md:
--------------------------------------------------------------------------------
1 | # OS_Lab2
2 |
3 | ## Part1 IPC
4 |
5 | ---
6 |
7 | ### 软中断通信
8 |
9 | 编写程序,在父进程中
10 | ```cpp
11 | signal(SIGALRM, father_handler); // 14
12 | signal(SIGQUIT, father_handler); // 3
13 | alarm(5);
14 | ```
15 |
16 | 子进程中
17 | ```cpp
18 | /* child 2 process*/
19 | signal(SIGUSR2, child2_handler);
20 | // signal(SIGQUIT, SIG_IGN);
21 | while (1) sleep(1);
22 |
23 | /* child 1 process*/
24 | signal(SIGUSR1, child1_handler);
25 | // signal(SIGQUIT, SIG_IGN);
26 | while (1) sleep(1);
27 | ```
28 |
29 | 运行程序,由于 `alarm(5)`, 因此 5s 后父进程收到软中断信号 `SIGALRM`, 父进程向子进程发送信号,子进程接收信号后退出,父进程随之退出,输出如下:
30 |
31 |
32 | 但是如果在时间内键盘按下 `Ctrl + \`, 向父进程发出信号
33 | `SIGQUIT`, 则父进程会退出,但是子进程不会打印。
34 | 经过查阅, `Ctrl + \`终端会向所有现在运行中的前台进程发送 `SIGQUIT`, 因此子进程直接退出,不会执行其剩下的语句
35 | 。在子进程里加上
36 | ```cpp
37 | signal(SIGQUIT, SIG_IGN);
38 | ```
39 | 使其忽略 `SIGQUIT` 信号后问题解决.
40 |
41 |
42 |
43 |
44 | ---
45 |
46 | ### 管道通信
47 |
48 | 根据所给代码补充完在父(子)进程中读写的部分代码
49 | ```cpp
50 | lockf(fd[1],1,0);
51 | for (int i = 0; i < (BUFFER_SIZE - 1) / 2; ++i)
52 | {
53 | write(fd[WRITE_END], c1, 1);
54 | }
55 | lockf(fd[1],0,0);
56 | sleep(2);
57 |
58 | // **************************
59 |
60 | lockf(fd[1],1,0);
61 | for (int i = 0; i < (BUFFER_SIZE - 1) / 2; ++i)
62 | {
63 | write(fd[WRITE_END], c2, 1);
64 | }
65 | lockf(fd[1],0,0);
66 | sleep(2);
67 |
68 | // *****************************
69 |
70 | wait(NULL);
71 | lockf(fd[0],1,0);
72 | ssize_t size = read(fd[READ_END], InPipe, BUFFER_SIZE - 1);
73 | if(size > 0)
74 | InPipe[size] = '\0';
75 | else if(size == 0)
76 | printf("quit\n");
77 | else
78 | printf("error\n");
79 | lockf(fd[0],0,0);
80 | printf("%s\n",InPipe);
81 | ```
82 |
83 | 在父进程中,如果去掉lockf函数
84 | ```cpp
85 | // lockf(fd[0],1,0);
86 | // .............
87 | // lockf(fd[0],0,0);
88 | ```
89 |
90 | 则管道读写的互斥和同步出现问题,输出结果如下:
91 |
92 |
93 | 加上lockf函数后读写正常
94 |
95 |
96 | ---
97 |
98 | ## Part2 内存分配与回收
99 |
100 | 实现该部分实验主要有两个类
101 |
102 | `VarSizeAllocMngr` ---> 可变长度分配Manager
103 |
104 | `Allocator` ---> 继承自 `VarSizeAllocMngr`, 并且包装了对于"进程"pid的管理.
105 |
106 | ---
107 |
108 | ### *VarSizeAllocMngr*
109 |
110 | 该类的核心成员如下,其中对于每一个分配`(Allocation)`用一个偏移`(Offset)`和长度`(Size)`来表示, 类似 `std::span`, 对每一个空闲块`(FreeBlock)`也同样如此
111 |
112 | ```cpp
113 | using OffsetType = int;
114 |
115 | struct FreeBlockInfo;
116 |
117 | // Type of the map that keeps memory blocks sorted by their offsets
118 | using TFreeBlocksByOffsetMap = std::map;
119 |
120 | // Type of the map that keeps memory blocks sorted by their sizes
121 | using TFreeBlocksBySizeMap = std::multimap;
122 |
123 | using AllocatedMap = std::map;
124 |
125 | struct FreeBlockInfo {
126 | // Block size (no reserved space for the size of allocation)
127 | // actually Size == OrderBySizeIt->first
128 | OffsetType Size;
129 | // Iterator referencing this block in the multimap sorted by the block size
130 | TFreeBlocksBySizeMap::iterator OrderBySizeIt;
131 |
132 | FreeBlockInfo(OffsetType _Size) : Size(_Size) {}
133 | };
134 | ```
135 |
136 | 其中有三个核心成员
137 | ```cpp
138 | TFreeBlocksByOffsetMap m_FreeBlocksByOffset;
139 | TFreeBlocksBySizeMap m_FreeBlocksBySize;
140 | AllocatedMap m_Allocated;
141 | ```
142 |
143 | * `m_FreeBlocksByOffset`记录每个空闲块, 其索引为空闲块的偏移(即起点).
144 | * `m_FreeBlocksBySize`其索引为空闲块的大小, 其类型是一个 `std::multimap<...>`, 使用 `multi` 是因为不同空闲块的大小可能一致,在此 map 中保存的块是按大小从小到大排序的,可以用于后续实现三种不同算法的 `Allocate`
145 | * `m_Allocated` 是一个 `std::map`, 记录了目前所有已分配的块, 其索引为块的 `Offset`, 值为块的 `Size`
146 |
147 |
148 | 三种 Allocate 的实现:
149 |
150 | * FF:遍历 `m_FreeBlocksByOffset`, 在容量足够的第一个块中分配
151 | * BF: 使用 `lower_bound(xxx)` 在 `m_FreeBlocksBySize` 中找到最小的容量足够的块,在其中分配即可
152 | * WF: 与 BF 恰好相反
153 |
154 | Free的实现:
155 |
156 | * 需要考虑在释放块时,该空闲块能不能与其紧挨的之前的空闲块合并,后者与之后的紧挨的空闲块合并。多判断几种情况即可
157 |
158 | ---
159 |
160 | ### *Allocator*
161 |
162 | 其核心数据成员如下:
163 | ```cpp
164 | std::set ChildPidList;
165 | std::map> Pid2Offsets;
166 | ```
167 |
168 | 记录了所有存在的子进程的pid以及其该进程所占有的分配块,
169 | `Pid2Offsets`键值对中的值为块的`Offset`,根据该偏移即可在 `VarSizeAllocMngr` 中的 `m_FreeBlocksByOffset` 里查到该块的长度
170 |
171 | 核心方法如下:
172 | ```cpp
173 | int AddProcessAndAllocate(int size, Option option = Option::BF);
174 |
175 | int Allocate(int size, Option option = Option::BF);
176 |
177 | void NotDelProcessAndFree(pid_t pid, int offset, int size);
178 |
179 | void DelProcessAndFree(pid_t pid);
180 |
181 | void OutputGraph();
182 |
183 | void Free(pid_t pid, OffsetType Offset, OffsetType Size);
184 | ```
185 |
186 | 通过枚举类
187 | ```cpp
188 | enum class Option : uint8_t
189 | {
190 | FF = 0,
191 | BF,
192 | WF
193 | };
194 | ```
195 | 来设置不同的分配算法.
196 |
197 | ---
198 |
199 | ### *运行展示*
200 |
201 | 菜单界面
202 |
203 |
204 |
205 | 如果没有调用 `1`,即分配内存总大小就调用其他,则会提示
206 | Initilize the allocator first!!!
207 |
208 |
209 |
210 |
211 | 设置内存为`1024`
212 |
213 |
214 |
215 |
216 | 创建一个进程内存大小为100
217 |
218 |
219 |
220 |
221 | 创建一个进程内存大小为800
222 |
223 |
224 |
225 |
226 | 目前内存布局如下
227 |
228 |
229 |
230 |
231 | 把进程2的内存中 (300 --> 500)的部分释放
232 |
233 |
234 |
235 |
236 | 内存布局如下,可以看到进程2目前占有两个块
237 |
238 |
239 |
240 |
241 | 把进程2的内存中 (500 --> 800)的部分释放
242 |
243 |
244 |
245 |
246 | 目前内存布局如下
247 |
248 |
249 |
250 |
251 | 设置算法为 BF
252 |
253 |
254 |
255 |
256 | 分配一个大小的100的进程
257 |
258 |
259 |
260 |
261 | 内存布局如下,可以看到采用了 BF 算法,选择了最小的块, 即后面的那个块去分配这 100 的大小
262 |
263 |
264 |
265 |
266 | 设置算法为FF
267 |
268 |
269 |
270 |
271 | 分配一个大小为10的进程,可以看到根据 FF 算法,内存分配在了最前面的块里
272 |
273 |
274 |
275 |
276 | 设置为 WF 算法,可以看到分配在了最大的块里
277 |
278 |
279 |
280 |
281 | 终止进程1
282 |
283 |
284 |
285 |
286 | 查看内存布局如下,可以看到进程1的内存已经被全部释放
287 |
288 |
289 |
290 | ---
291 |
292 | ## Part 3 遇到的问题
293 |
294 | 在管道程序中,由于 `alarm(5)`, 因此 5s 后父进程收到软中断信号 `SIGALRM`, 父进程向子进程发送信号,子进程接收信号后退出,父进程随之退出,输出如下:
295 |
296 |
297 | 但是如果在时间内键盘按下 `Ctrl + \`, 向父进程发出信号
298 | `SIGQUIT`, 则父进程会退出,但是子进程不会打印。
299 | 经过查阅, `Ctrl + \`终端会向所有现在运行中的前台进程发送 `SIGQUIT`, 因此子进程直接退出,不会执行其剩下的语句
300 | 。在子进程里加上
301 | ```cpp
302 | signal(SIGQUIT, SIG_IGN);
303 | ```
304 | 使其忽略 `SIGQUIT` 信号后问题解决.
305 |
306 |
307 |
308 | ---
309 |
310 | ## Part 4 分析和思考
311 |
312 | * 在写程序时要注意设计以及功能模块的划分,对于每一个功能要考虑各种情况,保证程序的健壮性.
313 |
314 | ---
315 |
316 | ## Part 5 Score
317 |
318 | 自评分: 30
319 |
--------------------------------------------------------------------------------
/lab2/images/mem/alloc1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YichenWu11/OS_Lab/6d7322bb59542aa0c2b72f62dcc41bc53d288417/lab2/images/mem/alloc1.png
--------------------------------------------------------------------------------
/lab2/images/mem/alloc10.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YichenWu11/OS_Lab/6d7322bb59542aa0c2b72f62dcc41bc53d288417/lab2/images/mem/alloc10.png
--------------------------------------------------------------------------------
/lab2/images/mem/alloc11.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YichenWu11/OS_Lab/6d7322bb59542aa0c2b72f62dcc41bc53d288417/lab2/images/mem/alloc11.png
--------------------------------------------------------------------------------
/lab2/images/mem/alloc12.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YichenWu11/OS_Lab/6d7322bb59542aa0c2b72f62dcc41bc53d288417/lab2/images/mem/alloc12.png
--------------------------------------------------------------------------------
/lab2/images/mem/alloc13.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YichenWu11/OS_Lab/6d7322bb59542aa0c2b72f62dcc41bc53d288417/lab2/images/mem/alloc13.png
--------------------------------------------------------------------------------
/lab2/images/mem/alloc14.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YichenWu11/OS_Lab/6d7322bb59542aa0c2b72f62dcc41bc53d288417/lab2/images/mem/alloc14.png
--------------------------------------------------------------------------------
/lab2/images/mem/alloc15.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YichenWu11/OS_Lab/6d7322bb59542aa0c2b72f62dcc41bc53d288417/lab2/images/mem/alloc15.png
--------------------------------------------------------------------------------
/lab2/images/mem/alloc16.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YichenWu11/OS_Lab/6d7322bb59542aa0c2b72f62dcc41bc53d288417/lab2/images/mem/alloc16.png
--------------------------------------------------------------------------------
/lab2/images/mem/alloc17.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YichenWu11/OS_Lab/6d7322bb59542aa0c2b72f62dcc41bc53d288417/lab2/images/mem/alloc17.png
--------------------------------------------------------------------------------
/lab2/images/mem/alloc18.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YichenWu11/OS_Lab/6d7322bb59542aa0c2b72f62dcc41bc53d288417/lab2/images/mem/alloc18.png
--------------------------------------------------------------------------------
/lab2/images/mem/alloc2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YichenWu11/OS_Lab/6d7322bb59542aa0c2b72f62dcc41bc53d288417/lab2/images/mem/alloc2.png
--------------------------------------------------------------------------------
/lab2/images/mem/alloc3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YichenWu11/OS_Lab/6d7322bb59542aa0c2b72f62dcc41bc53d288417/lab2/images/mem/alloc3.png
--------------------------------------------------------------------------------
/lab2/images/mem/alloc4.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YichenWu11/OS_Lab/6d7322bb59542aa0c2b72f62dcc41bc53d288417/lab2/images/mem/alloc4.png
--------------------------------------------------------------------------------
/lab2/images/mem/alloc5.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YichenWu11/OS_Lab/6d7322bb59542aa0c2b72f62dcc41bc53d288417/lab2/images/mem/alloc5.png
--------------------------------------------------------------------------------
/lab2/images/mem/alloc6.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YichenWu11/OS_Lab/6d7322bb59542aa0c2b72f62dcc41bc53d288417/lab2/images/mem/alloc6.png
--------------------------------------------------------------------------------
/lab2/images/mem/alloc7.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YichenWu11/OS_Lab/6d7322bb59542aa0c2b72f62dcc41bc53d288417/lab2/images/mem/alloc7.png
--------------------------------------------------------------------------------
/lab2/images/mem/alloc8.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YichenWu11/OS_Lab/6d7322bb59542aa0c2b72f62dcc41bc53d288417/lab2/images/mem/alloc8.png
--------------------------------------------------------------------------------
/lab2/images/mem/alloc9.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YichenWu11/OS_Lab/6d7322bb59542aa0c2b72f62dcc41bc53d288417/lab2/images/mem/alloc9.png
--------------------------------------------------------------------------------
/lab2/images/pipe/pipe_with_lock.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YichenWu11/OS_Lab/6d7322bb59542aa0c2b72f62dcc41bc53d288417/lab2/images/pipe/pipe_with_lock.png
--------------------------------------------------------------------------------
/lab2/images/pipe/pipe_without_lock.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YichenWu11/OS_Lab/6d7322bb59542aa0c2b72f62dcc41bc53d288417/lab2/images/pipe/pipe_without_lock.png
--------------------------------------------------------------------------------
/lab2/images/sig/sig_alarm1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YichenWu11/OS_Lab/6d7322bb59542aa0c2b72f62dcc41bc53d288417/lab2/images/sig/sig_alarm1.png
--------------------------------------------------------------------------------
/lab2/images/sig/sig_alarm2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YichenWu11/OS_Lab/6d7322bb59542aa0c2b72f62dcc41bc53d288417/lab2/images/sig/sig_alarm2.png
--------------------------------------------------------------------------------
/lab2/images/sig/sig_quit1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YichenWu11/OS_Lab/6d7322bb59542aa0c2b72f62dcc41bc53d288417/lab2/images/sig/sig_quit1.png
--------------------------------------------------------------------------------
/lab2/images/sig/sig_quit2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YichenWu11/OS_Lab/6d7322bb59542aa0c2b72f62dcc41bc53d288417/lab2/images/sig/sig_quit2.png
--------------------------------------------------------------------------------
/lab2/memory/.gitignore:
--------------------------------------------------------------------------------
1 | build
2 | .vscode
--------------------------------------------------------------------------------
/lab2/memory/CMakeLists.txt:
--------------------------------------------------------------------------------
1 | cmake_minimum_required (VERSION 3.5)
2 |
3 | project(MemoryAlloc)
4 |
5 | set(CMAKE_CXX_STANDARD 17)
6 | set(CMAKE_CXX_STANDARD_REQUIRED True)
7 | set(CMAKE_BUILD_TYPE "Debug")
8 |
9 | add_executable(${PROJECT_NAME}
10 | src/main.cpp
11 | src/VarSizeAllocMngr.cpp
12 | src/Allocator.cpp
13 | )
14 |
15 | target_include_directories(${PROJECT_NAME}
16 | PUBLIC ${PROJECT_SOURCE_DIR}/include
17 | )
18 |
--------------------------------------------------------------------------------
/lab2/memory/Example.md:
--------------------------------------------------------------------------------
1 | ## WF
2 |
3 | ```cpp
4 | VarSizeAllocMngr allocMngr;
5 |
6 | allocMngr.AllocateBF(10);
7 |
8 | allocMngr.AllocateBF(900);
9 |
10 | allocMngr.Free(10, 200);
11 | allocMngr.Free(400, 300);
12 |
13 | allocMngr.OutputAllocGraph();
14 |
15 | allocMngr.AllocateWF(50);
16 |
17 | allocMngr.OutputAllocGraph();
18 | ```
--------------------------------------------------------------------------------
/lab2/memory/include/Allocator.h:
--------------------------------------------------------------------------------
1 | #include "VarSizeAllocMngr.h"
2 | #include
3 | #include
4 | #include
5 |
6 | class Allocator : public VarSizeAllocMngr
7 | {
8 | public:
9 | enum class Option : uint8_t
10 | {
11 | FF = 0,
12 | BF,
13 | WF
14 | };
15 |
16 | Allocator(int capacity = 1024);
17 | ~Allocator();
18 |
19 | int AddProcessAndAllocate(int size, Option option = Option::BF);
20 |
21 | std::vector Allocate(int size, Option option = Option::BF);
22 |
23 | void NotDelProcessAndFree(pid_t pid, int offset, int size);
24 |
25 | void DelProcessAndFree(pid_t pid);
26 |
27 | void OutputGraph();
28 |
29 | void Free(pid_t pid, OffsetType Offset, OffsetType Size);
30 |
31 | int Extend2PowerOfTwo(int byteSize) { return (byteSize + 255) & ~255; }
32 |
33 | private:
34 | // father pid : 0
35 | std::set ChildPidList;
36 | std::map> Pid2Offsets; // 记录分配给子进程的 allocation
37 |
38 | static int source;
39 | };
40 |
--------------------------------------------------------------------------------
/lab2/memory/include/VarSizeAllocMngr.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include