Presentation is loading. Please wait.

Presentation is loading. Please wait.

Operating Systems COMP 4850/CISG 5550 Interprocess Communication, Part II Dr. James Money.

Similar presentations


Presentation on theme: "Operating Systems COMP 4850/CISG 5550 Interprocess Communication, Part II Dr. James Money."— Presentation transcript:

1 Operating Systems COMP 4850/CISG 5550 Interprocess Communication, Part II Dr. James Money

2 Semaphores In 1965, Dijkstra introduced the idea of a semaphore for the producer-consumer problem. In 1965, Dijkstra introduced the idea of a semaphore for the producer-consumer problem. A semaphore is an integer variable to count the number of wakeups. A semaphore is an integer variable to count the number of wakeups. The semaphore is 0 if there are no wakeups saved, or some positive value is there are some waiting. The semaphore is 0 if there are no wakeups saved, or some positive value is there are some waiting.

3 Semaphores There are two operations on the semaphore: There are two operations on the semaphore: –up –down These generalize sleep and wakeup These generalize sleep and wakeup

4 Semaphore - down The down function does the following: The down function does the following: –If the value of the semaphore is greater than 0, it decrements the value –If the value is 0, the process it put to sleep without completing down –This is done as an atomic operation

5 Semaphore – up The up function does the following: The up function does the following: –Increments the value of the semaphore –If one of the processes running down are sleeping, it is chosen by the system to complete it’s down operation. In this case, the semaphore remains 0.

6 Solving P-C Problem with Semaphores We can solve the producer-consumer problem with semaphores We can solve the producer-consumer problem with semaphores We need three semaphores: We need three semaphores: –full – counting the number of slots full –empty – counting the slots that are empty –mutex – to ensure the producer and consumer do not access the buffer at the same time

7 P-C Problem with Semaphores Initially: Initially: –full = 0 –empty=N –mutex – 1 Semaphores that are set to 1, and used by 2+ processes to ensure only one of them enter their critical region at a time are called binary semaphores. Semaphores that are set to 1, and used by 2+ processes to ensure only one of them enter their critical region at a time are called binary semaphores.

8 P-C Problem with Semaphores

9 Mutexes When the counting ability of a semaphore is not needed, we use a binary version called a mutex. When the counting ability of a semaphore is not needed, we use a binary version called a mutex. A mutex is a variable that can be in one of two states: A mutex is a variable that can be in one of two states: –locked –unlocked

10 Mutexes There are two functions: There are two functions: –mutex_lock –mutex_unlock

11 mutex_lock If the mutex is unlocked, the mutex is locked and the program can enter its critical regions If the mutex is unlocked, the mutex is locked and the program can enter its critical regions If the mutex is locked, the calling thread is blocked until mutex_unlock has been called by another thread If the mutex is locked, the calling thread is blocked until mutex_unlock has been called by another thread If multiple threads are blocked mutex_lock, then one of them is chosen to be given the lock. If multiple threads are blocked mutex_lock, then one of them is chosen to be given the lock.

12 mutex_unlock mutex_unlock setp the mutex to 0, and unblocks a process if needed mutex_unlock setp the mutex to 0, and unblocks a process if needed

13 Mutexes Mutexes are simple enough to implement in user space:

14 Message Passing When multiple machines are involved, none of the prior solutions work. When multiple machines are involved, none of the prior solutions work. This new IPC method is called message passing. This new IPC method is called message passing. There are two functions: There are two functions: –send(destination, &message); –receive(source,&message);

15 Message Passing Design Issues: Design Issues: –Messages can be lost – there must be some acknowledgement of the received message –What if a message is received twice? Use sequence numbers or message tags –How to name processes and ensure authentication?

16 P-C Problem with Message Passing

17 Barriers Barriers are used when one process may not proceed until another process reaches a certain point Barriers are used when one process may not proceed until another process reaches a certain point When a process reaches a barrier call, it blocks until all the processes call barrier When a process reaches a barrier call, it blocks until all the processes call barrier

18 Barriers


Download ppt "Operating Systems COMP 4850/CISG 5550 Interprocess Communication, Part II Dr. James Money."

Similar presentations


Ads by Google