Title: CS414 Review Session I
1CS414 Review Session I
2Todays Agenda
- Brief Overview of Syllabus
- Processes and Threads
- Process Scheduling
- Process Synchronization
- Deadlocks
- Example Questions and Solutions
- Question Time
3Processes vs Threads
4Processes
- Code Segment
- Data Segments
- Resources
- Files open, Devices open, System resources
- Process Control Block
- Process state, book-keeping, access rights
5Threads
- Individual Threads
- Stack
- Instruction Pointer
- Thread Control Block
- Register image, thread state, priority info etc
- Shared with other threads of that process
- Memory segments, Code segments
- Resources
6User level threads
- The kernel is not aware of the existence of
threads - All thread management is done by the application,
using a thread library - Thread switching does not require kernel mode
privileges - Scheduling is application specific
7Plus and Minus of ULTs
- Advantages
- Thread switching does not involve the kernel no
mode switching - Scheduling can be application specific choose
the best algorithm. - ULTs can run on any OS. Only needs a thread
library
- Inconveniences
- Most system calls are blocking and the kernel
blocks processes. So all threads within the
process will be blocked - The kernel can only assign processes to
processors. Two threads within the same process
cannot run simultaneously on two processors
8Kernel Level Threads
- All thread management is done by kernel
- No thread library but an API to the kernel thread
facility - Kernel maintains context information for the
process and the threads - Switching between threads requires the kernel
- Scheduling occurs on a thread basis, usually
- Ex Windows NT and OS/2
9Plus and Minus of KLTs
- Advantages
- the kernel can simultaneously schedule many
threads of the same process on many processors - blocking is done on a thread level
- kernel routines can be multithreaded
- Inconveniences
- thread switching within the same process involves
the kernel. We have 2 mode switches per thread
switch - this results in a significant slow down
-
10Process/Thread State Diagram
11Process/Thread Scheduling
- Long Term Scheduling
- Admission Control
- I/O bound vs CPU bound
- Medium Term Scheduling
- Number of processes in memory (SWAPPER)
- Short Term Scheduling
- Select from ready processes (Dispatcher)
- Pre-emption vs non-preemption
12Scheduling Metrics
- Completion Time
- Finish time of a process
- Turn Around Time
- Finish time Start time
- CPU bound jobs
- Response Time
- Time at which first response to user is given
- I/O bound jobs
- Throughput
- Number of processes completed per unit time.
13Scheduling Policies
- First Come First Serve
- Shortest Job First (Shortest Remaining Time
First) - Round Robin Scheduling
- Priority Based Scheduling
14Multiple Feedback Queues
Different RQs may have different quantum values
15Synchronization Primitives
- Semaphores
- Condition Variables
- Monitors
16Semaphores (Operations)
- Initialize counter (VERY IMPORTANT)
- Wait or P or Down (blocks process)
- Signal or V or Up (releases process)
- BEWARE
- Dont assign values to count (S.count -1)
- Dont read values of count (if S.count -1)
- Use only the above operations.
17Monitors
- Shared Variables.
- Only one process currently inside a monitor.
- Block on a condition variable.
- Wait( c )
- Another process releases the variable.
- Signal( c )
18Synchronization Tips
- Shared Variables
- Always use mutex semaphores to access shared
variables. - Or use monitors for shared variables.
- Synchronization
- Use semaphores or condition variables for
synchronization - Dont forget to initialize.
19How to implement Semaphores?
- Hardware
- Atomic instructions (tset, xchng)
- Software
- Enable/disable interrupts
- Spinlocks (multi-processor systems)
20Things to avoid!!!
- Race Conditions
- NO SYNCHRONIZATION
- Deadlocks
- Busy Waiting
- Starvation
21Conditions for deadlock
- Mutual Exclusion
- Hold and Wait
- No preemption (of resources)
- Circular Wait
22Resource Allocation Graph
R1
R3
P1 is holding an instance of R2 and waiting for
an instance of R1 P2 is holding an instance of R1
and R2 and waiting for an instance of R3 P3 is
holding an instance of R3
R1
P1
P2
P3
R2
R4
23Example 1 (review question 1)
- Flight Reservation Algorithm
- (Ithaca, Miami, Dallas, San Diego, Seattle)
- One server per city and one request per server at
a time - Only decides about out going flights
- Connecting flights both legs confirmed
- Eg Mr. Mosse wants ticket from Ithaca to San
Diego via Dallas - Server at Ithaca sends a request to server at
dallas, waits for confirmed ticket before booking
from ithaca to dallas.
24Example 1 contd
- 1a) Solve the synchronization problem using
semaphores. - Request should not be served until server is free
- Server should not start until there is a request
- Write down procedures for client and server for
booking tickets
25Solution 1 (Wrong)
- Client
- Synch 0
- begin
- submit request
- V(synch)
- P(mutex)
- processing request
- end
- Server
- Mutex 1
- repeat
- P(synch)
- service request
- V(mutex)
- until false
26Solution 2
- Client
- mutex 1 synch 0
- begin
- P(mutex)
- submit request
- V(sync)
- process reply
- end
- Server
- repeat
- P(sync)
- service request
- V(mutex)
- until false
27Example 1 contd
- 1b) Describe a deadlock scenario in this
problem. Show that all 4 conditions hold. - Solution
- Mutual exclusion only one request at a time
- Hold-Wait Wait for a connection flight
- No Preemption Got to wait for reply
- Circular Wait
- A requests Ithaca to San Diego via Dallas
- B requests Dallas to Ithaca via San Diego
- C requests San Diego to Dallas via Ithaca
28Example 1 (contd)
- 1c) Give a strategy to remove deadlock.
- Order Cities in alphabetical order.
- Always process requests in alphabetical order
- A requests from Dallas to San Diego before Ithaca
to Dallas - B requests Dallas to San Diego before San Diego
to Ithaca - RAG wont work with 5 servers because a cycle in
a RAG does not imply deadlock.
29Example 2 (review question 2)
- There are 3 processes
- Process Run Time per thread threads
- P1 6 1
- P2 3 2
- P3 2 3
30Example 2 contd
- a) Kernel Level Threads with preemptive round
robin scheduling . How will the first 6 time
units be scheduled? - 1 time unit per each of the 6 threads
- 1 quantum for P1, 2 for P2 and 3 for P3
- b) User Level Threads
- 2 time units per process.
31Example 3 (review question 3)
- Communicating Monitors
- A process executing a procedure in a monitor
could call a procedure in some other monitor. - When a process waits to enter the other monitor,
it is still considered active in the first
monitor. - What is wrong in allowing this to happen?
- Deadlock could arise Process A in monitor M
calls a procedure in monitor N where B is
currently active. If B calls a procedure in
monitor M. Then there is a deadlock.
32Example 4
- Shower Room in a Co-Ed Dorm
- There is a shower room in a co-ed dorm with
plenty of showers (infinite!!!) - Both men and women come to the shower room to
take a shower. - Give a synchronized procedure for both men and
women to use the shower room subject to following
conditions
33Example 4 contd
- 1. Any number of male students can use the
showers at the same time - 2. Any number of female students can use the
showers at the same time - 3. While male students are using the showers,
female students wait. - 4. While female students are using the
showers, male students wait. - 5. If any female student is waiting and a
male student is using the showers, no additional
male student can enter until all that female and
any others who are waiting have gotten in - 6. If any male student is waiting and a
female student is using the showers, no
additional female students can enter until that
male and any others who are waiting have gotten
in.
34Solution
- What do we need to take care of?
- Make women wait when men are taking bath
- Make men wait when women are taking bath
- Use synchronization semaphores for this
- What do need for book keeping
- Number of men and women waiting or using showers
- Shared variables need mutex semaphore.
35Solution to Example 4
- var nm, nw, wm, wf Integer 0,0,0,0
- var mwait, wwait, mutex Semaphore 0,0,1
- Nm Number of men showering
- Nw Number of women showering
- Wm Number of men waiting
- Ww Number of women waiting
36Solution contd
- MEnter
- P(mutex)
- if(nw 0 or ww 0) wm wm1 V(mutex)
P(mwait) - else nm nm1 V(mutex)
- MLeave
- P(mutex)
- nm nm-1
- if(nm 0 and ww 0)
- while(ww0) ww ww-1 nw nw1 V(wwait)
-
- V(mutex)
37Solution contd
- WEnter
- P(mutex)
- if(nm 0 or wm 0) ww ww1 V(mutex)
P(wwait) - else nw nw1 V(mutex)
- WLeave
- P(mutex)
- nw nw-1
- if(nw 0 and wm 0)
- while(wm0) wm wm-1 nm nm1 V(mwait)
-
- V(mutex)
-
38Question Time
- Good Luck for the Prelim.