Presentation is loading. Please wait.

Presentation is loading. Please wait.

TANNENBAUM SECTION 2.3 INTERPROCESS COMMUNICATION2 OPERATING SYSTEMS.

Similar presentations


Presentation on theme: "TANNENBAUM SECTION 2.3 INTERPROCESS COMMUNICATION2 OPERATING SYSTEMS."— Presentation transcript:

1 TANNENBAUM SECTION 2.3 INTERPROCESS COMMUNICATION2 OPERATING SYSTEMS

2 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

3 THE PETERSON MODEL peterson() { int turn; int turn; int pr_0 = 0; int pr_0 = 0; int pr_1 = 0; int pr_1 = 0; parbegin parbegin { proc0(); proc0(); proc1(); proc1(); } parend parend}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(){ 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; }}

4 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

5 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

6 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

7 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 error Args: 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

8 SHMAT int* shmat(int shmid, char *shmaddr, int shmflag); Returns the starting address of the shared memory segment or -1 if error Arguments 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 segment address may have to be cast to an appropriate type.

9 SHMDT When a process if finished, shmdt detaches the shared memory segment int shmdt(int* address) Where address is the address returned by shmat Returns 0 or -1 indicating success or failure int value = smdt(address);

10 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);

11 PROBLEMS WITH ALL FAILED SOLUTIONS Context Switch between when a lock variable was tested and set results in: Mutual Exclusion Violation Deadlock

12 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 area 3.Sets the variable

13 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

14 TEST AND SET LOCK Test_Set() { int lock; unset_lock(); parbegin parbegin { proc0(); proc0(); proc1(); proc1(); } parend parend}proc0(){ enter_region(); enter_region(); crit_sect(); crit_sect(); leave_region(); leave_region();}proc1(){ enter_region() enter_region() crit_sect(); crit_sect(); leave_region() leave_region()} Trace conditions for solution to CS problem

15 TWO CORRECT SOLUTIONS TO CRITICAL SECTION PROBLEM Peterson: Software TSL: Hardware and software But 1.Both require busy-wait 2.Both are ad hoc in the sense that they are not part of programming language object


Download ppt "TANNENBAUM SECTION 2.3 INTERPROCESS COMMUNICATION2 OPERATING SYSTEMS."

Similar presentations


Ads by Google