20
1 Advanced Operating Systems - Fall 2009 Lecture 7 – February 2, 2009 Dan C. Marinescu Email: [email protected] Office: HEC 439 B. Office hours: M, Wd 3 – 4:30.

1 Advanced Operating Systems - Fall 2009 Lecture 7 February 2, 2009 Dan C. Marinescu Office: HEC 439 B. Office hours:

Embed Size (px)

DESCRIPTION

3 Dictionary Monitor: programming language construct for controlled access to critical sections. Monitor: programming language construct for controlled access to critical sections. Transaction (general): an agreement, communication, or movement carried out between separate entities involving the exchange of information, goods, services, or money. Transaction (general): an agreement, communication, or movement carried out between separate entities involving the exchange of information, goods, services, or money. Database transaction: a unit of work performed by a database management system in a coherent and reliable way, independent of other transactions. Atomic transaction: a group of database operations that either all occur, or none occurs. Atomicity prevents updates to the database occurring only partially. Storage: volatile, non-volatile, stable  Can the information be recovered in case of failure?

Citation preview

Page 1: 1 Advanced Operating Systems - Fall 2009 Lecture 7  February 2, 2009 Dan C. Marinescu   Office: HEC 439 B. Office hours:

1

Advanced Operating Systems - Fall 2009Lecture 7 – February 2, 2009

Dan C. MarinescuEmail: [email protected]: HEC 439 B. Office hours: M, Wd 3 – 4:30.

Page 2: 1 Advanced Operating Systems - Fall 2009 Lecture 7  February 2, 2009 Dan C. Marinescu   Office: HEC 439 B. Office hours:

2

Last, Current, Next Lecture

Last time: Process synchronization

Today Discussion of a problem regarding threads and I/O. More about condition variables and monitors Discussion of a problem about condition variables and monitors.

Next time: Atomic transactions

Page 3: 1 Advanced Operating Systems - Fall 2009 Lecture 7  February 2, 2009 Dan C. Marinescu   Office: HEC 439 B. Office hours:

3

Dictionary

MonitorMonitor: programming language construct for controlled access : programming language construct for controlled access to critical sections. to critical sections.

TransactionTransaction (general): an agreement, communication, or (general): an agreement, communication, or movement carried out between separate entities involving the movement carried out between separate entities involving the

exchange of information, goods, services, or moneyexchange of information, goods, services, or money.. Database transaction: a unit of work performed by a database

management system in a coherent and reliable way, independent of other transactions. 

Atomic transaction: a group of database operations that either all occur, or none occurs. Atomicity prevents updates to the database occurring only partially.

Storage: volatile, non-volatile, stable Can the information be recovered in case of failure?

Page 4: 1 Advanced Operating Systems - Fall 2009 Lecture 7  February 2, 2009 Dan C. Marinescu   Office: HEC 439 B. Office hours:

4

Dictionary (cont’d)

Condition variablesCondition variables: indicate an event and have no value. One : indicate an event and have no value. One cannot store a value into nor retrieve a value from a condition cannot store a value into nor retrieve a value from a condition variable. If a thread must wait for an event to occur, that thread variable. If a thread must wait for an event to occur, that thread waits on the corresponding condition variable. Two operations can waits on the corresponding condition variable. Two operations can be performed on a condition variable: Wait(v) and Signal(v).be performed on a condition variable: Wait(v) and Signal(v).

Memory-mapped fileMemory-mapped file: a segment of virtual memory which has been assigned a direct byte-for-byte correlation with some portion of a file or file-like resource. This resource could be a file physically present on-disk, or a device, shared memory object, or other resource that the OS can reference through a file descriptor.

Memory-mapped I/O uses the same address bus to address both memory and I/O devices, and the CPU instructions used to access the memory are also used for accessing devices. Regions of CPU's addressable space are reserved for I/O. 

Page 5: 1 Advanced Operating Systems - Fall 2009 Lecture 7  February 2, 2009 Dan C. Marinescu   Office: HEC 439 B. Office hours:

