n.a.c.h.o.s

29
N.A.C.H.O.S. Not Another Completely Heuristic Operating System

Upload: nora

Post on 12-Jan-2016

25 views

Category:

Documents


0 download

DESCRIPTION

N.A.C.H.O.S. Not Another Completely Heuristic Operating System. 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. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: N.A.C.H.O.S

N.A.C.H.O.S.

Not Another Completely Heuristic Operating System

Page 2: N.A.C.H.O.S

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

Page 3: N.A.C.H.O.S

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)

Page 4: N.A.C.H.O.S

Synchronization - Monitors

• By turning all the critical regions into monitor procedures, no two process will ever execute their critical regions at the same time!

Page 5: N.A.C.H.O.S

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())

Page 6: N.A.C.H.O.S

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.

Page 7: N.A.C.H.O.S

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

Page 8: N.A.C.H.O.S

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

Page 9: N.A.C.H.O.S

Signal() Algorithm

• Turn off INTs

• If waitQueue is not empty, put one on the readyList

• Set INTs back to what it was before

Page 10: N.A.C.H.O.S

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

Page 11: N.A.C.H.O.S

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

Page 12: N.A.C.H.O.S

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.

Page 13: N.A.C.H.O.S

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)

Page 14: N.A.C.H.O.S

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.

Page 15: N.A.C.H.O.S

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

Page 16: N.A.C.H.O.S

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 multi character buffer and with multiple producers and consumers.

Page 17: N.A.C.H.O.S

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

Page 18: N.A.C.H.O.S

Bridge Problem

In this assignment we must to synchronize traffic over a narrow light duty 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.

Page 19: N.A.C.H.O.S

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 head on collisions or bridge collapses). Exit Bridge 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

Page 20: N.A.C.H.O.S

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.

Page 21: N.A.C.H.O.S

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

Page 22: N.A.C.H.O.S

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.

Page 23: N.A.C.H.O.S

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

Page 24: N.A.C.H.O.S

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.

Page 25: N.A.C.H.O.S

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

Page 26: N.A.C.H.O.S

Schedule

0

5

10

15

20

25

30

35

40

Locks Condition Alarm Buffer

NávrhChybyKód

Page 27: N.A.C.H.O.S

Schedule

0

5

10

15

20

25

30

35

40

Laundromat Bridge Dzungle Elevator

NávrhChybyKód

Page 28: N.A.C.H.O.S

Schedule

0

5

10

15

20

25

30

35

40

Study Design Errors Implement.

Page 29: N.A.C.H.O.S

Chinese Proverb

„I hear and I forget, I see and I remember, I do and I understand.“