跳轉到

Basic

Process, Thread, Coroutine 是什麼?

Process 可視為程式的實體,不同的 Process 所分配到的記憶體空間 (Memory Space) 是互相獨立的

Process 的記憶體、變數可以讓其下所有 Thread 共享,也就是所有 Thread 都可以存取 Process 的記憶體和變數。而 Thread 之間也會共享記憶體、變數。

Process 分派到 CPU 後,Process 上的 Thread 獲得 CPU 時間,此時 CPU 就會開始執行 Thread 內的任務。

要注意的是,Process 是作業系統分配記憶體資源的對象,而 Thread 是作業系統分配 CPU 時間的對象,有 CPU 時間,才可以執行 Thread 上的任務。

以蓋房子當作例子:

  • 建設公司 => OS
  • 磚塊、水泥、鋼筋、怪手 => Memory
  • 建案 => Process
  • 建築工人 => Thread

設要蓋房子,建設公司就會分配磚塊、水泥、鋼筋、怪手….等建設資源給這個建案。建築工人就會利用建設資源來開始蓋房子。

Coroutine 也是任務 (Task),而 Coroutine 任務類似平常大家寫的 function,但又不像一般的 function,Coroutine 是可以隨時暫停執行,再從暫停的地方恢復執行的 function。

在 Python 中的 async, await, yield, generator…等,就是 Coroutine 很好的應用。

而在 Golang 中,除了使用 Coroutine 的機制暫停和恢復機制外,還另外加以優化,增加效能,在 Golang 中稱為 Goroutine。

Concurrency 是什麼?

Concurrency 表示任務進入 Thread 內的順序等於開始執行的順序,但不一定等於任務的結束順序。也就是說, Concurrency 代表的是同時處理多個任務(不是同時執行多個任務,同時執行多個任務的是 Parallelism)

當原本在執行任務的 Thread 讓出 CPU 時間後,接著會遇到 3 種適用 Concurrency 的情況:

  1. 該 Thread 所屬的 Process 上只有這個 Thread 或者有多個 Thread ,但也都被 Blocking 中。
  2. 該 Thread 所屬的 Process 尚有其他未被 Blocking 的 Thread
  3. 不論是上述 1 或 2 的情況,有其他優先權更高的 Process 要求 CPU 時間來執行任務。

如果一段代碼無論是順序執行還是併發執行,結果都是正確的,那就可以說這個代碼是併發安全的。

以下幾種現象會導致併發不安全:

Race Condition

多個 Thread 同時存取修改同一個資源時,會產生同步問題 (Synchronization),也就是 Race Condition,此時需要加鎖 (lock) 保護,以控制程式執行的流程。每個鎖同一時間只有一個 Thread 可以擁有他,Thread 擁有鎖的時候,才可以使用 CPU 時間來執行任務。

Deadlock

多個 Thread 分別取得鎖來執行各別的任務,但彼此互相等待任務執行完成才能繼續下去,此時就會產生 Deadlock,使程式無法繼續下去,其實就是種互搶資源造成的問題。

舉例來說,A Thread 有 moveLeft key 在執行任務, B Thread 有 moveRight key 在執行任務。A Thread 同時在等待 moveRight key ,才會將 moveLeft key 釋出,B Thread 同時在等待 moveLeft key ,才會將 moveRight key 釋出,兩個互相等待彼此,形成 Deadlock。

在多執行緒時 ( multi-threading ),常會產生 Race Condition, Deadlock 的問題,維持線程安全 (Thread-Safe) 就是個十分重要的課題。

Livelock

Livelock 不同於 Deadlock , Livelock 發生在彼此競爭但都願意讓步的情況,用生活上的例子就像是: 當我們走在行人道遇到對向的行人,兩個人都想讓路給對方卻又很剛好的站到同一邊,在電腦中,這樣看似有在執行卻毫無進展的情況就是 Livelock 。

Introduction of System Call