5

Dictionary (cont’d)

Condition variablesCondition variables: indicate an event and have no value. One : indicate an event and have no value. One cannot store a value into nor retrieve a value from a condition cannot store a value into nor retrieve a value from a condition variable. If a thread must wait for an event to occur, that thread variable. If a thread must wait for an event to occur, that thread waits on the corresponding condition variable. Two operations can waits on the corresponding condition variable. Two operations can be performed on a condition variable: Wait(v) and Signal(v).be performed on a condition variable: Wait(v) and Signal(v).

Memory-mapped fileMemory-mapped file: a segment of virtual memory which has been assigned a direct byte-for-byte correlation with some portion of a file or file-like resource. This resource could be a file physically present on-disk, or a device, shared memory object, or other resource that the OS can reference through a file descriptor.

Memory-mapped I/O uses the same address bus to address both memory and I/O devices, and the CPU instructions used to access the memory are also used for accessing devices. Regions of CPU's addressable space are reserved for I/O. 

Page 6: 1 Advanced Operating Systems - Fall 2009 Lecture 7  February 2, 2009 Dan C. Marinescu   Office: HEC 439 B. Office hours:

6

Problem

We wish to separate the logical from physical reading/writing to a file and implement a file server process which receives from a user process requests to

read to files and sends back the data write files and writes the data to the file

Two possible implementations: The server maintains a cache of recently used files in memory

reading and writing from/cache whenever possible. How should we use multithreading in this case:

(a) one thread per process or (b) multiple threads per process. The server maintains a large buffer space and allows a user

process to read and write asynchronously to file buffers.

Page 7: 1 Advanced Operating Systems - Fall 2009 Lecture 7  February 2, 2009 Dan C. Marinescu   Office: HEC 439 B. Office hours:

7

Design questions

Why separate logical from physical I/O operations? What do we gain and what is the price to pay? Can we use the concept for all types of I/O (e.g., disk and networking?)

What are the main features of each one of the two alternatives? Can a user process use multiple files? How many? Could it support multiple user processes? How many? What are the limitations of each approach.

Sketch the design for each of the two alternatives. Which one will be easier to implement and why? Which one is more effective at run time and why? Which one of the two options for the first alternative would you consider

and why? Are both options feasible? Compare these two alternative with a memory-mapped file. Discuss memory mapped I/O. Is it related to the methods above?

Page 8: 1 Advanced Operating Systems - Fall 2009 Lecture 7  February 2, 2009 Dan C. Marinescu   Office: HEC 439 B. Office hours:

8

Monitors

Programming languages constructs. The compiler handles calls to monitors differently than

other calls. Only one process may be active within the monitor at a

timemonitor monitor-name{

// shared variable declarationsprocedure P1 (…) { …. }

…procedure Pn (…) {……}

Initialization code ( ….) { … }…

}}

Page 9: 1 Advanced Operating Systems - Fall 2009 Lecture 7  February 2, 2009 Dan C. Marinescu   Office: HEC 439 B. Office hours:

9

Schematic view of a Monitor

Page 10: 1 Advanced Operating Systems - Fall 2009 Lecture 7  February 2, 2009 Dan C. Marinescu   Office: HEC 439 B. Office hours:

10

Blocking a process when it cannot proceed

Condition variables condition x, y;

Two operations on a condition variable: x.wait () – a process that invokes the operation is suspended. x.signal () – resumes one of processes (if any) that invoked

x.wait ()

Page 11: 1 Advanced Operating Systems - Fall 2009 Lecture 7  February 2, 2009 Dan C. Marinescu   Office: HEC 439 B. Office hours:

11

Monitor with Condition Variables

Page 12: 1 Advanced Operating Systems - Fall 2009 Lecture 7  February 2, 2009 Dan C. Marinescu   Office: HEC 439 B. Office hours:

12

A Producer-Consumer Monitor

