CSE 451 Section #3
Questions from Lecture Kinds of threads When threads are used How threads are implemented Scheduling
Homework Mean – 22.8/25 Style –Always explain your answers. Just an answer with not explanation is only worth 50% –Try to be concise. A short correct answer is better than a long correct answer –Please staple – if we lose a page because its not stapled, that is your problem.
Homework Confusions 1.4 – security issues with time sharing 1.x – is efficiency important 2.1 – prefetching 2.3 – traps vs interrupts 2.8 – what operations must be restricted? 3.7 – what is a system call? 3.10 – what is the benefit of a VM?
Project Implement: –Condition Variables –Sending / receiving messages –Thread::Join() –Priority scheduling –Whale Mating
What we give you Threads with Fork() Semaphores Sample code for a list with synchronized access –In synchlist.cc –N.b.: locks not implemented yet
What you need to implement In Synch.cc: –Condition class How: –Look at implementation of semaphores –Implement using a similar style –Use semaphores as part of implementation
Example: p() // disable interrupts IntStatus oldLevel = interrupt->SetLevel(IntOff); // semaphore not available while (value == 0) { // so go to sleep queue->Append((void *)currentThread); currentThread->Sleep(); } // semaphore available, consume its value value--; // re-enable interrupts (void) interrupt->SetLevel(oldLevel);
Condition Variables Three operations –Wait –Signal –Broadcast
Condition Variable Usage Define the condition you are synchronizing Hold the lock –Test the condition –If false, release lock and wait for signal –When signaled, grab lock and repeat –If true, execute code & release lock
Signaling a Condition Variable Signal – wake up one waiter Broadcast – wake up all waiters –Useful if they have different conditions –Useful for reader/writer locks
Sample Code Void Consume(LIST_TYPE List) { LIST_ELEM Next; List.mutex.lock() While (List.head == NULL) do Cond_wait(ListNonEmpty, List.mutex); Next = List.Head; List.Head = List.Head.Next; List.mutex.unlock(); }
Sample Code Void Produce(LIST_TYPE List, LIST_ELEM Next) { List.mutex.lock(); Next.next = List.Head; List.head = Next; Cond_Signal(ListNonEmpty, List.Mutex) List.mutex.unlock(); }
How Condition Variables Work Wait: add to waiting list, waiting on variable Signal: remove first element of waiting list, move to ready list, waiting on Lock. Broadcast: remove one element of waiting list, move to ready list. Make all other elements wait on the Lock instead of the variable.
Sending / Receiving Messages Mailboxes – can hold at most one message Reader blocks until there is a message in it Sender blocks until there are no messages in it If there are multiple senders / readers, they queue Treat as bounded buffer with bound = 1
Mail Boxes What is needed to implement this? –A queue? –A condition variable? –A mutex?
Thread Join Allows a thread to wait for another one to terminate Does not return the return value from forked thread Need to modify ThreadFinish to signal any waiters on Join() May be multiple callers to Join
Priority Scheduling Modify ready list to handle priority TimerInterruptHandler calls Interrupt::YieldOnReturn –Calls CurrentThread->Yield Enable timer interrupts with “nachos –rs #” –To make non random, modify system.cc to pass “false” to “new timer(…)”
How are Threads Used Two common paradigms: –Thread pools –Worker Threads
Thread Pools Used in server applications: –Web servers –File servers –RPC servers Have a list of threads –On a new request, hand to a thread and signal it to start –If no threads ready, either queue request or create a new thread –Often have high/low thresholds for # of threads
Worker Threads Used to partition work into multiple parallel pieces Example: web page –Pull basic data –Pull personalization data –Pull advertising –All 3 threads execute simultaneously
Worker Threads (2) Fork multiple threads, one for each part Signal when all complete: –Use a condition variable, basic thread checks all three when done –Use a sempaphore – how? –Use a mutex Have a counter…
Other kinds of synchronization Hardware based: –Atomic increment Good for reference counts lock xadd [ecx],eax –Atomic exchange Good for list manipulation mov eax, [ecx] ; get operand value Ixchg: cmpxchg [ecx], edx ; compare and swap jnz Ixchg ; if nz, exchange failed