+ All Categories
Home > Documents > TANNENBAUM SECTION 2.3 INTERPROCESS COMMUNICATION2 OPERATING SYSTEMS.

TANNENBAUM SECTION 2.3 INTERPROCESS COMMUNICATION2 OPERATING SYSTEMS.

Date post: 03-Jan-2016
Category:
Upload: imogen-smith
View: 218 times
Download: 0 times
Share this document with a friend
15
TANNENBAUM SECTION 2.3 INTERPROCESS COMMUNICATION2 OPERATING SYSTEMS
Transcript
Page 1: TANNENBAUM SECTION 2.3 INTERPROCESS COMMUNICATION2 OPERATING SYSTEMS.

TANNENBAUMSECTION 2.3

INTERPROCESS COMMUNICATION2

OPERATING SYSTEMS

Page 2: TANNENBAUM SECTION 2.3 INTERPROCESS COMMUNICATION2 OPERATING SYSTEMS.

OVERALL PROBLEM

• How to solve the 2-process critical section problem in software• 1965 – Dekker solution. Very complex• 1981 – Peterson solution. Much simpler• Three variables• pr_0: set means that proc0 wants to enter cs• pr_1: set means that proc1 wants to enter cs• turn: indicates whose turn it is

Page 3: TANNENBAUM SECTION 2.3 INTERPROCESS COMMUNICATION2 OPERATING SYSTEMS.

THE PETERSON MODEL

peterson(){ int turn;int turn;

int pr_0 = 0;int pr_0 = 0;

int pr_1 = 0;int pr_1 = 0;

parbeginparbegin

{{

proc0();proc0();

proc1();proc1();

}}

parendparend

}}

proc0()proc0()

{{

pr_0 = 1;pr_0 = 1;

turn = 1;turn = 1;

while(pr_1 && turn);while(pr_1 && turn);

{{

crit_sect();crit_sect();

pr_0 = 0;pr_0 = 0;

}}

}}

proc1()proc1()

{{

pr_1 = 1;pr_1 = 1;

turn = 0;turn = 0;

while(pr_0 && !turn);while(pr_0 && !turn);

{{

crit_sect();crit_sect();

pr_1 = 0;pr_1 = 0;

}}

}}

Page 4: TANNENBAUM SECTION 2.3 INTERPROCESS COMMUNICATION2 OPERATING SYSTEMS.

CHECK FOR MUTUAL EXCLUSION

• Assume both processes are in CS• This implies• pr_1 == 1 and pr_0 == 1

• Since proc_0 is in CS• either pr_1 == 0 or turn == 0

• Since proc_1 is in CS• either pr_0 == 0 or turn = 1

• Since pr_1 and pr_0 are both 1• turn == 0 && turn == 1

• This is a contradiction, so the initial assumption must be false

Page 5: TANNENBAUM SECTION 2.3 INTERPROCESS COMMUNICATION2 OPERATING SYSTEMS.

CHECK FOR REMAINING THREE CONDITIONS

Speed•No assumptions were made•Authority• proc_0 is prevented from entering only if both pr_1 and

turn are set to 1.• but this happens only at the mutex gate

•Postponement• Assume that proc_0 is in a long non_crit_sect• proc_1 can enter cs because proc_0 has unset pr_0

Page 6: TANNENBAUM SECTION 2.3 INTERPROCESS COMMUNICATION2 OPERATING SYSTEMS.

FOUR IPC SYSTEM CALLS

• shmget: creates a new region of shared memory• shmat: logically attaches the newly created

shared memory to the virtual address space of a process• shmdt: detaches the shared memory region• shmctl: deletes the shared memory region• Programs using these, require:

#include <sys/ipc.h>#include <sys/shm.c>

Page 7: TANNENBAUM SECTION 2.3 INTERPROCESS COMMUNICATION2 OPERATING SYSTEMS.

SHMGET

int shmget(int key, int size, int shmflag)• Creates and opens a shared memory segment• Returns an identifier that is used to provide access to the shared

