資訊人筆記

Work hard, Have fun, Make history!

使用者工具

網站工具


course:nctu-高等unix程式設計:chapter1

Overview and Tools

0x00 Outline

  • Prepare your UNIX environment
  • A Brief Introduction to the UNIX environment
  • Fundamental UNIX programming practices
  • Tools

0x01 Prepare your UNIX environment

使用 Ubuntu Linux (Debian-based) 為例,建議最少需要以下套件

  • gcc, g++, gdb, make
  • manpages-dev, manpages-posix, manpages-posix-dev
$ sudo apt-get install gcc g++ gdb make manpages-dev manpages-posix manpages-posix-dev

0x02 A Brief Introduction to the UNIX environment

  • The interface to the kernel is a layer of software called the system calls.
  • Libraries of common functions are built on top of the system call interface, but applications are free to use both.
  • The shell is a special application that provides an interface for running other applications.

Booting Process

開機後依序會經過下面流程

  • BIOS
  • OS Loader(e.g. grub): Loads kernel and an (optional) initial RAM disk into the memory
  • Kernel initializes system hardware components
  • Launch the first process: /sbin/init, /etc/init, /bin/init, and finally /bin/sh
  • init process
    • Mount file systems
    • Setup networks
    • Launch services
    • Provide the login interface

File System Architecture

Everything starts from the “root directory” (/)

UNIX Commands

  • Builtin commands: 由 login shell 提供
  • Other commands: 由 system administrators 安裝的套件
  • The Linux standard base (LSB): Linux operating system 的基本工具或 libraries

Common Notations

UNIX 文件中的提示符號

  • %, $ : 一般使用者輸入
  • # : root 權限輸入
  • [] : 可選用
  • … : 允許複數參數
  • -, – : 參數

Man page

$ man [section] page
  1. User commands
  2. System calls
  3. C library functions
  4. Devices and special files
  5. File formats and conventions
  6. Games et al.
  7. Miscellaneous
  8. System administration tools and daemons

0x03 Fundamental UNIX Programming Practices

#include <stdio.h>
 
int main() {
    printf("Hello, World.\n");
    return 0;
}

Return value of the main() function

  • a one byte value
  • zero: True
  • non-zero: False

Read return values from your program

  • Run echo $? immediately right after your program execution

Shell’s short cut branch

Boolean OR (||) – Evaluate until a condition is true

$ ./return 0 || echo 'A'
$ ./return 1 || echo 'B'
  • 第一個前面為 0 在 shell 中是 True,整個 OR 結果可知,後面 echo 'A' 不執行
  • 第二個前面為 1 在 shell 中是 False,整個 OR 結果未知,後面 echo 'B' 需執行

Boolean AND (&&) – Evaluate until a condition is not false

$ ./return 0 && echo 'C'
$ ./return 1 && echo 'D'
  • 第一個前面為 0 在 shell 中是 True,整個 AND 結果未知,後面 echo 'C' 需執行
  • 第二個前面為 1 在 shell 中是 False,整個 AND 結果已知,後面 echo 'D' 不執行

Handle program options

參數包含程式名稱本身

#include <unistd.h>
 
int getopt(int argc, char * const argv[], const char *optstring);
 
extern char *optarg;
extern int optind, opterr, optopt;
 
/*Return: option now if OK, -1 if no more options, Colon (:) or question mark (?) if Invalid option encountered*/
 
#include <getopt.h>
 
int getopt_long(int argc, char * const argv[],
           const char *optstring,
           const struct option *longopts, int *longindex);
 
int getopt_long_only(int argc, char * const argv[],
           const char *optstring,
           const struct option *longopts, int *longindex);
 
struct option {
    const char *name;
    int         has_arg;
    int        *flag;
    int         val;
};
  • argc: 參數個數
  • *argv[]: 參數
  • optstring: 由字母和冒號組成,字母表示可用參數,冒號表示該字母參數後面還需要額外補充參數
  • optind: 目前 getopt() 消耗的參數個數
  • optarg: 目前取到的補充參數
  • getopt_long(): 可取得一個 (-) 或兩個 (–) 減號的參數
  • getopt_long_only(): 只取得兩個 (–) 減號的參數

UNIX time representations

  • Wall clock time: time_t
    • 以秒為單位
    • 一般是 32-bit signed integer
    • 表示從 00:00:00, January 1st, 1970 UTC 至今經過的時間
    • 另外有 struct timeval,基本上是 time_t 加上微秒,較精準
struct timeval {
    long tv_sec; // seconds
    long tv_usec; // microseconds
};
  • CPU time: clock_t, in CPU-ticks unit
    • 單位是 CLOCKS_PER_SEC constant
    • 回傳程式開始執行後所使用的 cpu 時間

相關函式

  • time(3) function: Get time in time_t format
  • gettimeofday(2) function: Get time in struct timeval format
  • clock(3) function: Get time in clock_t format
#include<time.h>
 
time_t time(time_t *t);
int gettimeofday(struct timeval *tv, struct timezone *tz);
clock_t clock(void);

0x04 Tools

  • gcc
  • make/Makefile
  • GDB

0x05 參考資料

course/nctu-高等unix程式設計/chapter1.txt · 上一次變更: 127.0.0.1