monitor ProducerConsumer; condition full, empty; integer count; procedure enter; if count = N then wait(full); add_Item; count++; if count = 1 then signal(empty); end;

procedure remove; if count = 0 then wait(empty); remove_Item; count--; if count = N-1 then signal(full); end; count=0;end monitor;

Page 13: 1 Advanced Operating Systems - Fall 2009 Lecture 7  February 2, 2009 Dan C. Marinescu   Office: HEC 439 B. Office hours:

13

A Producer-Consumer Monitor (cont’d)

procedure producer; begin while true do begin produce_Item; ProducerConsumer_enter; end end

procedure producer; begin while true do begin ProducerConsumer_remove; consume_Item; end end

Page 14: 1 Advanced Operating Systems - Fall 2009 Lecture 7  February 2, 2009 Dan C. Marinescu   Office: HEC 439 B. Office hours:

14

Solution to Dining Philosophers

monitor DP {

enum { THINKING; HUNGRY, EATING) state [5] ;condition self [5];void pickup (int i) { state[i] = HUNGRY; test(i); if (state[i] != EATING) self [i].wait;}

void putdown (int i) { state[i] = THINKING;

// test left and right neighbors test((i + 4) modulo 5); test((i + 1) modulo 5);

}

Page 15: 1 Advanced Operating Systems - Fall 2009 Lecture 7  February 2, 2009 Dan C. Marinescu   Office: HEC 439 B. Office hours:

15

Dining Philosophers (cont’d)

void test (int i) { if ( (state[(i + 4) modulo 5] != EATING) && (state[i] == HUNGRY) && (state[(i + 1) modulo 5] != EATING) ) { state[i] = EATING ;

self[i].signal () ; } }

initialization_code() { for (int i = 0; i < 5; i++) state[i] = THINKING;}

}

Page 16: 1 Advanced Operating Systems - Fall 2009 Lecture 7  February 2, 2009 Dan C. Marinescu   Office: HEC 439 B. Office hours:

16

Dining Philosophers (cont’d)

Each philosopher I invokes the operations pickup() and putdown() in the following sequence:

dp.pickup (i)

EAT

dp.putdown (i)

Page 17: 1 Advanced Operating Systems - Fall 2009 Lecture 7  February 2, 2009 Dan C. Marinescu   Office: HEC 439 B. Office hours:

17

Monitor Implementation Using Semaphores

Variables semaphore mutex; // (initially = 1)semaphore next; // (initially = 0)int next-count = 0;

Each procedure F will be replaced bywait(mutex); …

body of F; …if (next-count > 0)

signal(next)else

signal(mutex); Mutual exclusion within a monitor is ensured.

Page 18: 1 Advanced Operating Systems - Fall 2009 Lecture 7  February 2, 2009 Dan C. Marinescu   Office: HEC 439 B. Office hours:

18

Monitor Implementation

For each condition variable x, we have:semaphore x-sem; // (initially = 0)int x-count = 0;

The operation x.wait can be implemented as:

x-count++;if (next-count > 0)

signal(next);

elsesignal(mutex);

wait(x-sem);

x-count--;

Page 19: 1 Advanced Operating Systems - Fall 2009 Lecture 7  February 2, 2009 Dan C. Marinescu   Office: HEC 439 B. Office hours:

19

Monitor Implementation

The operation x.signal can be implemented as:

if (x-count > 0) {next-count++;signal(x-sem);wait(next);next-count--;

}

Page 20: 1 Advanced Operating Systems - Fall 2009 Lecture 7  February 2, 2009 Dan C. Marinescu   Office: HEC 439 B. Office hours:

20

Problem for discussion

Monitors use condition variables and two operations: WAIT(v) and SIGNAL(v). Why not use a single primitive with a Boolean predicate p: WAITUNTIL(p). For example:

WAITUNTIL (v1 < 0 or v2 + v3 > n) How would you implement WAITUNTIL?