學習 Linux

註:因為排版有點困難所以直接搬到這裡重新開始

Intro

C Language

Types

a value stored in an object or returned by a function

Types are partitioned into :
- function type
- object type
- incomplete type

你所不知道的C語言 指標篇

ISO/IEC 9899:TC3


a pointer points to a word.

doubly free 會造成 undefined behavior

free(NULL) 是合乎規範的用法,它不會做任何事情

在需要歸零的情況下 callocmalloc + memset更有效率

void * 存在的目的就是為了被轉型

Type qualifiers

const
  • float * has type ''pointer to float''
  • The const-qualified version of this type is designated as float * const. Its type is ''const-qualified pointer to float''.
  • whereas const float * has type ''pointer to const-qualified float''
restrict

An object that is accessed through a restrict-qualified pointer has a special association with that pointer.
If an object is accessed through one of the pointer parameters, then it is not also accessed through the other.

strict aliasing rule

https://cellperformance.beyond3d.com/articles/2006/06/understanding-strict-aliasing.html

The rule may be avoid by union.

compile

gcc
-o 生成目的檔案
-E only preprocessing
-c no linking
-Werror 把 warning 轉為 error
-Wall generate all warnings
-w no warning
-I specify header path
-L specify function library path
-static 連結成靜態函式庫
-g 包含 debug 資訊
-v 輸出編譯過程資訊
-O<n> 編譯器最佳化等級
n{0,1,2,}
預設為 O1

交換編譯:在一種平台上編譯,然後放到另一種平台執行。

objdump -d *.c 反組譯命令

gdb

語法
  • run ___
    • run ___ < ___ (run)
  • next
  • step
  • continue
  • print _
  • whatis _
  • break ___ [if ___]
  • set args __
    • set *argv[]

Makefile

https://hackmd.io/@sysprog/gnu-linux-dev/https%3A%2F%2Fhackmd.io%2Fs%2FSySTMXPvl

Linux kernel coding style

Linux kernel coding style

If only one branch of a conditional statement is a single statement, all the branches should use braces.

if (condition) { do_this(); do_that(); } else { otherwise(); }

GNU C

  • typeof() 取得 data type
typeof(*x) y;
  • variable length array
struct t { int length; char contents[0]; }; int myLength; struct t *p = malloc(sizeof(struct t) + myLength); p->length = myLength;
  • switch-case with ranges
static int local_atoi(const char *name) { int val = 0; for(;; name++) { switch (*name) { case '0' ... '9': val = 10 * val + (*name - '0'); break; default: return val; } } }
  • Designated Initializers

https://gcc.gnu.org/onlinedocs/gcc/Designated-Inits.html

  • __attribute__()

Computer Architechture

  • big endian
    • e.g. 1234
    • TCP/IP
  • little endian
    • e.g. 4321

cache

OS

  • Hard real time: The system can answer within a given maximum amount of time (guaranteed worst case)
  • Soft real time:

基本 Linux 操作與概念

POSIX = Portable Operating System Interface 規範 kernel 與 application 間的界面

鳥哥

鳥哥 Linux 權限操作與檔案管理

/var/lock/

/lib/modules/5.13.0-28-generic/kernel

arch CPU 相關程式碼
block
certs 證書機制
crypto 加密相關
Documentation
drivers
fs file system
include
init
ipc interprocess communication
kernel process management, scheduler, lock, e.t.c.
lib
mm memory management
net 網路通訊協定相關
samples
scripts
security
sound 音效卡相關
tools 一些開發工具,如 perf 等
usr
virt 虛擬化相關

usr: unix software resource

stream I/O

fd: file descriptor

中斷處理 interrupt handler

檔案系統 file system

Linux 抽象出 VFS (virtual file system) 軟體層,以便於支援多種檔案系統架構。


Linux kernel setting

  • make config
    • "y": 編譯進核心
    • "m": 編譯成模組
    • "n": 不編譯
  • make menuconfig : with GUI

設定檔 .config

Linked List

Linux kernel 於 <list.h> 中提供了 struct list_head 及一套框架來操作 list

Linux kernel API 文件

struct list_head {
    struct list_head *next, *prev;
}

使用上需要將 struct list_head 嵌入其它資料結構,並使用 container_of() 來獲取需要的內容。使用範例如下:

struct node { int data; struct list_head p; // note: it is not a pointer }; int main(void) { struct list_head h = LIST_HEAD_INIT(h); /* construct a list with 100 elements */ for (int i = 1; i <= 100; i++) { struct node *newNode = (struct node *) malloc(sizeof(struct node)); if (!newNode) goto clearlist; newNode->data = i; list_add_tail(&newNode->p, &h); } /* traverse the list and print each data */ struct list_head *it; list_for_each(it, &h) printf("%d\n", container_of(it, struct node, p)->data); clearlist: while (!list_empty(&h)) { struct list_head *curr = h.next; struct node *tmp = container_of(curr, struct node, p); list_del(curr); free(tmp); } return 0; }

疑惑區

  • argc/argv[]
  • static, extern
  • macro 與 pointer 之互動

skip part

  • QEMU lab
  • ARM structure