In computing, a system call is the programmatic way in which a computer program requests a service from the kernel of the operating system it is executed on.

A system call is a way for programs to interact with the operating system. A computer program makes a system call when it makes a request to the operating system’s kernel.

System call provides the services of the operating system to the user programs via Application Program Interface(API).

It provides an interface between a process and operating system to allow user-level processes to request services of the operating system.

System calls are the only entry points into the kernel system. All programs needing resources must use system calls.

Services Provided by System Calls

  1. Process creation and management
  2. Main memory management
  3. File Access, Directory and File system management
  4. Device handling(I/O)
  5. Protection
  6. Networking, etc.

States of a Process in Operating Systems

img

Difference between Swapping and Context Switching

Context switching

An operating system uses this technique to switch a process between states to execute its functions through CPUs.

It is a process of saving the context(state) of the old process(suspend) and loading it into the new process(resume).

It occurs whenever the CPU switches between one process and another. Basically, the state of CPU’s registers and program counter at any time represent a context.

Here, the saved state of the currently executing process means to copy all live registers to PCB(Process Control Block).

Moreover, after that, restore the state of the process to run or execute next, which means copying live registers’ values from PCB to registers.

img

Swapping

This is the process by which a process is temporarily swapped (moved) from the main memory (RAM) to the secondary memory (Disk).

The main memory is fast but has less space than secondary storage, so the inactive processes are moved to secondary memory, and the system swaps the memory from secondary to the main memory later.

During swapping, most of the time is spent transferring information, and the amount of memory swapped is directly proportional to the total time.

Swapping has been divided into two more concepts: Swap-in and Swap-out.

img

CPU Scheduling

CPU Scheduling is a process that allows one process to use the CPU while another process is delayed (in standby) due to unavailability of any resources such as I/O etc, thus making full use of the CPU.

The purpose of CPU Scheduling is to make the system more efficient, faster, and fairer.

Types of schedulers

Long term (Job Scheduler) – Performance

Makes a decision about how many processes should be made to stay in the ready state, this decides the degree of multiprogramming.

Once a decision is taken it lasts for a long time hence called long term scheduler.

Short term (CPU Scheduler) – Context switching time

Short term scheduler will decide which process to be executed next and then it will call dispatcher.

A dispatcher is a software that moves process from ready to run and vice versa. In other words, it is context switching.

Medium term – Swapping time

Suspension decision is taken by medium term scheduler. Medium term scheduler is used for swapping that is moving the process from main memory to secondary and vice versa.

Types of CPU scheduling algorithms

Preemptive Scheduling

Preemptive scheduling is used when a process switches from running state to ready state or from the waiting state to the ready state.

Non-Preemptive Scheduling

Non-Preemptive scheduling is used when a process terminates , or when a process switches from running state to waiting state.

img

Comparison

Parameter PREEMPTIVE SCHEDULING NON-PREEMPTIVE SCHEDULING
Basic In this resources(CPU Cycle) are allocated to a process for a limited time. Once resources(CPU Cycle) are allocated to a process, the process holds it till it completes its burst time or switches to waiting state.
Interrupt Process can be interrupted in between. Process can not be interrupted until it terminates itself or its time is up.
Starvation If a process having high priority frequently arrives in the ready queue, a low priority process may starve. If a process with a long burst time is running CPU, then later coming process with less CPU burst time may starve.
Overhead It has overheads of scheduling the processes. It does not have overheads.
Flexibility flexible rigid
Cost cost associated no cost associated
CPU Utilization In preemptive scheduling, CPU utilization is high. It is low in non preemptive scheduling.
Waiting Time Preemptive scheduling waiting time is less. Non-preemptive scheduling waiting time is high.
Response Time Preemptive scheduling response time is less. Non-preemptive scheduling response time is high.
Examples Examples of preemptive scheduling are Round Robin and Shortest Remaining Time First. Examples of non-preemptive scheduling are First Come First Serve and Shortest Job First.

Reference