# OS-Application Interface ## System calls ![](https://i.imgur.com/Pw8ehI8.jpg) * 定義:作為 user process 與 kernel 之間的溝通界面,**當執行中的 process 需要 OS 提供某種服務時,則 user process 會對 OS 發出 system call 執行的請求(伴隨 ==Trap== 先行)**,當 OS 收到請求(含 system call ID 及相關參數)後,則會執行一連串的 system call 完成服務請求,並將服務結果回傳給 user process * system call 種類 * Process Control > ex: ```fork()```, ```exit()```, ```wait()``` * File management > ex: ```open()```, ```read()```, ```write()```, ```close()``` * Device management > ```ioctl()```, ```read()```, ```write()``` * Information maintenance > ```getpid()```, ```alarm()```, ```sleep()``` * Communication Process 間的 communication 且只針對 **message passing** 方式提供相關 system calls > ```pipe()```, ```shmget()```, ```chown()``` * 補充:UNIX system calls 多一種類別 → protection > ```chmod()```, ```umask()```, ```chown()``` * system call 參數傳遞方式 1. Register 2. Memory 3. Stack * ==\[法一\] 使 register 保存參數==,OS 再從 register 取得 * 優點:存取速度快,simple * 缺點:不適用於大量參數的情況 * ==\[法二\] 使用 memory,以**一個 table(or Block)** 來儲存這些參數,同時用**一個 register** 紀錄此 table 的起始位址==,then pass it to OS * 優點:適用於大量參數的情況 * 缺點:存取速度比 register 慢 * ==\[法三\] 使用 stack 保存參數,即 push 參數到 stack 裡面,OS 再從 stack 取得這些參數 * 優點:操作較為 simple * 缺點:stack space 預留要大一些,避免 overflow ![](https://i.imgur.com/o0gH3bi.png) ## API: Application Program Interface * Three most common APIs: * ==Win32 API== for Windows * ==POSIX API== for POSIX-based systems(including virtually all versions of UNIX, Linux, and Mac OS X) * POSIX 全名:**P**ortable **O**perating **S**ystem **I**nterface for Uni**x** * ==Java API== for the Java virtual machine(JVM) ## System Calls & API * System Calls * The **OS interface** to a running program * An explicit request to the **kernel** made via a **software interrupt** * Generally available as **assembly-language** instructions * API: Application Program Interface * **Users mostly program against API instead of system call** > 純粹是為了 programming 方便,還有方便 application 而設計的,跟作業系統無關 * Commonly implemented by language libraries (ex: C library) * An API call could involve **zero or multiple system call** * Both ```malloc()``` and ```free()``` use system call ```brk()``` * Math API functions, suck as ```abs()```, don't need to involve system call > 一個 API 可能會對應到好幾個 system call(一對多),也有可能一對 0(單純做數學運算),不用 OS ![](https://i.imgur.com/IS5L31z.png) * 圖片說明: * API 下才是 system call * API call 到 system call 才會間接的進到 OS 裡面叫 OS 完成 * Interface vs. Library * User program: ```printf("%d", exp2(intx, int y));``` * Interface: ```int exp2(intx, int y);``` * Library: Imp 1: ```int exp2(int x, int y){ for(int i=0; i<y; i++)x=x+2; return x; }``` Imp 2: ```int exp2(int x, int y){ x = x << y; return x; }``` Imp 3: ```int exp2(int x, int y){ return HW_EXP(x,y);}``` * API & System Call & OS Relationship ![](https://i.imgur.com/vSMTyH0.png) * user 的 application 會先去 call API 的 library,如果需要 OS 幫忙,API 就會再去 call system call,就會進到 OS 裡面(kernel mode),透過 interrupt 去 call service routine,然後再回到 API 的部份 ### Why Use API? * Simplicity * API is designed for applications * Portability * API is an unified defined interface * Efficiency * Not all functions require OS services or involve kernel > 加一層 API 是最有效的方式去 implement OS service,讓使用者不用這麼了解下層如何執行 ## Review * What are the two commmuniaction models provided by OS? > shared memory、mesage passing * What is the relationship between system calls, API and C library? * Whay use API rather than system calls? ###### tags: `OS` `共筆`