32
COS 318 - Operating System Assignment 4 (Precept 2) Inter-Process Communication and Process management Fall 2004

COS 318 - Operating System Assignment 4 (Precept 2) Inter-Process Communication and Process management Fall 2004

Embed Size (px)

Citation preview

Page 1: COS 318 - Operating System Assignment 4 (Precept 2) Inter-Process Communication and Process management Fall 2004

COS 318 - Operating System

Assignment 4 (Precept 2)

Inter-Process Communication and Process management

Fall 2004

Page 2: COS 318 - Operating System Assignment 4 (Precept 2) Inter-Process Communication and Process management Fall 2004

Mailboxes clarification

Empty Unopened Mailbox

Page 3: COS 318 - Operating System Assignment 4 (Precept 2) Inter-Process Communication and Process management Fall 2004

W1 opens for writing

W1

Page 4: COS 318 - Operating System Assignment 4 (Precept 2) Inter-Process Communication and Process management Fall 2004

W1 writes message M1

W1

M1

Page 5: COS 318 - Operating System Assignment 4 (Precept 2) Inter-Process Communication and Process management Fall 2004

W2 opens mailbox

W1 W2

M1

Page 6: COS 318 - Operating System Assignment 4 (Precept 2) Inter-Process Communication and Process management Fall 2004

W1 closes mailbox

W2

M1

Page 7: COS 318 - Operating System Assignment 4 (Precept 2) Inter-Process Communication and Process management Fall 2004

R1 opens mailbox

W2 R1

M1

Page 8: COS 318 - Operating System Assignment 4 (Precept 2) Inter-Process Communication and Process management Fall 2004

R1 will find message M1

W2 R1

M1

Page 9: COS 318 - Operating System Assignment 4 (Precept 2) Inter-Process Communication and Process management Fall 2004

Same outcome for R2 instead of W2

R1

M1

R2

Page 10: COS 318 - Operating System Assignment 4 (Precept 2) Inter-Process Communication and Process management Fall 2004

W1 opens for writing

W1

Page 11: COS 318 - Operating System Assignment 4 (Precept 2) Inter-Process Communication and Process management Fall 2004

W1 writes message M1

W1

M1

Page 12: COS 318 - Operating System Assignment 4 (Precept 2) Inter-Process Communication and Process management Fall 2004

W1 closes mailbox

M1

Page 13: COS 318 - Operating System Assignment 4 (Precept 2) Inter-Process Communication and Process management Fall 2004

W2 opens mailbox

W2

M1

Page 14: COS 318 - Operating System Assignment 4 (Precept 2) Inter-Process Communication and Process management Fall 2004

R1 opens mailbox

W2 R1

M1

Page 15: COS 318 - Operating System Assignment 4 (Precept 2) Inter-Process Communication and Process management Fall 2004

R1 will not find M1

W2 R1

M1

Page 16: COS 318 - Operating System Assignment 4 (Precept 2) Inter-Process Communication and Process management Fall 2004

W2 writes M2

W2

M2

R1

Page 17: COS 318 - Operating System Assignment 4 (Precept 2) Inter-Process Communication and Process management Fall 2004

R1 will receive M2

W2 R1

M2

Page 18: COS 318 - Operating System Assignment 4 (Precept 2) Inter-Process Communication and Process management Fall 2004

Mailbox state is reset sometime after the last close and before the first re-open

Page 19: COS 318 - Operating System Assignment 4 (Precept 2) Inter-Process Communication and Process management Fall 2004

Atomic operationsOperations on shared state need to be

atomicAtomicity can be guranteed by the

hardware or by softwareHardware – a single instruction is always

atomic

Software – locks, monitors, semaphores...

Software atomicity always derived from hardware support.

Page 20: COS 318 - Operating System Assignment 4 (Precept 2) Inter-Process Communication and Process management Fall 2004

Is foo() atomic ?

int foo(void) {return global_shared++;

}

int foo(void) { return ++global_shared;}

void foo(void) {global_shared++;

}

