Process management and Inter process communication
Process management and Inter process communication
and
Inter process communication
What is Process?
Program Process
What is Process?
P P P Memory
1 2 3 Logical
Program Counter
P1
Processor
Logical Physical
Program Counter Program Counter
P2
Logical
Program Counter
P3
There are three processes, one processor (CPU), three logical program
counter (one for each processes) in memory and one physical program counter in
processor.
Here CPU is free (no process is running).
No data in physical program counter.
Multiprogramming execution
P P P Memory
1 2 3 Logical
Program Counter
P1
P Processor
1
Logical Physical
Program Counter Program Counter
P2
Logical
Program Counter
P3
P P P Memory
1 2 3 Logical
Program Counter
P Processor
1
Logical Physical
P
Program Counter Program Counter
2 P
P2
1
Logical
Program Counter
P3
9
Multiprogramming execution
P P P Memory
1 2 3 Logical
Program Counter
P1
Processor
Logical Physical
P
Program Counter Program Counter
2 P
P 2
3
Logical
Program Counter
P3
10
Process Model
Multiprogramming of Conceptual model of 4 independent, Over a long period of time interval, all the
four programs in memory sequential processes, each with its own flow processes have made progress, but at any
of control (i.e., its own logical program given instant only one process is actually
counter) and each one running independently running.
of the other ones.
Process State
As a process executes, it changes state
new: The process is being created
running: Instructions are being executed
waiting: The process is waiting for some event to occur
ready: The process is waiting to be assigned to a processor
terminated: The process has finished execution
Process Control Block (PCB)
Information associated with each
process (also called task control
block)
Process state – running, waiting, etc
Program counter – location
of instruction to next execute
CPU registers – contents of all process-
centric registers
CPU scheduling information-
priorities, scheduling queue pointers
Memory-management information –
memory allocated to the process
Accounting information – CPU
used, clock time elapsed since start,
time limits
I/O status information – I/O
devices allocated to process, list of
open files
Context Switch
When CPU switches to another process, the system must save the
state of the old process and load the saved state for the new process via
a context switch
Context of a process represented in the PCB
Context-switch time is overhead; the system does no useful work while
switching
The more complex the OS and the PCB the longer the context switch
Time dependent on hardware support
Some hardware provides multiple sets of registers per CPU
multiple contexts loaded at once
CPU Scheduler
Gantt chart
P1 P2 P3 P4 P5
0 4 7 8 10 15
First- Come, First-Served (FCFS) Scheduling
Gantt chart
P1 P2 P3
0 21 24 26
Convoy effect
Gantt chart
P1 P2 P3
0 21 24 26
Gantt chart
P2 P3 P1
0 3 5 26
Gantt chart
P1 P3 P4 P2 P5
0 1 8 9 11 16 24
Shortest-Job-First (SJF) Scheduling
Gantt chart
P1 P3 P4 P2 P5
0 1 8 9 11 16 24
AVG TAT: ?
AVG WT: ?
shortest-remaining-time-first
Gantt chart
P1
0 1
shortest-remaining-time-first
Gantt chart
P1 P2
0 1 2
shortest-remaining-time-first
Gantt chart
P1 P2 P3
0 1 2 3
shortest-remaining-time-first
Gantt chart
P1 P2 P3 P4
0 1 2 3 4
shortest-remaining-time-first
5 4 2
6 5 1
NOW p3 and p5 both have same time : pick based on A.T, so p3 is picked
Gantt chart
P1 P2 P3 P4 P3
0 1 2 3 4 5
shortest-remaining-time-first
5 4 2
6 5 1
NOW p3 and p6 both have same time : pick based on A.T, so p3 is picked
Gantt chart
P1 P2 P3 P4 P3 P3
0 1 2 3 4 5 6
shortest-remaining-time-first
5 4 2
6 5 1
Gantt chart
P1 P2 P3 P4 P3 P3 P6
0 1 2 3 4 5 6 7
shortest-remaining-time-first
5 4 2
Gantt chart
P1 P2 P3 P4 P3 P3 P6 P5
0 1 2 3 4 5 6 7 9
shortest-remaining-time-first
Gantt chart
P1 P2 P3 P4 P3 P3 P6 P5 P2 P1
0 1 2 3 4 5 6 7 9 13 19
shortest-remaining-time-first
Gantt chart
P1 P2 P3 P4 P3 P3 P6 P5 P2 P1
0 1 2 3 4 5 6 7 9 13 19
AVG TAT:?
AVG WT:?
Round Robin (RR)
Each process gets a small unit of CPU time (time quantum q),
usually 10-100 milliseconds. After this time has elapsed, the
process is preempted and added to the end of the ready queue.
If there are n processes in the ready queue and the time
quantum is q, then each process gets 1/n of the CPU time in
chunks of at most q time units at once. No process waits more
than (n-1)q time units.
Timer interrupts every quantum to schedule next process
Performance
q large FIFO
q small q must be large with respect to context switch,
otherwise overhead is too high
Round Robin (RR) Algorithm
Round Robin (RR) Example
TQ= 2
P1 P2 P3 P1 P4 P5 P2 P6 P5 P2 P6 P5
0 2
AVG4TAT:?6 8 9 11 13 15 17 18 19 21
AVG WT:?
Round Robin (RR) Example
What if we have TQ= 4 and TQ= 1 ?
available = false;
}
release() {
available = true;
}
do {
acquire lock
critical section
release lock
remainder section
} while (true);
Semaphore
Synchronization tool that provides more sophisticated ways (than Mutex locks)
for process to synchronize their activities.
Semaphore S – integer variable
Can only be accessed via two indivisible operations
wait() and signal()
Originally called P() and V()
Definition of the wait() operation
wait(S) {
while (S <= 0); // busy wait
S--;
}
Definition of the signal() operation
signal(S) {
S++;
}
do {
wait(s)
critical section
signal(s)
remainder section
} while (TRUE);
Pi requests instance of Rj
Pi
Rj
Pi is holding an instance of Rj
Pi
Rj
Example of a Resource Allocation Graph
Resource Allocation Graph With A Deadlock
Graph With A Cycle But No Deadlock
Basic Facts
Detection algorithm
Recovery scheme
Recovery from Deadlock: Process Termination