memory or -1 if there is an errorArgs:• key: 0 or identifier for an existing shared memory segment• size: size required in bytes• shmflag: specifies how the space is to be treated (use: 0777|

IPC_CREAT)

Putting it together:int shmid;shmid = shmget(0, 1, 0777|IPC_CREAT)

shmid, like a file descriptor, is available to the parent and all child processes

Page 8: TANNENBAUM SECTION 2.3 INTERPROCESS COMMUNICATION2 OPERATING SYSTEMS.

SHMAT

int* shmat(int shmid, char *shmaddr, int shmflag);Returns the starting address of the shared memory segment

or -1 if errorArguments• shmid: integer returned by shmget• shmaddr: if 0, unix selects the address• shmflag: 0 permits both read and write

int* address;address = shmat(shmid, 0, 0)address now holds the starting address of the shared

memory segmentaddress may have to be cast to an appropriate type.

Page 9: TANNENBAUM SECTION 2.3 INTERPROCESS COMMUNICATION2 OPERATING SYSTEMS.

SHMDT

• When a process if finished, shmdt detaches the shared memory segment

int shmdt(int* address)Where address is the address returned by shmatReturns 0 or -1 indicating success or failure

int value = smdt(address);

Page 10: TANNENBAUM SECTION 2.3 INTERPROCESS COMMUNICATION2 OPERATING SYSTEMS.

SHMCTL

Performs the control operation identified by the second argument. We’ll use it to return a shared memory segment to the operating system.

int shmctl(int shmid, int cmd, struct shmid_ds* buf)

Args:•shmid is shmid returned by shmget•cmd: IPC_RMID•buf: 0 or a data structure specified in man page. •When used with IPC_RMID returns 0 for success or -1 for failure

int value = shmctl(shmid, IPC_RMID, 0);

Page 11: TANNENBAUM SECTION 2.3 INTERPROCESS COMMUNICATION2 OPERATING SYSTEMS.

PROBLEMS WITH ALL FAILED SOLUTIONS

• Context Switch between when a lock variable was tested and set results in:• Mutual Exclusion Violation• Deadlock

Page 12: TANNENBAUM SECTION 2.3 INTERPROCESS COMMUNICATION2 OPERATING SYSTEMS.

WITH A LITTLE HELP FROM HARDWARE

• Create a single, uninterruptible hardware instruction that

1.Reads a variable (1 or 0)2.Stores the value in a save area3.Sets the variable

Page 13: TANNENBAUM SECTION 2.3 INTERPROCESS COMMUNICATION2 OPERATING SYSTEMS.

TSL

unset_lock(){ lock = 0;}

enter_region: TSL register, lock //copy lock to register and set lock to 1 cmp register, #0 //is lock 0? JNE enter_region //jump to label if not equal (i.e., loop) RET //return to caller

leave_region: MOVE LOCK, #0 //store 0 in lock RET //return to caller

Page 14: TANNENBAUM SECTION 2.3 INTERPROCESS COMMUNICATION2 OPERATING SYSTEMS.

TEST AND SET LOCK

Test_Set(){ int lock; unset_lock();

parbeginparbegin

{{

proc0();proc0();

proc1();proc1();

}}

parendparend

}}

proc0()proc0()

{{

enter_region();enter_region();

crit_sect();crit_sect();

leave_region();leave_region();

}}

proc1()proc1()

{{

enter_region()enter_region()

crit_sect();crit_sect();

leave_region()leave_region()

}}

Trace conditions for solution to CS problem

Page 15: TANNENBAUM SECTION 2.3 INTERPROCESS COMMUNICATION2 OPERATING SYSTEMS.

TWO CORRECT SOLUTIONS TO CRITICAL SECTION PROBLEM

• Peterson: Software• TSL: Hardware and software• But1.Both require busy-wait2.Both are ad hoc in the sense that they are not

part of programming language object


Recommended