COP 4600 Operating Systems Spring 2011 Dan C
- Slides: 25
COP 4600 Operating Systems Spring 2011 Dan C. Marinescu Office: HEC 304 Office hours: Tu-Th 5: 00 -6: 00 PM
Lecture 17 – Thursday, March 24, 2011 n Last time: n n Today: n n ¨ Threads Thread state Processor switching - YIELD system call Processor switching Communication with a bounded buffer Next time n n n Communication with a bounded buffer Semaphores Deadlocks Lecture 17 2
YIELD n System call executed by the kernel at the request of an application ¨ n YIELD invokes the ENTER_PROCESSOR_LAYER procedure ¨ ¨ ¨ ¨ n allows an active thread A to voluntarily release control of the processor. locks the thread table and unlock it when it finishes it work changes the state of thread A from RUNNING to RUNNABLE invokes the SCHEDULER searches the thread table to find another tread B in RUNNABLE state the state of thread B is changed from RUNNABLE to RUNNING the registers of the processor are loaded with the ones saved on the stack for thread B becomes active Why is it necessary to lock the thread table? We may have multiple cores/processors so another thread my be active. ¨ An interrupt may occur ¨ n n The pseudo code assumes that we have a fixed number of threads, 7. The flow of control YIELD ENTER_PROCESSOR_LAYER SCHEDULER EXIT_PROCESSOR_LAYER YIELD Lecture 17 3
Lecture 17 4
Dynamic thread creation and termination n n Until now we assumed a fixed number, 7 threads; the thread table was of fixed size. We have to support two other system calls: EXIT_THREAD Allow a tread to self-destroy and clean-up ¨ DESTRY_THREAD Allow a thread to terminate another thread of the same application ¨ Lecture 17 5
Important facts to remember n n n Each thread has a unique Thread. Id Threads save their state on the stack. The stack pointer of a thread is stored in the thread table. To activate a thread the registers of the processor are loaded with information from the thread state. What 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 available ¨ the processor_thread runs in the thread layer ¨ the SCHEDULER runs in the processor layer ¨ n We have a processor thread for each processor/core. Lecture 17 6
System start-up procedure 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 17 7
Switching threads with dynamic thread creation n Switching from one user thread to another requires two steps Switch from the thread releasing the processor to the processor thread ¨ Switch from the processor thread to the new thread which is going to have the control of the processor ¨ The last step requires the SCHEDULER to circle through the thread_table until a thready to run is found ¨ n n The boundary between user layer threads and processor layer thread is crossed twice Example: switch from thread 0 to thread 6 using YIELD ¨ ENTER_PROCESSOR_LAYER ¨ EXIT_PROCESSOR_LAYER ¨ Lecture 17 8
Dynamic thread creation/destruction n As before, the control flow is not obvious as some of the procedures reload the stack pointer (SP) When a procedure reloads the stack pointer then the place where it transfers control when it executes a return is the procedure whose SP was saved on the stack and was reloaded before the execution of the return. ENTER_PROCESSOR_LAYER Changes the state of the thread calling YIELD from RUNNING to RUNNABLE ¨ Save the state of the procedure calling it , YIELD, on the stack ¨ Loads the processors registers with the state of the processor thread, thus starting the SCHEDULER ¨ n EXIT_PROCESSOR_LAYER ¨ Saves the state of processor thread into the corresponding PROCESSOR_TABLE and loads the state of the thread selected by the SCHEDULER to run (in our example of thread 6) in the processor’s registers ¨ Loads the SP with the values saved by the ENTER_PROCESSOR_LAYER Lecture 17 9
Lecture 17 10
Lecture 17 11
Lecture 17 12
Lecture 17 13
Thread coordination with bounded buffers n n Bounded buffer the virtualization of a communication channel Thread coordination Locks for serialization ¨ Bounded buffers for communication ¨ n n n Producer thread writes data into the buffer Consumer thread data from the buffer Basic assumptions: We have only two threads ¨ Threads proceed concurrently at independent speeds/rates ¨ Bounded buffer – only N buffer cells ¨ Messages are of fixed size and occupy only one buffer cell. ¨ n Spin lock a thread keeps checking a control variable/semaphore “until the light turns green. ” ¨ feasible only when the threads run on a different processors (how could otherwise give a chance to other threads? ) ¨ Lecture 17 14
Lecture 17 15
Implicit assumptions for the correctness of the implementation 1. 2. 3. 4. 5. 6. One sending and one receiving thread. Only one thread updates each shared variable. Sender and receiver threads run on different processors to allow spin locks in and out are implemented as integers large enough so that they do not overflow (e. g. , 64 bit integers) The shared memory used for the buffer provides read/write coherence The memory provides before-or-after atomicity for the shared variables in and out The result of executing a statement becomes visible to all threads in program order. No compiler optimization supported Lecture 17 16
In practice…. . n Threads run concurrently Race conditions may occur data in the buffer may be overwritten a lock for the bounded buffer the producer acquires the lock before writing the consumer acquires the lock before reading Lecture 17 17
Lecture 17 18
We have to avoid deadlocks n n If a producer thread cannot write because the buffer is full it has to release the lock to allow the consumer thread to acquire the lock to read, otherwise we have a deadlock. If a consumer thread cannot read because there is no new item in the buffer it has to release the lock to allow the consumer thread to acquire the lock to write, otherwise we have a deadlock. Lecture 17 19
Lecture 17 20
In practice… n We have to ensure atomicity of some operations, e. g. , updating the pointers Lecture 17 21
One more pitfall of the previous implementation of bounded buffer n If in and out are long integers (64 or 128 bit) then a load requires two registers, e. , g, R 1 and R 2. int “ 0000 FFFF” L R 1, int /* R 1 0000 L R 2, int+1 /* R 2 FFFF n Race conditions could affect a load or a store of the long integer. Lecture 17 22
Lecture 17 23
In practice threads may run on the same system…. n n We cannot use spinlocks for a thread to wait until an event occurs. That’s why we have spent time on YIELD… Lecture 17 24
Lecture 17 25
- Cop 4600
- Cop 4600
- Cop 4600 ucf
- Good cop bad cop interrogation
- Cop 1 cop 2
- Ecisd spring break 2011
- 4600 million years ago
- Ul 4600
- 4600/365
- 4600 miles
- Four seasons korean movie
- Spring summer autumn
- Spring information systems
- Operating systems examples
- Evolution of operating systems
- Components of an operating system
- Components of operating system
- Wsn operating systems
- Operating system 3 easy pieces
- Operating systems lab
- Introduction to operating systems
- Tanenbaum modern operating systems
- Main components of file management
- Design issues of distributed operating system
- Early operating systems
- Real-time operating systems