Homework 6 Sarah Diesburg Operating Systems CS 3430.

12
Homework 6 Sarah Diesburg Operating Systems CS 3430

Transcript of Homework 6 Sarah Diesburg Operating Systems CS 3430.

Page 1: Homework 6 Sarah Diesburg Operating Systems CS 3430.

Homework 6

Sarah Diesburg

Operating Systems

CS 3430

Page 2: Homework 6 Sarah Diesburg Operating Systems CS 3430.

Helicopter Problem

A helicopter ride has five seats, and it always carries a full load.

Use lock(s) and condition variable(s) to write a procedure PersonArrives(), which is called whenever a person (thread) arrives.

Once the load is full, one person (thread) should call UpAndAway(), and five threads should return from PersonArrives(). There should be no undue waiting: the helicopter

should depart as soon as it has a full load.

Page 3: Homework 6 Sarah Diesburg Operating Systems CS 3430.

Helicopter Problem

int queue = 0;condition okToGo = NULL;Lock lock = FREE;

PersonArrives() {lock.Acquire();++queue;if (queue < 4) {

okToGo.wait(&lock);} else {

GoGoGo(); queue = 0;

okToGo.Signal(&lock);okToGo.Signal(&lock);okToGo.Signal(&lock);

}lock.Release();

}

Page 4: Homework 6 Sarah Diesburg Operating Systems CS 3430.

Helicopter Problem

int queue = 0;condition okToGo = NULL;Lock lock = FREE;

PersonArrives() {lock.Acquire();++queue;if (queue < 5) {

okToGo.wait(&lock);} else {

UpAndAway();= 0; okToGo.Signal(&lock);okToGo.Signal(&lock);okToGo.Signal(&lock);okToGo.Signal(&lock);

}lock.Release();

}

Page 5: Homework 6 Sarah Diesburg Operating Systems CS 3430.

Helicopter Problem

int queue = 0;condition okToGo = NULL;Lock lock = FREE;

PersonArrives() {lock.Acquire();++queue;if (queue < 5) {

okToGo.wait(&lock);} else {

UpAndAway(); okToGo.Signal(&lock);

okToGo.Signal(&lock);okToGo.Signal(&lock);okToGo.Signal(&lock);queue = 0;

}lock.Release();

}

Page 6: Homework 6 Sarah Diesburg Operating Systems CS 3430.

Proving Correctness

Mutual exclusion All shared variables are locked

e.g., queue Signal() and Wait() are invoked between

lock.Acquire() and lock.Release()

Page 7: Homework 6 Sarah Diesburg Operating Systems CS 3430.

Proving Correctness

Liveness Maximum of 4 threads in okToGo.wait() Every 5th thread wakes up exactly four threads

already waiting in okToGo.wait() Additional threads wait at lock.Acquire() As long as we have 5n threads, every thread will

make progress

Page 8: Homework 6 Sarah Diesburg Operating Systems CS 3430.

Proving Correctness

Fairness As long as we have FIFO queues at

lock.Acquire() The queuing policy at okToGo.wait() does not

matter, since all four waiting threads will be awakened

As long as we have 5n threads Every thread will get its chance

Page 9: Homework 6 Sarah Diesburg Operating Systems CS 3430.

Common Pitfalls

int queue = 0;condition okToGo = NULL;Lock lock = FREE;

PersonArrives() {lock.Acquire();++queue;if (queue < 5) {

okToGo.wait(&lock);} else {

UpAndAway(); okToGo.Signal(&lock);

okToGo.Signal(&lock);okToGo.Signal(&lock); okToGo.Signal(&lock);queue = 0;lock.Release();

}}

Wait() requires lock on returnNo lock.Release()

Page 10: Homework 6 Sarah Diesburg Operating Systems CS 3430.

Common Pitfalls

int queue = 0;condition okToGo = NULL;Lock lock = FREE;

PersonArrives() {lock.Acquire();++queue;lock.Release();if (queue < 5) {

lock.Acquire();okToGo.wait(&lock);lock.Release();

} else { lock.Acquire();UpAndAway();

okToGo.Signal(&lock);okToGo.Signal(&lock);okToGo.Signal(&lock);okToGo.Signal(&lock); queue = 0; lock.Release();

}}

Unprotected states

Multiple threads may wait here

Page 11: Homework 6 Sarah Diesburg Operating Systems CS 3430.

Kernel Modules

A kernel module is driver code that is dynamically loaded into the driver when it is needed Also can be unloaded

The kernel is an executable file Can contain built-in driver code, but makes the

executable bigger

Page 12: Homework 6 Sarah Diesburg Operating Systems CS 3430.

Module Commands

insmod – insert a module into the running kernel

rmmod – remove a module from the running kernel

modprobe – inserts a module, along with all other modules it depends on Only using insmod for our project