Operating System

CPU bound process: These are those processes that require most of the time on CPU.
I/O bound process: These are those processes that require most of the time on I/O devices.
N.B: A good CPU scheduling idea should choose the mixture of both so that both I/O devices and CPU can be utilized correctly.
CPU scheduling are of two types: i) Non-Pre-emptive, ii) Pre-emptive
i) Non-Pre-emptive: We can't forcefully terminate this type of process. This process complete it's execution and leave CPU to perform other operation.
ii) Pre-emptive: If a process enters in the ready state from new or waiting state and high priority process.
CPU scheduling terminiology
i) Burst Time / Execution Time / Running Time: It's the time process require for running on CPU.
ii) Waiting Time: Time spend by a process in ready state waiting for CPU.
iii) Arrival Time: When a process enters ready state.
iv) Exit Time: When a process completes execution and exit from system.
v) Turn Around Time: It's the time process require for running on CPU. T.A.T == E.T - A.T == B.T + W.T
vi) Response Time: It's the time between a process enters ready queue and get scheduled on the CPU for the first time.
CPU scheduling algorithm criteria
i) Average waiting time: Average waiting time should be as low as possible
ii) Average response time: Average response time should be as low as possible
iii) CPU utilization: CPU utilization should be a high all time.
iv) Throughput: Number of process execution per unit time.
FCFS (First Come First Serve)
i) Simplest scheduling algorithm, it assigns CPU to the process which arrives first.
ii) Easy to understand and can easily be implemented using Queue data structure.
iii) Always non-pre-emptive in nature.

Chart

Pid A.T B.T T.A.T = E.T - A.T W.T = T.A.T - B.T
A 3 4 7 - 3 = 4 4 - 4 = 0
B 5 3 13 - 5 = 8 8 - 3 = 5
C 0 2 2 - 0 = 2 2 - 2 = 0
D 5 1 14 - 5 = 9 9 - 1 = 8
E 4 3 10 - 4 = 6 6 - 3 = 3

Gantt Chart

C
0 - 2
idle
2 - 3
A
3 - 7
E
7 - 10
B
10 - 13
D
13 - 14
Convoy Effect
Smaller process have to wait for long time for bigger process to release CPU.
Advantage: Simple, easy to use, easy to understand, easy to implement, must be used for background process where execution is not urgent.
Disadvantage: Suffer from convoy effect, normally higher average waiting time, no consideration on priority or burst time, should not be used for interactive system.
Shortest Job First (Non-Pre-emptive) / Shortest Remaining Time First (Pre-emptive)
i) Process, who have small burst time will assigned first.
ii) In case of tie, FCFS is used.
iii) It uses both pre-emptive and non-pre-emptive approach.

Chart (Non-Pre-emptive)

Pid A.T B.T T.A.T = E.T - A.T W.T = T.A.T - B.T
A 3 1 7 - 3 = 4 4 - 1 = 3
B 1 4 16 - 1 = 15 15 - 4 = 11
C 4 2 9 - 4 = 5 5 - 2 = 3
D 0 6 6 - 0 = 6 6 - 6 = 0
E 2 3 12 - 2 = 10 10 - 3 = 7

Gantt Chart

D
0 - 6
A
6 - 7
C
7 - 9
E
9 - 12
B
12 - 16

Chart SRTF (Pre-emptive) Optimal (uses purely greedy)

Pid A.T B.T T.A.T = E.T - A.T W.T = T.A.T - B.T
A 3 1 4 - 3 = 1 1 - 1 = 0
B 1 4 6 - 1 = 5 5 - 4 = 1
C 4 2 8 - 4 = 4 4 - 2 = 2
D 0 6 16 - 0 = 16 16 - 6 = 10
E 2 3 11 - 2 = 9 9 - 3 = 6

Gantt Chart

D
0 - 1
B
1 - 3
A
3 - 4
B
4 - 6
C
6 - 8
E
8 - 11
D
11 - 16
Advantages:
i) SRTF generates minimal average waiting time.
ii) Provide a standard for other algo in terms of average waiting time.
iii) Better average response time compare to FCFS.
Disadvantages:
i) Algo can't be implemented as there is no way to know the burst time of a process.
ii) Process with longer CPU burst time requirement will go into starvation.
iii) No idea of priority, process with large burst time have poor response time.
Priority Algorithm (Non-Pre-emptive)
i) Here a priority is associated with each process.
ii) At any instance of time out of all available process, CPU s allocated to the process which has the highest priority (number maybe higher or lower)
iii) Tie is broken using FCFS order.
iv) No importace to A.T or B.T.
v) Supports both non-pre-emptive and pre-emptive version

