wiki:wade/linuxProgramming

Version 6 (modified by jazz, 15 years ago) (diff)

--

基礎 Linux Programming

安裝 GNU/Linux 開發手冊

  1. 安裝 manpages-dev 跟 glibc-doc-reference,其中 manpages-dev 提供常用 POSIX 的 C 函式庫 manpage,而 glibc-doc-reference 則提供 C 函式庫的 info 檔(內含更多範例程式)
    # sudo apt-get install manpages-dev glibc-doc-reference
    
  2. 更新 manpage
      mandb
    
  3. 當用語法
    • 查詢這所有 manpage 內「指令」的用法:
         man -a 指令
      

例如要查調 printf 對應到那些 manpages:

 man -a printf
  • 查詢特定 manpages 內「指令」用法:
       man [section] 指令
    

例如要查詢 (1) Executable programs or shell commands 類的 printf 的用法。

   man 1 printf

File Descriptors

可以想成他只是一個 index ,負責檔案的開啟與關閉,是一個 C type int。

  1. 特殊的 FD:
FD Name 功能描述
0 Standard Input (stdin) 標準輸入,如 keyboard
1 Standard Output (stdout) 標準輸出,如 monitor
2 Standard Error (stderr) 標準錯誤,特殊輸出

Pipe

  • FD[0] 負責 read pipe。FD[1] 負責 write pipe。 FD[1] → [PIPE] → FD[0]
  • read() 會被 block 住,所以不用擔心 parent 會比 child 先結束,而導致 child 也跟著結束 (因為 child process 是依附著 parent process 而存在,依存關係可透過 pstree 查詢)。
  • 先使用 pipe(FD[2]),再使用 fork() 時,FD[2] 也會同時複製兩份,child 跟 parent 有各自的 FD[0]、FD[1],所以要記得關掉不必要的 FD ,兩個 processes 各自與 pipe 相接。

Reference