Upload
alvis
View
22
Download
0
Tags:
Embed Size (px)
DESCRIPTION
COP 4600 Operating Systems Spring 2011. Dan C. Marinescu Office: HEC 304 Office hours: Tu-Th 5:00-6:00 PM. Last time: Virtualization for the three abstractions Threads Virtual Memory Bounded buffer The kernel of an operating syste Today: Threads State Processor switching - PowerPoint PPT Presentation
Citation preview
COP 4600 Operating Systems Spring 2011
Dan C. Marinescu
Office: HEC 304
Office hours: Tu-Th 5:00-6:00 PM
Last time: Virtualization for the three abstractions
Threads Virtual Memory Bounded buffer
The kernel of an operating syste Today:
Threads State
Processor switching Semaphores Deadlocks
Next time Communication with a bounded buffer
Lecture 16 – Tuesday, March 22, 2011
Lecture 16 2
Thread and VM management – virtual computer
The kernel supports thread and virtual memory management Thread management:
Creation and destruction of threads Allocation of the processor to a ready to run thread Handling of interrupts Scheduling – deciding which one of the ready to run threads should be allocated
the processor
Virtual memory management maps virtual address space of a thread to physical memory.
Each module runs in own address space; if one module runs multiple threads all share one address space.
Thread + virtual memory virtual computer for each module.
Lecture 16 3
Threads and the Thread Manager
Thread virtual processor - multiplexes a physical processor a module in execution; a module may have several threads. sequence of operations:
Load the module’s text Create a thread and lunch the execution of the module in that thread.
Scheduler system component which chooses the thread to run next
Thread manager implements the thread abstraction. Interrupts processed by the interrupt handler which interacts with
the thread manager Exception interrupts caused by the running thread and processed by
exception handlers Interrupt handlers run in the context of the OS while exception
handlers run in the context of interrupted thread.
Lecture 16 4
The state of a thread; kernel versus application threads
Thread state: Thread Id unique identifier of a thread Program Counter (PC) -the reference to the next computational step Stack Pointer (SP) PMAR – Page Table Memory Address Register Other registers
Threads User level threads/application threads – threads running on behalf of users Kernel level threads – threads running on behalf of the kernel
Scheduler thread – the thread running the scheduler Processor level thread – the thread running when there is no thread ready to run
Lecture 16 5
Lecture 16 6
Virtual versus real; the state of a processor
Virtual objects need a physical support. In addition to threads we should be concerned with the processor or
cores running a thread. A system may have multiple processors and each processor may
have multiple cores The state of the processor or core:
Processor Id/Core Id unique identifier of a processor /core Program Counter (PC) -the reference to the next computational step Stack Pointer (SP) PMAR – Page Table Memory Address Register Other registers
Lecture 16 7
Thread Layer
Processor Layer
Thread 1 Thread 2 Thread 7
Processor A Processor B
ID
SP
PC
PMAR
ID
SP
PC
PMAR
ID
SP
PC
PMAR
ID
SP
PC
PMAR
ID
SP
PC
PMAR
8Lecture 16
Processor and thread tables – control structures that must be locked for serialization
Lecture 16 9
Primitives for processor virtualization
Lecture 16 10
Memory CREATE/DELETE_ADDRESS SPACEALLOCATE/FREE_BLOCKMAP/UNMAPUNMAP
Interpreter ALLOCATE_THREAD DESTROY_THREADEXIT_THREAD YIELDAWAIT ADVANCETICKETACQUIRE RELEASE
Communication channel
ALLOCATE/DEALLOCATE_BOUNDED_BUFFERSEND/RECEIVE
Thread states and state transitions
Lecture 16 11
The state of a thread and its associated virtual address space
Lecture 16 12
Switching the processor from one thread to another
Thread 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 thread Schedule another thread Start running the new thread – dispatch the processor to the new thread
YIELD 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++, Java allows several threads running on the same processor to wait for a lock. It replaces the
busy wait we have used before.
Lecture 16 13
Implementation of YIELD
Apply the principle of least astonishment; deal first with a simpler case: A fixed number of threads (7). All threads run on the same core and in the same address space. When
switching states we do not need to update the PMAR (Page Memory address Register)
When we enter the processor layer change the state of the current thread from RUNNING to RUNNABLE Save the stack pointer in the thread table Invoke the scheduler
The SCHEDULER Searches the thread table for a thread in RUNNABLE state and changes its state to
RUNNING Update the processor table to mark this thread as running on that processor EXITS the Processor layer
Lecture 16 14
Lecture 16 15
shared structure processor_table(7) integer thread_idshared structure thread_table(7) integer topstack integer stateshared lock instance thread_table_lock
procedure GET_THREAD_ID() return processor_table(CPUID).thread_id
procedure YIELD() ACQUIRE (thread_table_lock) ENTER_PROCESSOR_LAYER(GET_THREAD_ID()) RELEASE(thread_table_lock)return
procedure ENTER_PROCESSOR_LAYER(this_thread) thread_table(this_thread).state ß RUNNABLE thread_table(this_thread).topstackß SP SCHEDULER()return
procedure SCHEDULER() jß _GET_THREAD_ID() do jß j+1 (mod 7)
while thread_table(j).state¬= RUNNABLE thread_table(j).state ß RUNNING processor_table(CPUID).thread_idß j EXIT_PROCESSOR_LAYER(j) return
procedure EXIT_PROCESSOR_LAYER(new) SP,-- thread_table(new).topstack return
16Lecture 16