Early computers allowed only one program to be executed at a time. This pro- gram had complete control of the system and had access to all the system’s resources. In contrast, contemporary computer systems allow multiple pro- grams to be loaded into memory and executed concurrently. This evolution required firmer control and more compartmentalization of the various pro- grams; and these needs resulted in the notion of a process, which is a program in execution. A process is the unit of work in a modern computing system.

The more complex the operating system is, the more it is expected to do on behalf of its users. Although its main concern is the execution of user programs, it also needs to take care of various system tasks that are best done in user space, rather than within the kernel. A system therefore consists of a collection of processes, some executing user code, others executing operating system code. Potentially, all these processes can execute concurrently, with the CPU (or CPUs) multiplexed among them. In this chapter, you will read about what processes are, how they are represented in an operating system, and how they work.

1. Process Concept

2. Process State

  • New: Load code into memory
  • Ready: Wait for CPU assignment(dispatch)
  • Running: Using CPU right now
  • Waiting: Trigger IO or slow event and wait for finish
  • Treminate

3. Process Control Block (PCB)

  • In Linux, there’s only Task Control Block (TCB)
  • 在 OS 中,會 implement 多個 priority ready queue (high priority process go to high priority queue, and vice versa)

4. Multiple Processors

Multi-processor vs. Multi-core

  • 每個 processor 有自己的 cache,但 memory 跟 IO 是共享的

  • 單個 processor 有多個 processing unit,又稱作 core,共享 cache, memory, IO

Multiple Processor OS

  1. Simple implementation

  • 每個 CPU 都有自己的 single-processor OS,但 memory 跟 IO 是共享的(memeory 會分區)
  • 問題:memory 已事先分好區域,每個 core 只能使用固定大小的 memory,缺乏彈性
  1. Master slave multiprocessor

  • 一個 processor 專門當作 master 的角色 (run OS),控制其它 processor 的運作
  • 問題:當 core 很多的時候,每個 slave processor 都要等待 master processor 下達命令,造成大量 overhead -> bottle neck
  1. Symmetric multiprocessor (SMP)
  • 為了解決 master-slave 的問題,改成 any processor 都可以 run OS,大家都可以 local 的決定運算資源的分配
  • 問題:每個 core 都是平等的(symmetric),耗電量較多
  1. Non-symmetric multiprocessor

  • 在 loading 較低的時刻,只使用耗電量較低的 core 運算

5. Process Scheduling

Two queues of processes

  • Ready queue: 已經準備好可以繼續執行的 process
  • Wait queue: 正在等待某個 event(e.g. I/O) 完成的 process

Process Workload

  • 因為每個 process 的 workload 跟需求不同,一些 naive 的排程方法(e.g. FIFO)是沒有效率的,因此將 process 分類就變得很重要
    • I/O-bound process.
    • CPU-bound process.
  • 理論上來說,CPU-bounf process 應該被分到比教多 CPU time,但 I/O-bound process 在進行 I/O 前,通常需要取得 lock,若使用不好的方式(如下)則可能會佔住 CPU time
    • Spin Lock:
      • 用 busy waiting 不斷去要求 lock,沒有實質貢獻還佔住 CPU
    • Mutex Lock:
      • 當 resource is availabe 時會發出 signal 通知在等待的 process

Context Switch

  • 不管是從 processor 到 ready queue(&反過來),還是 processor 到 waiting queue(沒有反過來),都是 context switch
  • context switch 是 pure overhead
Two Type of Context Switch
  • Voluntarily context switch
    • Call yeild() to yeild CPU
    • Call sleep()
    • Request I/O or lock
    • Call a system call that block
  • Inoluntarily context switch
    • Switch by Process/Thread scheduler by timeslicing
    • 佔用 core 太久
Context Switch Cost
  • Direct Cost
    • 把前一個 process 的 PCB 存起來 & 把下一個 process 的 PCB load 回來
  • Incerect Cost
    • Cold cache vs. Hot cache
    • 當 process 在進行時,他需要的 data 會暫時被存在 cache 裡(Hot cache)以便利用。但當進行 context switch 時,就需要把新的 process 需要的 data load 到 cache 裡,這就是 indirect cost

6. Communications in Client-Server Systems

Socket

  • Socket 是 commuication 兩端的 endpoint
  • Each socket will have a specified poet number

Server Side

  1. Set up socket (IP, port, …)
  2. listen(max_queue_len)
  3. accept() a connection
  4. fork() a child server
  • In Parent Side:
    • close the socket (already duplicated to child process)
  • In Child Side:
    • send and recieve messages from client
    • close connection

Client Side

  1. Set up socket (server’s IP, port, …)
  2. connect() to server
  3. close connection (by enter empty string)

7. Remote Procedure Call (RPC)

Recall: Conventional Procedure Call

Stub

  • 在 server/client 端擔任「代理人」的角色,幫助 接收&傳遞 system call 跟他的 parameters

Steps of RPC

Issue of RPC

  • 網路連線穩定度
  • 資料傳送 format : big/little endian