Tag: Linux

  • Mutex, Semaphore, the difference, and Linux kernel

    Mutex, Semaphore, the difference, and Linux kernel

    名詞定義 Mutex: Linux kenrel 中的 mutex lock, <include/mutex.h> Semaphore: Linux kernel 中的 semaphore, <include/semaphore.h> mutual exclusion: 中翻互斥鎖,一個概念,為了防止 concurrency 狀況下出現 race condition.   Mutex 與 Semaphore 最大的差異是: 理論上,你應該要先跟面試官或是問你這個問題的人互動,詢問一下其所謂的差異是指哪個部份 (實作、用途、還是結構?),以及詢問這個問題時,想要將兩者應用在那邊,對於後續的回答會有所幫助。 30秒:最大的差異在於 Mutex 只能由上鎖的 thread 解鎖,而 Semaphore 沒有這個限制,可以由原本的 thread 或是另外一個 thread 解開。另外,Mutex 只能讓一個 thread 進入 critical section,Semaphore 的話則可以設定要讓幾個 thread 進入。這讓實際上使用 Mutex 跟 Semaphore 場景有很大的差別。 _ 60秒 (cont.):舉例而言,Mutex 的兩個特性:一個是只能有持鎖人解鎖、一個是在釋放鎖之前不能退出的特性,讓…

  • 無法 build Linux kernel samples/bpf

    小結 總之,遇到問題記得先看 README.md / README.rst / README。   解決方式 在 linux/samples/bpf 這個資料夾裏面無法 build 出 sample 來 ➜ bpf git:(master) ✗ make make -C ../../ $PWD/ make[1]: Entering directory ‘linux’ CHK include/config/kernel.release CHK include/generated/uapi/linux/version.h CHK include/generated/utsrelease.h CHK include/generated/timeconst.h CHK include/generated/bounds.h CHK include/generated/asm-offsets.h CALL scripts/checksyscalls.sh DESCEND objtool LD linux/samples/bpf/built-in.o HOSTCC linux/samples/bpf/test_verifier.o HOSTCC linux/samples/bpf/libbpf.o HOSTLD linux/samples/bpf/test_verifier HOSTCC linux/samples/bpf/test_maps.o…

  • 在 Linux 上不能用 pthread_delay_np

    在2016年系統軟體課程裏面有人提到:「為什麼不能用 pthread_delay_np,或是 pthread_cond_timewait等等」 會有這個問題是因為 jserv 在上到 pthread / concurrency 的時候常會提到中文界第一個以及唯一一個 pthread 文章翻譯「Getting Starated with POSIX Threads」,我們必須要考量到這篇文章翻譯的時間,是距今快要 20 年前的1998年翻譯完成的。當時使用的系統也不是 Linux,是 DEC OSF/1!作者另外也測試了 solaris 2.5.1 跟 SunOS 4.1.4。 回到正題,那為什麼 Linux 上不能用 pthread_delay_np 呢? 原因是因為,pthread_delay_np 只支援 HP-UX / VMS / Tru64 UNIX。pthread_delay_np() 存在於 POSIX.1c D4 draft,而 POSIX.1c D4 的 implementer 就是製作 OSF 的 DCE! 我們另外可以觀察到 pthread_delay_np 的後綴是 _np,代表這個function…

  • gdb 顯示 FLAGS register

    假設有這樣的程式碼 > cat eflags.c #include <stdint.h> int main(void) { int32_t n = 2147483647; n = n + 1; printf(“%dn”, n); return 0; } 在 int32_t 的情況下,2147483647 是最大值,加上 1 之後預期 variable n 會 overflow。 我們都知道 CPU 裏面有 FLAGS register 可以表示處理器現在的狀況,那要怎麼在 gdb 裏面顯示呢? 只要在 gdb 裏面打這個指令就會顯示出 FLAGS register 目前的數值。 (gdb) info reg eflags eflags 0xa96 [ PF…

  • 現在正在讀的 Linux 相關的書

    Linux kernel Linux Kernel Development / 3e (精通 Linux 核心開發) Linux Device Drivers / 3e (Linux 驅動程式) Linux Kernel and Driver Development Training – free electrons (PDF) Debug Hacks 除錯駭客:極致除錯的技巧與工具 Advanced Programming in the UNIX Environment / 3e C Expert C Programming – Deep C Debugging with gdb – The gnu Source-Level Debugger 透視 C…