diff --git a/content/friends/index.md b/content/friends/index.md index 32391a06..c1ae903c 100644 --- a/content/friends/index.md +++ b/content/friends/index.md @@ -2,6 +2,7 @@ title: "所有友链" subtitle: layout: friends +type: friends date: 2024-04-29T23:46:50+08:00 description: "ccrysisa's friends" keywords: diff --git a/content/posts/sysprog/posix-threads.md b/content/posts/sysprog/posix-threads.md index c25862ab..f79af6c1 100644 --- a/content/posts/sysprog/posix-threads.md +++ b/content/posts/sysprog/posix-threads.md @@ -63,6 +63,8 @@ repost: $\rightarrow$ 在使用 coroutinues 后执行流程变成 $\rightarrow$ {{< image src="https://hackpad-attachments.s3.amazonaws.com/embedded2016.hackpad.com_K6DJ0ZtiecH_p.537916_1460615044111_undefined" >}} +C 语言程序中实作 coroutinue 的方法很多,例如「[C 语言: goto 和流程控制篇](https://hackmd.io/@sysprog/c-control-flow)」中提到的使用 `switch-case` 技巧进行实作。 + ### Thread & Process {{< image src="https://imgur-backup.hackmd.io/QW1YWsC.png" >}} @@ -176,5 +178,74 @@ int sem_post(sem_t *sem); - As soon as semaphore value is greater than zero, one of the blocked threads wakes up and continues - no guarantees as to which thread this might be +{{< admonition >}} +总结一下,`mutex` 在意的是 **持有者**,`semaphore` 在意的是 **资源的总量**,而 `condition variables` 在意的是 **持有的条件**。 +{{< /admonition >}} + ## POSIX Threads +{{< image src="https://hackpad-attachments.s3.amazonaws.com/embedded2016.hackpad.com_xBRCF9BsC50_p.537916_1457976043696_fork-join.jpg" >}} + +### 实例: 光线追踪 + +光线追踪 (Ray tracing) 相关: +- [2016q1 Homework #2](http://wiki.csie.ncku.edu.tw/embedded/2016q1h2) +- [UCLA Computer Science 35L, Winter 2016. Software Construction Laboratory](https://web.cs.ucla.edu/classes/winter16/cs35L/) +- [CS35L_Assign8_Multithreading](https://github.com/maxwyb/CS35L_Assign8_Multithreading) + +光线追踪需要很大的运算量,所以我们可以自然地想到,能不能使用 pthreads 对运算进行加速,上面的最后一个链接就是对这种思路的实作。 + +编译与测试: + +```bash +$ git clone https://github.com/maxwyb/CS35L_Assign8_Multithreading.git raytracing-threads +$ cd raytracing-threads +$ make clean all +$ ./srt 4 > out.ppm +$ diff -u out.ppm baseline.ppm +$ open out.ppm +``` + +预期得到下面的图: + +{{< image src="https://hackpad-attachments.s3.amazonaws.com/embedded2016.hackpad.com_xBRCF9BsC50_p.537916_1457975632540_out.png" >}} + +可以将上面的 `./srt` 命令后面的数字改为 1, 2, 8 之类的进行尝试,这个数字代表使用的执行绪的数量。另外,在 `./srt` 命令之前使用 `time` 命令可以计算本次进行光线追踪所使用的时间,由此可以对比不同数量执行绪下的效能差异。 + +可以看下相关的程式码 [main.c](): +```c +#include +pthread_t* threadID = malloc(nthreads * sizeof(pthread_t)); +int res = pthread_create(&threadID[t], 0, pixelProcessing, (void *)&intervals[t]); +int res = pthread_join(threadID[t], &retVal); +``` +显然是经典的 **fork-join** 模型 (`pthread_create` 进行 "fork",`pthread_join` 进行 "join"),注意这里并没有使用到 mutex 之类的互斥量,这是可以做到的,只要你事先区分开不相关的区域分别进行计算即可,即不会发生数据竞争,那么久没必要使用 mutex 了。 + +### POSIX Thread + +- [POSIX Threads Programming](https://hpc-tutorials.llnl.gov/posix/) +> Condition variables provide yet another way for threads to synchronize. While mutexes implement synchronization by controlling thread access to data, {{< style "background-color:green" "strong" >}}condition variables allow threads to synchronize based upon the actual value of data.{{< /style >}} + +condition variables 由两种不同的初始化方式: +- 静态初始化 (static): `PTHREAD_COND_INITIALIZER` +- 动态初始化 (dynamic): `pthread_cond_init()` + +## Synchronization + +CMU 15-213: Intro to Computer Systems + +- $23^{rd}$ Lecture [Concurrent Programming](https://www.cs.cmu.edu/afs/cs/academic/class/15213-f15/www/lectures/23-concprog.pdf) + +{{< image src="/images/c/23-concprog-24.png" >}} + +- $24^{rd}$ Lecture [Synchroniza+on: Basics](https://www.cs.cmu.edu/afs/cs/academic/class/15213-f15/www/lectures/24-sync-basic.pdf) + +{{< image src="/images/c/24-sync-basic-17.png" >}} + +> mutual exclusion (互斥) 手段的選擇,不是根據 CS 的大小,而是根據 CS 的性質,以及有哪些部分的程式碼,也就是,仰賴於核心內部的執行路徑。 + +> semaphore 和 spinlock 屬於不同層次的互斥手段,前者的實現仰賴於後者,可類比於 HTTP 和 TCP/IP 的關係,儘管都算是網路通訊協定,但層次截然不同 + +### Angrave's Crowd-Sourced System Programming Book used at UIUC + + diff --git a/data/friends.yml b/data/friends.yml index 1ec37552..3922137a 100644 --- a/data/friends.yml +++ b/data/friends.yml @@ -1,12 +1,12 @@ - nickname: jserv avatar: https://avatars.githubusercontent.com/u/478921?v=4 url: http://wiki.csie.ncku.edu.tw/User/jserv - description: + description: Participating in system software programming for more than 20 years. - nickname: Jon Gjengset avatar: https://avatars.githubusercontent.com/u/176295?v=4 url: https://thesquareplanet.com/ - description: + description: PhD student & Software Engineer - nickname: Eli Bendersky avatar: https://avatars.githubusercontent.com/u/1130906?v=4 @@ -28,6 +28,11 @@ url: https://justine.lol/ description: Justine Tunney's Web Page +- nickname: Lawrence Angrave + avatar: https://avatars.githubusercontent.com/u/4468456?v=4 + url: https://github.com/angrave/SystemProgramming/wiki + description: Angrave's crowd-sourced System Programming wiki-book + - nickname: vanJker avatar: https://avatars.githubusercontent.com/u/88960102?s=96&v=4 url: https://github.com/vanJker diff --git a/docs/404.html b/docs/404.html index b7ed7ccb..f15d63ac 100644 --- a/docs/404.html +++ b/docs/404.html @@ -25,7 +25,7 @@
0% @@ -154,5 +154,5 @@

FixIt 主题在启用 JavaScript 的情况下效果最佳。
- + diff --git a/docs/baidu_urls.txt b/docs/baidu_urls.txt new file mode 100644 index 00000000..d3de1b46 --- /dev/null +++ b/docs/baidu_urls.txt @@ -0,0 +1,58 @@ +https://ccrysisa.github.io/posts/linux2023/ +https://ccrysisa.github.io/friends/ +https://ccrysisa.github.io/posts/rust-cli/ +https://ccrysisa.github.io/posts/c-compiler-optimization/ +https://ccrysisa.github.io/posts/c-undefined-behavior/ +https://ccrysisa.github.io/posts/c-compiler-construction/ +https://ccrysisa.github.io/posts/wsl2/ +https://ccrysisa.github.io/posts/csapp-ch2/ +https://ccrysisa.github.io/posts/linux-rbtree/ +https://ccrysisa.github.io/posts/posix-threads/ +https://ccrysisa.github.io/posts/c-trick/ +https://ccrysisa.github.io/posts/rust-lifetime/ +https://ccrysisa.github.io/posts/c-control-flow/ +https://ccrysisa.github.io/posts/nju-ics/ +https://ccrysisa.github.io/posts/english/ +https://ccrysisa.github.io/posts/oerv-pretask/ +https://ccrysisa.github.io/posts/deepin-kvm/ +https://ccrysisa.github.io/posts/c-preprocessor/ +https://ccrysisa.github.io/posts/nju-osdi/ +https://ccrysisa.github.io/posts/subtying-and-variance/ +https://ccrysisa.github.io/posts/c-recursion/ +https://ccrysisa.github.io/posts/linux-hashtable/ +https://ccrysisa.github.io/posts/c-function/ +https://ccrysisa.github.io/posts/concurrency-ordering/ +https://ccrysisa.github.io/posts/concurrency-concepts/ +https://ccrysisa.github.io/posts/openeuler-riscv-qemu/ +https://ccrysisa.github.io/posts/c-std-security/ +https://ccrysisa.github.io/posts/orst/ +https://ccrysisa.github.io/posts/linux-dev-review/ +https://ccrysisa.github.io/posts/riscv-optimization-guide/ +https://ccrysisa.github.io/posts/channels/ +https://ccrysisa.github.io/posts/c-standards/ +https://ccrysisa.github.io/posts/c-memory/ +https://ccrysisa.github.io/posts/c-bitwise/ +https://ccrysisa.github.io/posts/smart-pointers-and-interior-mutability/ +https://ccrysisa.github.io/posts/c-numerics/ +https://ccrysisa.github.io/posts/linux2023-lab0/ +https://ccrysisa.github.io/posts/rust-tcp/ +https://ccrysisa.github.io/posts/linux-quiz1/ +https://ccrysisa.github.io/posts/linux-concepts/ +https://ccrysisa.github.io/posts/iterators/ +https://ccrysisa.github.io/posts/c-linked-list/ +https://ccrysisa.github.io/posts/declarative-macros/ +https://ccrysisa.github.io/posts/lifetime-annotations/ +https://ccrysisa.github.io/posts/deepin20.9/ +https://ccrysisa.github.io/posts/deepin-dragonos/ +https://ccrysisa.github.io/posts/debug-gdb/ +https://ccrysisa.github.io/posts/c-pointer/ +https://ccrysisa.github.io/posts/nthu-computer-network/ +https://ccrysisa.github.io/posts/c-specification/ +https://ccrysisa.github.io/posts/git/ +https://ccrysisa.github.io/posts/binary-representation/ +https://ccrysisa.github.io/posts/why-rust-/ +https://ccrysisa.github.io/posts/git-learn/ +https://ccrysisa.github.io/posts/ubuntu22.04lts/ +https://ccrysisa.github.io/posts/gnu-linux-dev/ +https://ccrysisa.github.io/posts/math/ +https://ccrysisa.github.io/posts/hello_world/ diff --git a/docs/categories/c/index.html b/docs/categories/c/index.html index c98b55ca..9d061784 100644 --- a/docs/categories/c/index.html +++ b/docs/categories/c/index.html @@ -26,7 +26,7 @@
-

+ C 16

2024

0% @@ -193,5 +193,5 @@
FixIt 主题在启用 JavaScript 的情况下效果最佳。
- + diff --git a/docs/categories/c/index.xml b/docs/categories/c/index.xml index 918796db..00fe64a6 100644 --- a/docs/categories/c/index.xml +++ b/docs/categories/c/index.xml @@ -2,7 +2,7 @@ C - 分类 - KZnight's Blog https://ccrysisa.github.io/categories/c/ - C - 分类 | KZnight's Blog + C - 分类 - KZnight's Blog Hugo -- gohugo.iozh-CNcaijiaxin@dragonos.org (ccrysisa) caijiaxin@dragonos.org (ccrysisa)Wed, 24 Apr 2024 21:09:59 +0800 你所不知道的 C 语言: 编译器和最佳化原理篇 diff --git a/docs/categories/concurrency/index.html b/docs/categories/concurrency/index.html index 1f6b70c0..0b62db38 100644 --- a/docs/categories/concurrency/index.html +++ b/docs/categories/concurrency/index.html @@ -26,7 +26,7 @@
-

Concurrency 3 -

2024

0% @@ -154,5 +154,5 @@
FixIt 主题在启用 JavaScript 的情况下效果最佳。
- + diff --git a/docs/categories/concurrency/index.xml b/docs/categories/concurrency/index.xml index 046e3430..7d62f600 100644 --- a/docs/categories/concurrency/index.xml +++ b/docs/categories/concurrency/index.xml @@ -2,7 +2,7 @@ Concurrency - 分类 - KZnight's Blog https://ccrysisa.github.io/categories/concurrency/ - Concurrency - 分类 | KZnight's Blog + Concurrency - 分类 - KZnight's Blog Hugo -- gohugo.iozh-CNcaijiaxin@dragonos.org (ccrysisa) caijiaxin@dragonos.org (ccrysisa)Wed, 10 Apr 2024 16:09:35 +0800 并行程序设计: POSIX Threads diff --git a/docs/categories/csapp/index.html b/docs/categories/csapp/index.html index 96e28a22..1aded7da 100644 --- a/docs/categories/csapp/index.html +++ b/docs/categories/csapp/index.html @@ -26,7 +26,7 @@
-

+ CSAPP 1

2024

0% @@ -148,5 +148,5 @@
FixIt 主题在启用 JavaScript 的情况下效果最佳。
- + diff --git a/docs/categories/csapp/index.xml b/docs/categories/csapp/index.xml index 86272055..f91d9241 100644 --- a/docs/categories/csapp/index.xml +++ b/docs/categories/csapp/index.xml @@ -2,7 +2,7 @@ CSAPP - 分类 - KZnight's Blog https://ccrysisa.github.io/categories/csapp/ - CSAPP - 分类 | KZnight's Blog + CSAPP - 分类 - KZnight's Blog Hugo -- gohugo.iozh-CNcaijiaxin@dragonos.org (ccrysisa) caijiaxin@dragonos.org (ccrysisa)Fri, 19 Apr 2024 15:33:40 +0800 CS:APP 第 2 章重点提示和练习 diff --git a/docs/categories/draft/index.html b/docs/categories/draft/index.html index b7141deb..2c88d953 100644 --- a/docs/categories/draft/index.html +++ b/docs/categories/draft/index.html @@ -26,7 +26,7 @@
-

draft 2 -

2024

+ draft 2

2024

2023

2023

0% @@ -151,5 +151,5 @@
FixIt 主题在启用 JavaScript 的情况下效果最佳。
- + diff --git a/docs/categories/draft/index.xml b/docs/categories/draft/index.xml index 89bcedbc..293c1f6d 100644 --- a/docs/categories/draft/index.xml +++ b/docs/categories/draft/index.xml @@ -2,7 +2,7 @@ draft - 分类 - KZnight's Blog https://ccrysisa.github.io/categories/draft/ - draft - 分类 | KZnight's Blog + draft - 分类 - KZnight's Blog Hugo -- gohugo.iozh-CNcaijiaxin@dragonos.org (ccrysisa) caijiaxin@dragonos.org (ccrysisa)Sat, 30 Mar 2024 12:17:31 +0800 English Everyday diff --git a/docs/categories/index.html b/docs/categories/index.html index a465918b..9e7ac816 100644 --- a/docs/categories/index.html +++ b/docs/categories/index.html @@ -25,7 +25,7 @@
-

所有分类 11 -

-