Module2 OS
Module2 OS
Module2 OS
7/23/12
Process management
• A process -can be thought of as a program in
execution.
7/23/12
• Dispatcher
– Component in CPU scheduling
– is a module that selects the process from the
ready queue to allocate the cpu (ie.It is a module
which gives control of the CPU to the process
selected by short term scheduler)
– It should be fast
– The time taken to stop one process and start
another is known as dispatch latency.
– Its function involves
• Switching context
• Switching to user mode
• Jumping to proper location in the user program to
restart that program
Process Control Block (PCB)
7/23/12
Information associated with PCB
1.Process state. – the state may be new, ready,
running, waiting, halted, and so on.
2.Program counter. – it indicates the address of the
next instruction to be executed for this process
3.CPU registers.-when an interrupt occurs these
details must be saved to continue its execution later.
Eg: accumulators, index registers, stack pointers etc.
The registers vary in number and type
4.CPU-scheduling information- information include
process priority, pointers to scheduling queues, and
any other scheduling parameters.
5.Memory-management information- this information
include value of base and limit registers, the page
tables, or the segment tables depending on the
memory system used by the OS.
• Device Queue
7/23/12
Context Switch
• Switching the CPU from one process to another
requires saving the state of the old process and
loading the saved state for the new process. This
task is known as Context Switch.
• Context of a process is represented in its PCB
• When a context switch occurs , the kernel saves the
context of the old process in its PCB and loads the
saved context of the new process scheduled to run.
• Context switch times are hardware dependent
• It is a 2 step process
– Save the context – to save PCB of current process
– Restore the context – restore PCB of old process to
resume operation
Operations on processes
• Process Creation
– A process (parent) can create many new
processes(child) via create-process System call
(fork()) and a child process in-turn can create other
processes forming a tree.
– Each process is identified using a unique processID
which is an integer value.
– A child process can obtain its resources directly or
from its parent
– A parent has to share its resources among its
children
– A child can have same or separate program and data
of its parent
– When a child process is created, there are 2
possibilities
• New process is created by fork() and the fork()
returns ‘0’ to the child and child ID to the parent.
• Exec() replaces the entire current process with a
new program.
Process creation
Parent Resumes
wait()
fork()
7/23/12
• Process Termination
– A process terminates when it finishes
executing its final statement and asks the OS
to delete it by using the exit() system call
– At this time the child process returns the
status(PID of a terminated process) value to
its parent process.
– Now all the resources are de-allocated by the
OS
– A process can terminate another process via
TerminateProcess() system call.
7/23/12
• A parent can terminate its children when
7/23/12
Inter process Communication
2 types of processes
1. Independent processes - cannot affect or be
affected by the other processes executing in the
system, they do not share data with other
processes
2. Cooperating processes - it can affect or be
affected by the other processes executing in the
system, they share data with other processes
• Reasons for process cooperation
– Information Sharing – when many users are
interested same piece of information,
simultaneous access to that information is needed
– Computation Speedup – tasks can run faster by
breaking one task into subtasks and these sub
tasks are executed parallel provided that there is
multiple processing elements(CPU or I/O channels)
– Modularity – system should be in modular fashion,
dividing the functions into separate processes or
threads
– Convenience – individual user can work on many
tasks at the same time like editing, printing,
compiling etc. in parallel
• There are two fundamental models of inter
process communication:
(1) Shared memory - a region of memory that
is shared by cooperating processes .
(2) Message passing - communication takes
place by means of messages exchanged
between the cooperating processes with OS
intervention.
CPU scheduler(short-term scheduler)
• It allocates process from main
memory(ready queue) to the CPU using
various scheduling algorithms.
• 2 types of scheduling
– Preemptive – it is prioritized. A running
process can be stopped forcibly by sending
interrupts when a high priority process comes
or due to some other reasons. In this, Good
mechanisms should be used to coordinate
access to shared data.
– Non-preemptive - once CPU has been allotted
for a process, the process keeps the CPU until
it releases either by terminating or by
switching to a waiting state.
7/23/12
Scheduling Criteria (in selecting CPU scheduling algorithms)
i) CPU utilization– keep the CPU as busy as possible.
CPU utilization should be in the range of 0 to 100
percentage.
ii) Throughput– no: of processes that complete their
execution per time unit. For long processes it may
be 1 process/hr and for short processes it may be
10 process/second.
iii) Turnaround time – amount of time b/n the
submission and completion of a process. It is the
sum of time spent waiting to get memory, waiting in
the ready queue, executing on CPU and doing I/O.
iv) Waiting time - amount of time a process has been
waiting in the ready queue. CPU scheduling
v. Response time– amount of time it takes from when a
request was submitted until the first response is produced,
not output (time it starts responding). It is limited by the
speed of the output device
• Scheduling algorithms are selected in way such that
– To maximize CPU utilization
– To maximize Throughput
– To minimize Turnaround time
– To minimize Waiting time
– To minimize Response time
• Scheduling deals with the problem deciding which of the
processes in the ready queue can be allocated to the CPU
Scheduling Algorithms
Process P1 P2 P3 P4
Arrival time 0 2 3 5
CPU burst(ms) 15 6 7 5
Process P4 P2 P3 P1
Arrival time 0 2 3 5
CPU burst(ms) 5 6 7 15
Solutions for problem1
P1 P2 P3 P4
0 15 21 28 33
7/23/12
Advantages
– Eliminates the variance in waiting time and turn
around time
Disadvantages
– It is little complicated to implement as the
exact length of CPU burst should be known in
advance
– it results in starvation of long processes
Shortest-Remaining-Time-First
Scheduling
• SJF can be either preemptive or non-
preemptive
• This is a type of preemptive SJF scheduling
• If a new process have a short CPU burst
than the left CPU burst time of currently
executing process, preemptive SJF
scheduling will preempt the currently
Process P1 P2 P3 P4
running
Arrival
process. AWT = 4
0 1 3 4 Att=8.25
Time
CPU burst
7 5 2 3
time
7/23/12
Solution for the above problem : Gantt
Chart
P1 P2 P3 P2 P4 P1
0 1 3 5 8 11
17
WT for p1 = (0-0)+(11-1)=10 TAT for p1 = 17-0=17
7/23/12
• Important Problem (both preemptive and non-preemptive
SJF)
Process P1 P2 P3 P4 P5
Arrival Time 0 10 10 80 85
CPU burst time 75 40 25 20 45
7/23/12
Solution - Non-Premptive AWT :5.25 ATA:9.25
P1 P3 P4 P2
0 7 10 12 16
P1 P2 P3 P4 P2 P1
0 1 3 6 8 10
16
7/23/12
Round-Robin Scheduling
• Each process in the ready queue gets a fixed
amount of CPU time (time quantum or time slice),
usually 10-100 milliseconds.
• After this time has elapsed, the process is
preempted and added to the end of the ready queue.
• Designed mainly for time sharing systems.
• Ready queue is treated as circular queue
• RR scheduling algorithm is preemptive
• Advantages
– Efficient for time sharing systems
– Increases fairness among processes
• Disadvantages
– Even short processes may take long time to
execute
– Requires extra hardware support such as timer
to interrupt after each time slice
7/23/12
Time Quantum and Context Switch Time
7/23/12