Transcript Slide 1

Chapter 2: Processes
Topics
–
–
–
–
Processes
Threads
Process Scheduling
Inter Process Communication (IPC)
Reference:
Operating Systems Design and Implementation (Second Edition)
by Andrew S. Tanenbaum, Albert S. Woodhull
Processes: The Process Model
a) Multiprogramming of four programs
b) Conceptual model of 4 independent, sequential processes
c) Only one program active at any instant
Process Hierarchies
• Parent creates a child process, child processes can
create its own process
• Forms a hierarchy
– UNIX calls this a "process group"
• Windows has no concept of process hierarchy
– all processes created are equal
Process States (1)
Transition among states
• Possible process states
– running
– blocked
– ready
Process States (2)
• Lowest layer of process-structured OS
– handles interrupts, scheduling
• Above that layer are sequential processes
The Thread Model (1)
(a) Three processes each with one thread
(b) One process with three threads
The Thread Model (2)
Items shared by all threads
Items private to each thread
The Thread Model (3)
Each thread has its own stack
Thread Usage (1)
A word processor with three threads
Implementing Threads in User Space
Advantage
• Fast switching
among threads
Disadvantage
• when one thread
blocks, the kernel
blocks the entire
process
A user-level threads package
Implementing Threads in the Kernel
A threads package managed by the kernel
Process Scheduling
• When more than one process is runnable, the OS must
decide which one to run first.
• Scheduler makes this decision using some scheduling
algorithm
• Property of good scheduling algorithm
–
–
–
–
–
Fairness: Each process gets fair share of CPU
Efficiency: 100% utilization of CPU
Response time: minimize response time for users
Turnaround: minimize the time batch users must wait for output
Throughput: maximize the number of jobs processed per hour
Process Scheduling Algorithm
•
•
•
•
•
Round Robin Scheduling
Priority Scheduling
Shortest Job First
Guaranteed Scheduling
Lottery Scheduling
Round Robin Scheduling(1)
• Simplest, fairest and most widely used
• Each process is assigned a quantum (time interval) which it is allowed
to run
• If the process is still running at the end of quantum, the CPU is
preempted and given to another process
• Also the CPU switches when a process blocks
• All Processes are equally important
list of runnable processes
list of runnable processes after
B uses up its quantum
Round Robin Scheduling(2)
• Suppose quantum is 20 ms and context switch
takes 5 ms (total 25 ms)
Cause 20% CPU time wastage
• If quantum is 500 ms
Cause less than 1% CPU time wastage but poor
response time
Priority Scheduling
 Each process is assigned a priority and the runnable process with the
highest priority is allowed to run
 To prevent high-priority processes from running indefinitely, the
scheduler may decrease the priority of currently running process at
each clock tick.
Round Robin
 I/O bound processes are given highest priority
A scheduling algorithm with four priority classes
Shortest Job First
• Appropriate for batch jobs
• Mean Turnaround time is optimal
• Consider 4 jobs with run time of a, b, c and d respectively.
•The mean turnaround time is (4a +3b +2c + d)/4
Shortest job first scheduling
Interprocess Communication (IPC)
• Processes frequently need to communicate with
other processes.
• Example
– In a shell pipeline, the output of the first process must be
passed to the second process
• Generally shared memory is used to establish the
connection among processes.
• This memory must be shared carefully to avoid
inconsistency
IPC: Race Condition
Race Condition
The situation where 2
or more processes are
reading or writing some
shared data is called race
condition
Two processes want to access shared memory at same time
IPC: Critical Regions (1)
Critical Region/ Critical Section
The part of the program where the shared memory is
accessed is called the critical region
Four conditions to provide mutual exclusion




No two processes simultaneously in critical region
No assumptions made about speeds or numbers of
CPUs
No process running outside its critical region may block
another process
No process must wait forever to enter its critical region
IPC: Critical Regions (2)
Mutual exclusion using critical regions
How to Manage Race Condition
• Mutual Exclusion with Busy Waiting
– Strict Alternation
– Peterson’s Solution
• Mutual Exclusion without Busy Waiting
–
–
–
–
Sleep and wakeup
Semaphore
Message Passing
Monitor
Strict Alternation
Process 1
Process 2
Drawback
• Not a good idea when one process is much slower than the
other. Why?
• Wastage of CPU time by busy waiting
Peterson’s Solution
Drawback
Busy Waiting
Sleep and Wakeup: Consumer Producer Problem
Drawback
Both may sleep forever
Solution
Use wakeup waiting bit
What happens if more
than one consumer?
More waiting bit ?
Problem is still there.
Semaphores: Consumer Producer Problem(1)
Semaphore
An integer to keep count of wakeups saved for future use
Down (semaphore)
• If value > 0, Decrement it and continue (one stored wakeup is used)
• If value = 0, Process is put to sleep without completing Down
Up (semaphore)
if one or more processes were sleeping on that semaphore (unable to
complete down operation), one of them is chosen by the system at
random and allowed to complete its Down
Atomic Action
Checking value, changing it and possibly going to sleep
Semaphores: Consumer Producer Problem(2)
NO busy wait
Monitors (1)
• One have to be very careful while
using semaphore
• Monitor is like class where only one
procedure is active at one time
It is sufficient to put only the critical
regions into monitor procedures as no
two processes will ever execute their
critical regions at the same time
Monitors (2)
Block when buffer full
Send signal when buffer has one more slot
Only one monitor procedure active at one time
Monitors (3)
Solution to producer-consumer problem in Java (part 1)
Monitors (4)
Solution to producer-consumer problem in Java (part 2)
Message Passing
• No shared memory
• N message is analogous to
N slots in a shared buffer