Chart (Non- pre-emptive)

Pid A.T B.T Priority T.A.T = E.T - A.T W.T = T.A.T - B.T
A 0 4 2 4 - 0 = 4 4 - 4 = 0
B 1 3 3 15 - 1 = 14 14 - 3 = 11
C 2 1 4 12 - 2 = 10 10 - 1 = 9
D 3 5 5 9 - 3 = 6 6 - 5 = 1
E 4 2 5 11 - 4 = 7 7 - 2 = 5

Gantt Chart

A
0 - 4
D
4 - 9
E
9 - 11
C
11 - 12
B
12 - 15

Chart (Pre-emptive)

Pid A.T B.T Priority T.A.T = E.T - A.T W.T = T.A.T - B.T
A 0 4 2 15 - 0 = 15 15 - 4 = 11
B 1 3 3 12 - 1 = 11 11 - 3 = 8
C 2 1 4 3 - 2 = 1 1 - 1 = 0
D 3 5 5 8 - 3 = 5 5 - 5 = 0
E 4 2 5 10 - 4 = 6 6 - 2 = 4

Gantt Chart

A
0 - 1
B
1 - 2
C
2 - 3
D
3 - 8
E
8 - 10
B
10 - 12
A
12 - 15
Advantages:
i) provides a facility of priority speacially for system process.
ii) Allows to run important process first even if it is a userprocess.
Disadvantages:
i) Here process with the smaller priority may starve for the CPU.
ii) No idea of response time or waiting time.
N.B: - Ageing is a technique of gradually increasing the parity of process that wait in the system for long time.
Round-Robin

i) Designed for time sharing system.
ii) It divides time of the CPUamong the processes in ready state, and circularly repeat the same process.
iii) Here we fix a time quantum for a process to execute a process. After time quantum limit exceed the next process will come to queuefor executing.
iv) RR is pre-emptive in nature.

Chart (Pre-emptive) TQ = 2

Pid A.T B.T T.A.T = E.T - A.T W.T = T.A.T - B.T
A 0 5 13 - 0 = 13 13 - 5 = 8
B 1 3 12 - 1 = 11 11 - 3 = 8
C 2 1 5 - 2 = 3 3 - 1 = 2
D 3 2 9 - 3 = 6 6 - 2 = 4
E 4 3 11 - 4 = 7 7 - 3 = 4

Gantt Chart

A
0 - 2
B
2 - 4
C
4 - 5
A
5 - 7
D
7 - 9
E
9 - 11
B
11 - 12
A
12 - 13
E
13 - 14
Advantages:
i) Perform best in terms of response time.
ii) Works well in case of time sharing, client server architecture and intermediate system.
iii) Kind of SJF implementation.
Disadvantages:
i) Bigger process may starve.
ii) performance depends ehavily on time quantum.
iii) No idea of priority.
Process Synchronization
Race Condition: - multiple processes are involved in the system and these process access some shared resources now if they access in a mix fashion then system gives different result sometime.
Critical Section Problem
i) Mutual Exclusion - Critical Sectin will only be accessed by single process at a time.
ii) Progress - Those process should enter C.S who wants to.
iii) Bounded Wait (optional) - After a specific space of time the process should exit CS to overcome starving.
Semaphore
A semaphore is an integer variable int s that apart from initialization is accessed through two atomic standards.
    i) wait(s) => wait(s){
      while(s <= 0);
      s--;
    }
    ii) signal(s) => signal(s){
      s++;
    }
  
Code
  do{
    wait(s);
    //critical Section
    signal(s)
    //remainder section
  }
  while(true);
Uses of Semaphore:
i) Critical Section Problem solving.
ii) To decide order of execution.
iii) For managing resources.
What is Deadlock? If all the resources are circularly depending on each other then the state is called deadlock. Four condition of deadlock:
i) Mutual Exclusion
ii) Hold and Wait
iii) non-pre-emtion process
iv) Circular wait
Deadlock handling method:
i) Prevention
ii) Avoidance (banker's algo)
iii) detection and recovery
iv) Ignorance / ostrich method