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

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

Embed Size (px)

DESCRIPTION

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

Citation preview

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

Operating SystemsOperating SystemsCOMP 4850/CISG 5550COMP 4850/CISG 5550Interprocess Communication, Part Interprocess Communication, Part

IIII

Dr. James MoneyDr. James Money

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

SemaphoresSemaphores• In 1965, Dijkstra introduced the idea In 1965, Dijkstra introduced the idea

of a semaphore for the producer-of a semaphore for the producer-consumer problem.consumer problem.

• A A semaphoresemaphore is an integer variable to is an integer variable to count the number of wakeups.count the number of wakeups.

• The semaphore is 0 if there are no The semaphore is 0 if there are no wakeups saved, or some positive wakeups saved, or some positive value is there are some waiting.value is there are some waiting.

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

SemaphoresSemaphores• There are two operations on the There are two operations on the

semaphore:semaphore:– upup– downdown

• These generalize sleep and wakeupThese generalize sleep and wakeup

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

Semaphore - downSemaphore - down• The The downdown function does the function does the

following:following:– If the value of the semaphore is greater If the value of the semaphore is greater

than 0, it decrements the valuethan 0, it decrements the value– If the value is 0, the process it put to If the value is 0, the process it put to

sleep without completing downsleep without completing down– This is done as an atomic operationThis is done as an atomic operation

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

Semaphore – upSemaphore – up• The The upup function does the following: function does the following:

– Increments the value of the semaphoreIncrements the value of the semaphore– If one of the processes running down are If one of the processes running down are

sleeping, it is chosen by the system to sleeping, it is chosen by the system to complete it’s down operation. In this complete it’s down operation. In this case, the semaphore remains 0.case, the semaphore remains 0.

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

Solving P-C Problem with Solving P-C Problem with SemaphoresSemaphores• We can solve the producer-consumer We can solve the producer-consumer

problem with semaphoresproblem with semaphores• We need three semaphores:We need three semaphores:

– fullfull – counting the number of slots full – counting the number of slots full– emptyempty – counting the slots that are empty – counting the slots that are empty– mutexmutex – to ensure the producer and – to ensure the producer and

consumer do not access the buffer at the consumer do not access the buffer at the same timesame time

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

P-C Problem with P-C Problem with SemaphoresSemaphores• Initially:Initially:

– full = 0full = 0– empty=Nempty=N– mutex – 1mutex – 1

• Semaphores that are set to 1, and Semaphores that are set to 1, and used by 2+ processes to ensure only used by 2+ processes to ensure only one of them enter their critical region one of them enter their critical region at a time are called at a time are called binary binary semaphoressemaphores..

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

P-C Problem with P-C Problem with SemaphoresSemaphores

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

MutexesMutexes• When the counting ability of a When the counting ability of a

semaphore is not needed, we use a semaphore is not needed, we use a binary version called a mutex.binary version called a mutex.

• A A mutexmutex is a variable that can be in is a variable that can be in one of two states:one of two states:– lockedlocked– unlockedunlocked

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

MutexesMutexes• There are two functions:There are two functions:

– mutex_lockmutex_lock– mutex_unlockmutex_unlock

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

mutex_lockmutex_lock• If the mutex is unlocked, the mutex is If the mutex is unlocked, the mutex is

locked and the program can enter its locked and the program can enter its critical regionscritical regions

• If the mutex is locked, the calling thread is If the mutex is locked, the calling thread is blocked until mutex_unlock has been blocked until mutex_unlock has been called by another threadcalled by another thread

• If multiple threads are blocked If multiple threads are blocked mutex_lock, then one of them is chosen to mutex_lock, then one of them is chosen to be given the lock.be given the lock.

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

mutex_unlockmutex_unlock• mutex_unlock setp the mutex to 0, mutex_unlock setp the mutex to 0,

and unblocks a process if neededand unblocks a process if needed

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

MutexesMutexesMutexes are simple enough to implement in user space:

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

Message PassingMessage Passing• When multiple machines are involved, When multiple machines are involved,

none of the prior solutions work.none of the prior solutions work.• This new IPC method is called This new IPC method is called

message passingmessage passing..• There are two functions:There are two functions:

– send(destination, &message);send(destination, &message);– receive(source,&message);receive(source,&message);

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

Message PassingMessage Passing• Design Issues:Design Issues:

– Messages can be lost – there must be Messages can be lost – there must be some acknowledgement of the received some acknowledgement of the received messagemessage

– What if a message is received twice? What if a message is received twice? Use sequence numbers or message tagsUse sequence numbers or message tags

– How to name processes and ensure How to name processes and ensure authentication?authentication?

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

P-C Problem with Message P-C Problem with Message PassingPassing

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

BarriersBarriers• Barriers are used when one process Barriers are used when one process

may not proceed until another may not proceed until another process reaches a certain pointprocess reaches a certain point

• When a process reaches a barrier When a process reaches a barrier call, it blocks until all the processes call, it blocks until all the processes call barriercall barrier

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

BarriersBarriers