Page 21: COS 318 - Operating System Assignment 4 (Precept 2) Inter-Process Communication and Process management Fall 2004

Is foo() atomic ?

int foo(void) {return global_shared++;

}

int foo(void) { return ++global_shared;}

void foo(void) {global_shared++;

}

No.

No.

Depends.

Page 22: COS 318 - Operating System Assignment 4 (Precept 2) Inter-Process Communication and Process management Fall 2004

Mailbox calls – atomic or not ?

Function: Void mbox_init(void);

Atomic or not: No.

Why: Does modify global shared state. But is guranteed to be called in isolation.

Page 23: COS 318 - Operating System Assignment 4 (Precept 2) Inter-Process Communication and Process management Fall 2004

Mailbox calls – atomic or not ?

Function: int mbox_open(int key);

Atomic or not: Depends upon implementation.

Why: If shared state is modified, yes. Otherwise no.

Same for mbox_close();

Page 24: COS 318 - Operating System Assignment 4 (Precept 2) Inter-Process Communication and Process management Fall 2004

Mailbox calls – atomic or not ?

Function: int mbox_stat(int q, int *count, int *space);

Doesnt modify any shared state. Does it need to be atomic ?

Answer: Yes.

Why: For integrity of the data read. The relation between count and space should be maintained.

Page 25: COS 318 - Operating System Assignment 4 (Precept 2) Inter-Process Communication and Process management Fall 2004

Mailbox calls – atomic or not ?

Function: int mbox_send(int q, msg_t *m);

Answer: Obviously Yes. Modifies count, head and tail.

Question: Is that all or is more protection needed ?

Answer: We need more. For integrity of data write. Message body should be written in continous chunk and not get interleaved.

Message aaa & bbb should get written as aaabbb or bbbaaa and not abbaba

Same for mbox_recv();

Page 26: COS 318 - Operating System Assignment 4 (Precept 2) Inter-Process Communication and Process management Fall 2004

Why interrupts shouldnt block

Interrupts can happen anytime and in context of any process/thread.

It runs in the context of the interrupted process and not its own.

Can deadlock if blocking for event that only interrupted process can enable.

interrupt_handler() { … wait for event foobar ….}

Page 27: COS 318 - Operating System Assignment 4 (Precept 2) Inter-Process Communication and Process management Fall 2004

Simplest deadlock

foobar is acquire_lock(l);

foo() { acquire_lock(l); ….. // deadlock on interrupt region release_lock(l);}

Page 28: COS 318 - Operating System Assignment 4 (Precept 2) Inter-Process Communication and Process management Fall 2004

Little more subtle

foobar is busy_wait(kerry == president);

foo() { acquire_lock(l); ….. // deadlock on interrupt region release_lock(l);}

bar() { acquire_lock(l); …. // tamper with ohio results kerry = president; release_lock(l);}

Page 29: COS 318 - Operating System Assignment 4 (Precept 2) Inter-Process Communication and Process management Fall 2004

Keyboard interrupts

putchar(); takes no special precaution

mbox_send(); can block if buffer is full.

mbox_send(); can block waiting for lock being held by getchar() which got interrupted.

Page 30: COS 318 - Operating System Assignment 4 (Precept 2) Inter-Process Communication and Process management Fall 2004

Keyboard interrupts

putchar(); tries locking

putchar() needs some kind of locking between mbox_stat() and mbox_send().

Still doesn’t fix the problem with interrupting getchar();

Locking alone isn’t the answer.

Page 31: COS 318 - Operating System Assignment 4 (Precept 2) Inter-Process Communication and Process management Fall 2004

Extra credit – KillYou also need to take care of doing

closing any mailbox opened by the process

Page 32: COS 318 - Operating System Assignment 4 (Precept 2) Inter-Process Communication and Process management Fall 2004

Implementation notesDont assume anything about behavior of

other modulesFor eg, Mailboxes shouldn't assume

anything about scheduler or number of processes or threads or their access pattern or behavior of block, unblock.

Keep use of MAX_xxx limits to a minimum.