cs414 review session i rama. today’s agenda brief overview of syllabus – processes and threads...

38
CS414 Review Session I Rama

Post on 20-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

CS414 Review Session I

Rama

Today’s Agenda

Brief Overview of Syllabus– Processes and Threads– Process Scheduling– Process Synchronization– Deadlocks

Example Questions and Solutions Question Time

Processes vs Threads

Processes

Code Segment Data Segments Resources

– Files open, Devices open, System resources

Process Control Block– Process state, book-keeping, access rights

Threads

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

User 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

Plus 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

Kernel 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

Plus 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

Process/Thread State Diagram

Process/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

Scheduling 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.

Scheduling Policies

First Come First Serve Shortest Job First (Shortest Remaining Time

First) Round Robin Scheduling Priority Based Scheduling

Multiple Feedback Queues

Different RQs may have different quantum values

Synchronization Primitives

Semaphores Condition Variables Monitors

Semaphores (Operations)

Initialize counter (VERY IMPORTANT) Wait or P or Down (blocks process) Signal or V or Up (releases process) BEWARE

– Don’t assign values to count (S.count = -1)– Don’t read values of count (if S.count == -1)– Use only the above operations.

Monitors

Shared Variables. Only one process currently inside a monitor. Block on a condition variable.

– Wait( c )

Another process releases the variable.– Signal( c )

Synchronization 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– Don’t forget to initialize.

How to implement Semaphores?

Hardware– Atomic instructions (tset, xchng)

Software– Enable/disable interrupts– Spinlocks (multi-processor systems)

Things to avoid!!!

Race Conditions– NO SYNCHRONIZATION

Deadlocks Busy Waiting Starvation

Conditions for deadlock

Mutual Exclusion Hold and Wait No preemption (of resources) Circular Wait

Resource Allocation Graph

P1 P2 P3

R1 R3

R2R4

R1

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

Example 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.

Example 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:

Solution 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;

Solution 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

Example 1 contd…

1b) Describe a deadlock scenario in this problem. Show that all 4 conditions hold.

Solution:Mutual exclusion : only one request at a timeHold-Wait : Wait for a connection flightNo Preemption : Got to wait for replyCircular Wait :

A requests : Ithaca to San Diego via DallasB requests : Dallas to Ithaca via San DiegoC requests : San Diego to Dallas via Ithaca

Example 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 won’t work with 5 servers because a

cycle in a RAG does not imply deadlock.

Example 2 (review question 2)

There are 3 processes

Process Run Time per thread #threads

P1 6 1

P2 3 2

P3 2 3

Example 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.

Example 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.

a) 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.

Example 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

Example 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. 

Solution

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.

Solution 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  

Solution contd…

MEnter:P(mutex);if(nw > 0 or ww > 0) { wm := wm+1; V(mutex); P(mwait); }else { nm :=nm+1; V(mutex); }

MLeave:P(mutex);nm := nm-1;if(nm = 0 and ww > 0) {

while(ww>0) { ww := ww-1; nw := nw+1; V(wwait); }}V(mutex);

Solution contd…

WEnter:P(mutex);if(nm > 0 or wm > 0) { ww := ww+1; V(mutex); P(wwait); }else { nw :=nw+1; V(mutex); }

WLeave:P(mutex);nw := nw-1;if(nw = 0 and wm > 0) {

while(wm>0) { wm := wm-1; nm := nm+1; V(mwait); }}V(mutex);

 

Question Time

Good Luck for the Prelim.