No races from interrupt handlers or involuntary

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: el”, we can temporarily disable interrupts. no races from interrupt handlers or involuntary context switches issues? The CriticalThe Critical-Section Problem n processes or threads all competing to use some shared data Each process/thread has a code segment, called critical section, in which which the shared data is accessed. Problem – ensure that when one process/thread is executing in its critical section, no other process/thread is allowed to execute in its its critical section. Structure of process/thread entry section critical section exit section reminder section Critical Critical Sections in the Color Stack InitColorStack() { push(blue); push(purple); } PushColor() { if (s[top] == purple) { ASSERT(s[top-1] == blue); push(blue); } else { ASSERT(s[top] == blue); ASSERT(s[top-1] == purple); push(purple); } } Mutual Mutual Exclusion Race conditions can be avoiding by ensuring mutual exclusion in critical sections. • Critical sections are code sequences that contribute to races. Every race (possible incorrect interleaving) involves two or more threads executing related critical sections concurrently. • To avoid races, we must serialize related critical sections. Never allow more than one thread in a critical section at a time. Code is executed ATOMICALLY. Mutual Mutual Exclusion Two types of solutions to prevent entry to CS, and they are: busy waiting while (not_safe_to_enter_cs) ; blocking if (not_safe_to_enter_cs) We’ll look at blocking first sleep; Semaphore Semaphore Synchronization tool that does not require busy waiting. C++ like interface shown here class Semaphore { Semaphore (char *name, int initialValue); // set initial value (char name int // void P(); void V(); // these are the only operations on a semaphore (down) // they are both ~ atomic and indivisible (up) private: int value; // semaphore value, always >= 0 List *queue; *queue; // // threads waiting in P() for the value to be > 0 }; Semaphore Semaphore (cont’d) void Semaphore::P() { // disable interrupts while (value == 0) { queue->Append((void *)currentThread); *) // go to sleep – ints get re-enabled when new thread picked currentThread->Sleep(); // returns – ints disabled // } value value--; // re-enable interrupts } Semaphore Semaphore (cont’d) void Semaphore::V() { Thread *thread; *thread // disable interrupts thread = (Thread *)queue->Remove(); (Thread )queue if (thread != NULL) value++; // re-enable interrupts } Semaphore Semaphore (cont’d) void Semaphore::P() { // disable interrupts while (value == 0) { } value--; // // re-enable interrupts } Examples Examples Counting semaphore example: suppose there are N free resources semaphore *S; Thread *t[MaxT]; void fn (int arg) { S = new semaphore (“my_S”, N); …...
View Full Document

This document was uploaded on 04/02/2014.

Ask a homework question - tutors are online