Download presentation
Published byBrittany Eugenia Shaw Modified over 9 years ago
1
Not Another Completely Heuristic Operating System
N.A.C.H.O.S. Not Another Completely Heuristic Operating System
2
First Assignment Build (implement) a thread system:
Locks and condition variables Producer/consumer communication Computer age laundromat problem Alarm clock Bridge problem Dzungle problem Elevators in Evans Hall
3
Synchronization - Semaphores
Atomic P(), V() operations Each semaphore object has a queue If the semaphore is not available, process goes to Sleep() Decrements the value of the semaphore and executes critical section Before proccess exits its critical section invoke V() Process waiting in semaphore queue is then removed from list Then scheduled to start exec placing in the ReadyList by Scheduler The process is appended to the list for later work (ReadyToRun in scheduler.cc)
4
Synchronization - Monitors
By turning all the critical regions into monitor procedures, no two process will ever execute their critical regions at the same time!
5
Synchronization - Locks
Implementing locks must consider: Atomicity for Acquire(), Release() Provison of some wait mechanism if a process trying to acquire lock cannot do so (like a semaphore mechanism) Making sure only acquire lock can release lock (isHeldByCurrentThread())
6
Condition Variables The first thing is to determine what actually goes in the waitQueue of the condition variable. Then translate the operations that Wait and Signal perform into the correct interface provided by nachos.
7
Condition Variables Constraints
Wait(), Signal(), and Broadcast() must be atomic operations Signal() is a nop if the waitQueue is empty Use INTs instead of semaphores for implementation Wait() reacquires lock before it returns Wait() releases lock before putting thread to sleep
8
Wait() Algorithm Turn INTs off Append current thread to waitQueue
Release lock Sleep current thread Reacquire lock Set INTs back to what it was before
9
Signal() Algorithm Turn off INTs
If waitQueue is not empty, put one on the readyList Set INTs back to what it was before
10
Synchronization Deductions
Context switch is faster between threads (not process) User-level switching between thread are faster (then kernel switch), because user-level thread are independed of the operating systém Process executed by fork gets an identical copy of the address space of its parent Threads dont share their stack and PC with other threads Thread can create any child threads and can read or write other thread stack Space must be allocated for all threads
11
Synchronization Deductions
Disabling interupts is not a perfect solution for avoiding race conditions Process can be switched out if it is executing in its critical section Process waiting for semaphore, after signalled, don’t gets the CPU immediately If semaphore Queue is LIFO, can be happen indefinite blocking Test & Set synchronization primitives is a HW solution to the synchronization problem
12
Alarm Clock Constraints:
The problem is to modify the existing code so that a new function Alarm::GotoSleepFor(int x) can be called on a running thread, and it will place the current thread to sleep (removing from the ready queue) for x ticks. Only one thread will call GotoSleepFor at a time (many threads can be waiting, but GotoSleepFor can only be called on the current thread). Multiple threads can be "waiting" at the same time.
13
Alarm Clock Constraints:
Multiple threads can be woken during a WakeUp. This happens because we have no controll over when the callback occurrs, and several threads could be ready to resume their normal, day-to-day thread lives. We can not call or schedule interrupts. We need to rely on those that occurr from other events (we need to make sure the timer is active whenever we have threads waiting)
14
Alarm Clock Algorithm:
My solution is in by placing the current thread (when GotoSleepFor is called) onto a special "waiting list" which is maintained by the alarm class, and then put to sleep until it is awakened by the WakeUp function after X ticks have occurred. Whenever a wakeup occurrs, alarm will loop through its list of "waiting threads" and awake those that have spent their time. A threads time is stored in a variable atimer in the thread class, which specifies the tick on which the thread can be woken. If the waiting list is not empty, the timer will not be disabled.
15
Alarm Clock Deductions:
Nachos code frequently enable/disable interupts for the purposes of mutual exclusion by calling Setlevel() Whenever MIPS simulator executes one instruction, clock advance one tick Whenever the ready list is empty, the clock advances many ticks are needed to fast forward the current time to that of the next scheduled event Whenever the clock advance, the event queue is examined and any pending interupts are serviced by invoking the interupts service routine. All interupt service routines runs with interupts dissabled and the service routine not re-enable them
16
Producer - Consumer We must implement producer consumer com- munication through a bounded buffer using locks and condition variables. The producer places characters from the string ''Hello world'' into the buffer one character at a time. In the other hand, the consumer pulls characters out of the buffer one at a time and prints them to the screen. We must do our work with a multicharacter buffer and with multiple producers and consumers.
17
Producer - Consumer Constraints:
When the buffer is full, producer must wait When the buffer is empty consumer must also wait We must provide multi-character buffer with multiple producers and consumers
18
Bridge Problem In this assignment we must to synchronize traffic over a narrow lightduty bridge on a public highway. Traffic may only cross the bridge in one direction at a time, and if there are ever more than 3 vehicles on the bridge at one time, it will collapse under their weight. In this system, each car is represented by one thread. Direction is represented either 0 and 1.
19
Bridge Problem Constraints:
We must write the procedures for arrive, cross and exit the bridge using locks and condition variables. ArriveBridge must not return until it safe for the car to cross the bridge in the given direction (it must guarantee that there will be no headon collisions or bridge collapses). ExitBridge is called to indicate that the caller has finished crossing the bridge. ExitBridge should take steps to let additional cars cross the bridge When a car arrives on the bridge from direction 0, another car from opposite direction can not travel
20
Local Laundromat Each customer, which puts coins into slots at one of two stations and types in the number of washing machines he will need. The stations are connected to a central computer that automatically assigns available machines and outputs tokens that identify the machines to be used. The customer puts laundry into the machines and inserts each token into the machine indicated on the token. When a machine finishes its cycle, it informs the computer that it is available again.
21
Local Laundromat Constraints:
If two people make requests at the two stations at the same time, they will occasionally be assigned the same machine. The same washing machine can be assigned to two different customers. We must modify the code to eliminate the problem Solve the synchronization problem using locks and condition variables instead of semaphores Customer must wait if we are little of machine we need If some machines are freed we can finish waiting customer/s
22
Missionary - Cannibal A river crossing is shared by both cannibals and missionaries. A boat is used to cross the river, but it only seats three people. In order to guarantee the safety of the missionaries, you cannot put one missionary and two cannibals in the same boat (because the cannibals would gang up and eat the missionary), but all other combinations are legal. We must provide code for MissionaryArrives and CannibalArrives, called by a missionary or cannibal when it arrives at the river bank. The procedures arrange the arriving missionaries and cannibals into safe boatloads.
23
Missionary - Cannibal Constraints:
A boat must always carry a full load We cannot put one missionary and two cannibals in the same boat Cannibal must wait if on the boat is already one cannibal Missionary must wait if on the boat are two cannibals
24
Elevators In this assignment we should provide controller for the elevator, using semaphores or condition variables. In addition to the elevator manager, you need to implement the routines called by the arriving student/faculty: This should wake up the elevator, tell it which floor the person is on, and wait until the elevator arrives before telling it which floor to go to. The elevator is amazingly fast, but it is not instantaneous - it takes only 100 ticks to go from one floor to the next. For simplicity, you can assume there's only one elevator, and that it holds an arbitrary number of people.
25
Elevators Constraints: We must use semaphores or condition variables
Each elevator is represented by a thread Each student or faculty member is represented also by a thread Elevator takes 100 ticks to go from one floor to the next We must know where is the elevator, and whether are doors open
26
Schedule
27
Schedule
28
Schedule
29
„I hear and I forget, I see and I remember, I do and I understand.“
Chinese Proverb „I hear and I forget, I see and I remember, I do and I understand.“
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.