C LANGUAGE
linux
fork
signal
wait
mmap
thread
Author: WhoAmI
email: kccddb@gmail.com
Date: 20230915
Copyright: CC BY-NC-SA
也許以初學者而言您認為不重要, 但是這是設計穩定有效的程式基礎知識
Typical layout of a simple computer's program memory with the text, various data, and stack and heap sections.
Text segment (i.e. instructions)
Initialized Data Segment, e.g., char s[] = “hello world”, static int i = 10;
Uninitialized Data Segment,
e.g., static int i;
stack x86: LIFO structure
Heap: Heap is the segment where dynamic memory allocation usually takes place.
Ref.
Memory Layout of C Programs
Linker
ld - The GNU linker. Usually the last step in compiling a program is to run ld.
https://computersciencewiki.org/index.php/Operating_system#Virtual_memory
fork system call
fork+interrupt
您可用 pstree 看你 Linux processes
由此圖, 可看出 設計網路 server 程式 單獨使用 fork 可能不是很適合.
fork() system call
duplicate
1. file descriptor (* Notice the side effect, e.g., lseek)
2. global variables (* Notice copy-on-write), local variables (except pid, why?)
3. …
( If you want to know the details, you may try to understand the Linux process.)
On success, the PID of the child process is returned in the parent, and
0 is returned in the child.
On failure, -1 is returned in the parent,
no child process is created, and errno is set appropriately. ( #include <errno.h>)
child termination:
The termination signal of the child is always SIGCHLD. Notice you must handle signal SIGCHLD for important applications. See man 7 signal
pthread (執行緒)
Multithreaded Programming (POSIX pthreads Tutorial)
使用 pthread 的 echo server (有bug! why?)
C 語言 pthread 多執行緒平行化程式設計入門教學與範例, by G. T. Wang
Futher Reading (Linux kernel 的, 初學者可跳過):
Linux VFS (virtual file system) and System Call
context switching
根據上圖 FSM 的觀念, 必須將 process 的狀態 Mn 存入(save) 記憶體中, 等待換其執行前再(upload)已經紀錄的 Mn, 該processs始能繼續執行 . 至於等多久需視作業系統排程(scheduler)決定
Ref. State Machine Design in C
Note: 對於 某些 CPU 可能 引起 重大負擔, 甚至 Linux kernel crash
The handling of interrupts was split into two parts:
There are three types of deferred interrupts in the Linux kernel:
softirqs; # cat /proc/softirqs
tasklets;
enum
{
HI_SOFTIRQ=0,
TIMER_SOFTIRQ,
NET_TX_SOFTIRQ,
NET_RX_SOFTIRQ,
BLOCK_SOFTIRQ,
BLOCK_IOPOLL_SOFTIRQ,
TASKLET_SOFTIRQ,
SCHED_SOFTIRQ,
HRTIMER_SOFTIRQ,
RCU_SOFTIRQ,
NR_SOFTIRQS
};
workqueues;
Tasklet
workqueue can sleep and hold the lock for longtime.
kernel thread
Kernel threads are the basis of the workqueue mechanism. Essentially, a kernel thread is a thread that only runs in kernel mode and has no user address space or other user attributes
Ref. https://0xax.gitbooks.io/linux-insides/content/Interrupts/linux-interrupts-9.html
MC68451 MEMORY MANAGEMENTUNIT
第二十一天 Virtual Memory(虛擬記憶體)–(一)
linux kernel - how to get physical address (memory management)?
Linux 核心設計: 記憶體管理, by 宅色夫
The page size is typically 4096 bytes in Linux for x86-64 processors.
# Day 12 Cache and TLB Flushing Under Linux (四)
Professional
Linux® Kernel Architecture, by Wolfgang Mauerer
Page fault is an exception that the memory management unit (MMU) raises when a process accesses a memory page without proper preparations.
Content-addressable memory (CAM)
Logical and Physical Address in Operating System
Translation lookaside buffer
POSIX thread (pthread) libraries
The Linux Kernel documentation
See also
Pthread Scheduling, Fairness, Power
man 2 wait
A child that terminates, but has not been waited for becomes a
"zombie". The kernel maintains a minimal set of information
about the zombie process (PID, termination status, resource usage
information) in order to allow the parent to later perform a wait
to obtain information about the child. As long as a zombie is
not removed from the system via a wait, it will consume a slot in
the kernel process table, and if this table fills, it will not be
possible to create further processes.
Linux Signal
請注意 的default 行為 (man 7 signal)
Signal Standard Action Comment
SIGABRT P1990 Core Abort signal from abort(3)
SIGALRM P1990 Term Timer signal from alarm(2)
SIGBUS P2001 Core Bus error (bad memory access)
SIGCHLD P1990 Ign Child stopped or terminated
SIGCLD - Ign A synonym for SIGCHLD
SIGCONT P1990 Cont Continue if stopped
SIGEMT - Term Emulator trap
SIGFPE P1990 Core Floating-point exception
SIGHUP P1990 Term Hangup detected on controlling terminal
or death of controlling process
SIGILL P1990 Core Illegal Instruction
SIGINFO - A synonym for SIGPWR
SIGINT P1990 Term Interrupt from keyboard
SIGIO - Term I/O now possible (4.2BSD)
SIGIOT - Core IOT trap. A synonym for SIGABRT
SIGKILL P1990 Term Kill signal
SIGLOST - Term File lock lost (unused)
SIGPIPE P1990 Term Broken pipe: write to pipe with no
readers; see pipe(7)
SIGPOLL P2001 Term Pollable event (Sys V).
Synonym for SIGIO
SIGPROF P2001 Term Profiling timer expired
SIGPWR - Term Power failure (System V)
SIGQUIT P1990 Core Quit from keyboard
SIGSEGV P1990 Core Invalid memory reference
SIGSTKFLT - Term Stack fault on coprocessor (unused)
SIGSTOP P1990 Stop Stop process
SIGTSTP P1990 Stop Stop typed at terminal
SIGSYS P2001 Core Bad system call (SVr4);
see also seccomp(2)
SIGTERM P1990 Term Termination signal
SIGTRAP P2001 Core Trace/breakpoint trap
SIGTTIN P1990 Stop Terminal input for background process
SIGTTOU P1990 Stop Terminal output for background process
SIGUNUSED - Core Synonymous with SIGSYS
SIGURG P2001 Ign Urgent condition on socket (4.2BSD)
SIGUSR1 P1990 Term User-defined signal 1
SIGUSR2 P1990 Term User-defined signal 2
SIGVTALRM P2001 Term Virtual alarm clock (4.2BSD)
SIGXCPU P2001 Core CPU time limit exceeded (4.2BSD);
see setrlimit(2)
SIGXFSZ P2001 Core File size limit exceeded (4.2BSD);
see setrlimit(2)
signal 運作原理 (Signal hander is sigterm())
fork, wait, signal
fork() 後 open file, lseek, copy-on-write 的特別效應 請注意聽課
Linux 有些CPU 有 Kernel page-table isolation 的功能
fork copy-on-write
Under Linux, fork() is implemented using copy-on-write pages, so the only penalty that it incurs is the time and memory required to duplicate the parent's page tables, and to create a unique task structure for the child.
基本 virtual address, physical address, MMU 的觀念 ( 一般單晶片或簡單 SoC 可能沒有)
例如
if(發生機率高的條件){
程式碼A
}{
程式碼B
{
效率就會比較好! 尤其是 kernel, driver, library 的設計
What every systems programmer should know about concurrency, by Matt Kline.
mmap, munmap - map or unmap files or devices into memory
How to use mmap function in C language? by Bamdeb Ghosh
以下可能有兩種結果(故意設計的)~Why?
mmap
Linux 最有名的 mmap 應是 Framebuffer, 運用 user space 與 kernel space 溝通
另外 就是 運用 user space 控制與設定 硬體晶片 (大學部 要注意alignment 的問題, 有觀念想法就好)
Home Work: Add a system call
Ref. 增加一個 System Call 到 Linux Kernel (v4.x)