Skip to content

Commit

Permalink
update 2024年 05月 14日 星期二 15:59:16 CST
Browse files Browse the repository at this point in the history
  • Loading branch information
ccrysisa committed May 14, 2024
1 parent 15025c0 commit 33f3f79
Show file tree
Hide file tree
Showing 296 changed files with 6,747 additions and 5,879 deletions.
1 change: 1 addition & 0 deletions content/friends/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
title: "所有友链"
subtitle:
layout: friends
type: friends
date: 2024-04-29T23:46:50+08:00
description: "ccrysisa's friends"
keywords:
Expand Down
71 changes: 71 additions & 0 deletions content/posts/sysprog/posix-threads.md
Original file line number Diff line number Diff line change
Expand Up @@ -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" >}}
Expand Down Expand Up @@ -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.h>
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


9 changes: 7 additions & 2 deletions data/friends.yml
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
10 changes: 5 additions & 5 deletions docs/404.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<body data-header-desktop="sticky" data-header-mobile="auto"><script>(window.localStorage?.getItem('theme') ? localStorage.getItem('theme') === 'dark' : ('auto' === 'auto' ? window.matchMedia('(prefers-color-scheme: dark)').matches : 'auto' === 'dark')) && document.body.setAttribute('data-theme', 'dark');</script><div class="wrapper" data-page-style="normal"><header class="desktop animate__faster" id="header-desktop">
<div class="header-wrapper">
<div class="header-title">
<a href="/" title="KZnight&#39;s Blog"><img loading="lazy" src="/fixit.min.svg" alt="KZnight&#39;s Blog" data-title="KZnight&#39;s Blog" class="logo" style="background: url(/images/loading.min.svg) no-repeat center;" onload="this.title=this.dataset.title;for(const i of ['style', 'data-title','onerror','onload']){this.removeAttribute(i);}this.dataset.lazyloaded='';" onerror="this.title=this.dataset.title;for(const i of ['style', 'data-title','onerror','onload']){this.removeAttribute(i);}"/><span class="header-title-text">KZnight&#39;s Blog</span></a><span class="header-subtitle"></span></div>
<a href="/" title="KZnight&#39;s Blog"><img loading="lazy" src="/fixit.min.svg" data-title="KZnight&#39;s Blog" data-alt="KZnight&#39;s Blog" class="logo" style="background: url(/svg/loading.min.svg) no-repeat center;" onload="this.title=this.dataset.title;this.alt=this.dataset.alt;for(const i of ['style', 'data-title','data-alt','onerror','onload']){this.removeAttribute(i);}this.dataset.lazyloaded='';" onerror="this.title=this.dataset.title;this.alt=this.dataset.alt;for(const i of ['style', 'data-title','data-alt','onerror','onload']){this.removeAttribute(i);}"/><span class="header-title-text">KZnight&#39;s Blog</span></a><span class="header-subtitle"></span></div>
<nav>
<ul class="menu"><li class="menu-item">
<a
Expand Down Expand Up @@ -71,7 +71,7 @@
<div class="header-container">
<div class="header-wrapper">
<div class="header-title">
<a href="/" title="KZnight&#39;s Blog"><img loading="lazy" src="/fixit.min.svg" alt="/fixit.min.svg" data-title="/fixit.min.svg" class="logo" style="background: url(/images/loading.min.svg) no-repeat center;" onload="this.title=this.dataset.title;for(const i of ['style', 'data-title','onerror','onload']){this.removeAttribute(i);}this.dataset.lazyloaded='';" onerror="this.title=this.dataset.title;for(const i of ['style', 'data-title','onerror','onload']){this.removeAttribute(i);}"/><span class="header-title-text">KZnight&#39;s Blog</span></a><span class="header-subtitle"></span></div>
<a href="/" title="KZnight&#39;s Blog"><img loading="lazy" src="/fixit.min.svg" data-title="/fixit.min.svg" data-alt="/fixit.min.svg" class="logo" style="background: url(/svg/loading.min.svg) no-repeat center;" onload="this.title=this.dataset.title;this.alt=this.dataset.alt;for(const i of ['style', 'data-title','data-alt','onerror','onload']){this.removeAttribute(i);}this.dataset.lazyloaded='';" onerror="this.title=this.dataset.title;this.alt=this.dataset.alt;for(const i of ['style', 'data-title','data-alt','onerror','onload']){this.removeAttribute(i);}"/><span class="header-title-text">KZnight&#39;s Blog</span></a><span class="header-subtitle"></span></div>
<div class="menu-toggle" id="menu-toggle-mobile">
<span></span><span></span><span></span>
</div>
Expand Down Expand Up @@ -143,16 +143,16 @@ <h1 id="error-emoji"></h1>
document.getElementById('error-emoji').appendChild(document.createTextNode(emojiArray[Math.floor(Math.random() * emojiArray.length)]));
})();
</script></main><footer class="footer">
<div class="footer-container"><div class="footer-line powered"><a href="https://gohugo.io/" target="_blank" rel="external nofollow noopener noreferrer" title="Hugo 0.121.1"><img class="hugo-icon" src="/images/hugo.min.svg" alt="Hugo logo" /> Hugo</a> 强力驱动 | 主题 - <a href="https://github.com/hugo-fixit/FixIt" target="_blank" rel="external" title="FixIt v0.3.2-RC"><img class="fixit-icon" src="/images/fixit.min.svg" alt="FixIt logo" /> FixIt</a>
<div class="footer-container"><div class="footer-line powered"><a href="https://gohugo.io/" target="_blank" rel="external nofollow noopener noreferrer" title="Hugo 0.121.1">Hugo</a> 强力驱动 | 主题 - <a href="https://github.com/hugo-fixit/FixIt" target="_blank" rel="external" title="FixIt v0.2.18-lts.5"><img class="fixit-icon" src="/fixit.min.svg" alt="FixIt logo" />&nbsp;FixIt</a>
</div><div class="footer-line copyright" itemscope itemtype="http://schema.org/CreativeWork"><i class="fa-regular fa-copyright fa-fw" aria-hidden="true"></i>
<span itemprop="copyrightYear">2021 - 2024</span><span class="author" itemprop="copyrightHolder">
<a href="https://github.com/ccrysisa"target="_blank" rel="external nofollow noopener noreferrer">ccrysisa</a></span><span class="license footer-divider"><a rel="license external nofollow noopener noreferrer" href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a></span></div><div class="footer-line visitor">
<a href="https://github.com/ccrysisa"target="_blank" rel="external nofollow noopener noreferrer">ccrysisa</a></span><span class="license footer-divider"><a rel="license external nofollow noopener noreferrer" href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a></span></div><div class="footer-line statistics"></div><div class="footer-line visitor">
<span id="busuanzi_container_site_uv" title='总访客数'><i class="fa-regular fa-user fa-fw" aria-hidden="true"></i>&nbsp;<span id="busuanzi_value_site_uv"><i class="fa-solid fa-spinner fa-spin fa-fw" aria-hidden="true"></i></span></span><span id="busuanzi_container_site_pv" class="footer-divider" title='总访问量'><i class="fa-regular fa-eye fa-fw" aria-hidden="true"></i>&nbsp;<span id="busuanzi_value_site_pv"><i class="fa-solid fa-spinner fa-spin fa-fw" aria-hidden="true"></i></span></span>
</div></div>
</footer></div><div class="widgets"><div class="fixed-buttons animate__faster d-none"><div class="fixed-button back-to-top" role="button" aria-label="回到顶部"><i class="fa-solid fa-arrow-up fa-fw" aria-hidden="true"></i><span class="variant-numeric d-none">0%</span>
</div></div><div id="mask"></div><noscript>
<div class="noscript-warning">FixIt 主题在启用 JavaScript 的情况下效果最佳。</div>
</noscript>
</div><link rel="preload" href="/lib/katex/katex.min.css" as="style" onload="this.removeAttribute('onload');this.rel='stylesheet'">
<noscript><link rel="stylesheet" href="/lib/katex/katex.min.css"></noscript><link rel="stylesheet" href="/lib/cookieconsent/cookieconsent.min.css"><script src="/lib/autocomplete/autocomplete.min.js" defer></script><script src="/lib/sharer/sharer.min.js" async defer></script><script src="/lib/katex/katex.min.js" defer></script><script src="/lib/katex/auto-render.min.js" defer></script><script src="/lib/katex/copy-tex.min.js" defer></script><script src="/lib/katex/mhchem.min.js" defer></script><script src="/lib/cookieconsent/cookieconsent.min.js" defer></script><script src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" async defer></script><script>window.config={"code":{"copyTitle":"复制到剪贴板","editLockTitle":"锁定可编辑代码块","editUnLockTitle":"解锁可编辑代码块","editable":true,"maxShownLines":30},"cookieconsent":{"content":{"dismiss":"同意","link":"了解更多","message":"本网站使用 Cookies 来改善您的浏览体验。"},"enable":true,"palette":{"button":{"background":"#f0f0f0"},"popup":{"background":"#1aa3ff"}},"theme":"edgeless"},"enablePWA":true,"math":{"delimiters":[{"display":true,"left":"$$","right":"$$"},{"display":true,"left":"\\[","right":"\\]"},{"display":true,"left":"\\begin{equation}","right":"\\end{equation}"},{"display":true,"left":"\\begin{equation*}","right":"\\end{equation*}"},{"display":true,"left":"\\begin{align}","right":"\\end{align}"},{"display":true,"left":"\\begin{align*}","right":"\\end{align*}"},{"display":true,"left":"\\begin{alignat}","right":"\\end{alignat}"},{"display":true,"left":"\\begin{alignat*}","right":"\\end{alignat*}"},{"display":true,"left":"\\begin{gather}","right":"\\end{gather}"},{"display":true,"left":"\\begin{CD}","right":"\\end{CD}"},{"display":false,"left":"$","right":"$"},{"display":false,"left":"\\(","right":"\\)"}],"strict":false},"search":{"highlightTag":"em","maxResultLength":10,"noResultsFound":"没有找到结果","snippetLength":50}};</script><script src="/js/theme.min.js" defer></script></body>
<noscript><link rel="stylesheet" href="/lib/katex/katex.min.css"></noscript><link rel="stylesheet" href="/lib/cookieconsent/cookieconsent.min.css"><script src="/lib/autocomplete/autocomplete.min.js" defer></script><script src="/lib/lunr/lunr.min.js" defer></script><script src="/lib/lunr/lunr.stemmer.support.min.js" defer></script><script src="/lib/lunr/lunr.zh.min.js" defer></script><script src="/lib/sharer/sharer.min.js" async defer></script><script src="/lib/katex/katex.min.js" defer></script><script src="/lib/katex/auto-render.min.js" defer></script><script src="/lib/katex/copy-tex.min.js" defer></script><script src="/lib/katex/mhchem.min.js" defer></script><script src="/lib/cookieconsent/cookieconsent.min.js" defer></script><script src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" async defer></script><script>window.config={"code":{"copyTitle":"复制到剪贴板","editLockTitle":"锁定可编辑代码块","editUnLockTitle":"解锁可编辑代码块","editable":true,"maxShownLines":30},"cookieconsent":{"content":{"dismiss":"同意","link":"了解更多","message":"本网站使用 Cookies 来改善您的浏览体验。"},"enable":true,"palette":{"button":{"background":"#f0f0f0"},"popup":{"background":"#1aa3ff"}},"theme":"edgeless"},"enablePWA":true,"math":{"delimiters":[{"display":true,"left":"$$","right":"$$"},{"display":true,"left":"\\[","right":"\\]"},{"display":true,"left":"\\begin{equation}","right":"\\end{equation}"},{"display":true,"left":"\\begin{equation*}","right":"\\end{equation*}"},{"display":true,"left":"\\begin{align}","right":"\\end{align}"},{"display":true,"left":"\\begin{align*}","right":"\\end{align*}"},{"display":true,"left":"\\begin{alignat}","right":"\\end{alignat}"},{"display":true,"left":"\\begin{alignat*}","right":"\\end{alignat*}"},{"display":true,"left":"\\begin{gather}","right":"\\end{gather}"},{"display":true,"left":"\\begin{CD}","right":"\\end{CD}"},{"display":false,"left":"$","right":"$"},{"display":false,"left":"\\(","right":"\\)"}],"strict":false},"search":{"highlightTag":"em","lunrIndexURL":"/index.json","lunrLanguageCode":"zh","lunrSegmentitURL":"/lib/lunr/lunr.segmentit.js","maxResultLength":10,"noResultsFound":"没有找到结果","snippetLength":50,"type":"lunr"}};</script><script src="/js/theme.min.js" defer></script></body>
</html>
58 changes: 58 additions & 0 deletions docs/baidu_urls.txt
Original file line number Diff line number Diff line change
@@ -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/
Loading

0 comments on commit 33f3f79

Please sign in to comment.