COT 4600 Operating Systems Fall 2009Lecture 19Virtualization of threadsBasic primitives for processor virtualizationSlide 5The state of a thread and its associated virtual address spaceProcessor sharingThread states and state transitionsSwitching the processor from one thread to anotherSlide 10Slide 11Implementation of YIELDSlide 13More about thread creation and terminationSwitching threads with dynamic thread creationSlide 16Slide 17Slide 18COT 4600 Operating Systems Fall 2009Dan C. MarinescuOffice: HEC 439 BOffice hours: Tu-Th 3:00-4:00 PM22222Lecture 19Last time: Enforcing Modularity in MemoryToday:Sharing a processor among multiple threadsImplementation of the YIELDCreating and terminating threadsPreemptive schedulingNext Time:Thread primitives for sequence coordinationVirtualization of threadsImplemented by the operating system for the three abstractions:1. Threads a thread is a virtual processor; a module in execution1. Multiplexes a physical processor2. The state of a thread: (1) the reference to the next computational step (the Pc register) + (2) the environment (registers, stack, heap, current objects).3. Sequence of operations:1. Load the module’s text2. Create a thread and lunch the execution of the module in that thread.4. A module may have several threads.5. The thread manager implements the thread abstraction.1. Interrupts processed by the interrupt handler which interacts with the thread manager2. Exception interrupts caused by the running thread and processed by exception handlers3. Interrupt handlers run in the context of the OS while exception handlers run in the context of interrupted thread.Basic primitives for processor virtualizationMemoryCREATE/DELETE_ADDRESS SPACEALLOCATE/FREE_BLOCKMAP/UNMAPUNMAPInterpreterALLOCATE_THREAD DESTROY_THREADEXIT_THREAD YIELDAWAIT ADVANCETICKETACQUIRE RELEASECommunication channelALLOCATE/DEALLOCATE_BOUNDED_BUFFERSEND/RECEIVEThe state of a thread and its associated virtual address spaceLecture 18 6Processor sharingPossible because threads spend a significant percentage of their lifetime waiting for external events.Called:Time-sharingProcessor multiplexingMultiprogrammingMultitaskingThe kernel must support a number of functions:Creation and destruction of threadsAllocation of the processor to a ready to run threadHandling of interruptsScheduling – deciding which one of the ready to run threads should be allocated the processor Lecture 19 7Thread states and state transitionsLecture 19 8Switching the processor from one thread to anotherThread creation: thread_id ALLOCATE_THREAD(starting_address_of_procedure, address_space_id);YIELD function implemented by the kernel to allow a thread to wait for an event.Save the state of the current threadSchedule another threadStart running the new thread – dispatch the processor to the new threadYIELD cannot be implemented in a high level language, must be implemented in the machine language.can be called from the environment of the thread, e.g., C, C++, Javaallows several threads running on the same processor to wait for a lock. It replaces the busy wait we have used before. Lecture 19 9Lecture 6 10Implementation of YIELDLecture 19 12More about thread creation and terminationWhat if want to create/terminate threads dynamically we have to:Allow a tread to self-destroy and clean-up -> EXIT_THREADAllow a thread to terminate another thread of the same application DESTRY_THREADWhat if no thread is able to run create a dummy thread for each processor called a processor_thread which is scheduled to run when no other thread is availablethe processor_thread runs in the thread layer the SCHEDULER runs in the processor layerThe procedure followed when a kernel starts----------------------------------------------------------------------- Procedure RUN_PROCESSORS() for each processor do allocate stack and setup processor thread /*allocation of the stack done at processor layer shutdown FALSE SCHEDULER() deallocate processor_thread stack /*deallocation of the stack done at processor layer halt processor Lecture 19 14Switching threads with dynamic thread creationSwitching from one user thread to another requires two stepsSwitch from the thread releasing the processor to the processor threadSwitch from the processor thread to the new thread which is going to have the control of the processorThe last step requires the SCHEDULER to circle through the thread_table until a thread ready to run is foundThe boundary between user layer threads and processor layer thread is crossed twiceExample: switch from thread 1 to thread 6 using YIELDENTER_PROCESSOR_LAYEREXIT_PROCESSOR_LAYERLecture 6 15Lecture 19 16Lecture 19 17Lecture 19
View Full Document