1 / 17

Semaphore Interface

Semaphore Interface. S : Integer value Down(S): If (S == 0) block else if (S > 0) S = S – 1 Up(S): If any blocked processes, Release one of them else S = S + 1. Atomic actions Down might block Up never blocks.

karl
Download Presentation

Semaphore Interface

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Semaphore Interface • S: Integer value • Down(S):If (S == 0) blockelse if (S > 0) S = S – 1 • Up(S):If any blocked processes, Release one of themelse S = S + 1 • Atomic actions • Down might block • Up never blocks Cpr E 308

  2. Down(S): if (S == 0) then suspend process and add to Q; else S = S-1 Up(S):if (Q non-empty) dequeue a process and make it runnable; else S = S+1 Semaphore Implementation Data Structures:Integer SQueue of processes, Q Cpr E 308

  3. Down(S): lock (mutex) if (S == 0) then suspend process and add to Q; unlock(mutex); switch to another process else S = S-1;unlock(mutex); Semaphore Implementation Data Structures:Integer SQueue of processes, Q Up(S): • lock (mutex) • if (Q non-empty) then • dequeue a process and make it runnable • unlock(mutex); else S = S+1;unlock(mutex); Cpr E 308

  4. How is mutex implemented in this case? • Using a semaphore? No • Peterson’s algorithm? Ok • Test-and-set-lock? Better • In the kernel: disable interrupts Best Cpr E 308

  5. Uni vs Multi processors • Disabling interrupts does not work on multiprocessors. Why? • Test and set - best bet Cpr E 308

  6. Semaphore Example:Implementing wait() system call • Parent does a wait() call on child • wait till child finishes before exiting • What if parent executed wait() after child exited? Cpr E 308

  7. Parent and Child 0 Cpr E 308

  8. Child exits late 1 0 Busy waiting expensive, go to sleep Cpr E 308

  9. Parent Sleeps 1 0 wakeup Cpr E 308

  10. Child exits early 1 Cpr E 308

  11. Simultaneous 1 0 wakeup Cpr E 308

  12. Solution: Semaphore • Semaphore zombie: initialize to 0 • Parent:down(zombie) inside wait() • Child:up(zombie) upon exiting Cpr E 308

  13. POSIX mutexes • int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *mutexattr); • int pthread_mutex_lock(pthread_mutex_t *mutex)); • int pthread_mutex_trylock(pthread_mutex_t *mutex); • int pthread_mutex_unlock(pthread_mutex_t *mutex); • int pthread_mutex_destroy(pthread_mutex_t *mutex); • “man pthread_mutex_init” on your Linux machine Cpr E 308

  14. POSIX Semaphores • int sem_init(sem_t *sem, int pshared, unsigned int value); • int sem_wait(sem_t * sem); • int sem_trywait(sem_t * sem); • int sem_post(sem_t * sem); • int sem_getvalue(sem_t * sem, int * sval); • int sem_destroy(sem_t * sem); Cpr E 308

  15. Message Passing • No shared variables • send (destination, message) • receive (destination, message)blocks till a message arrives Cpr E 308

  16. Issues • Across different machines, message passing is the real thing • On the same machine (shared physical memory), performance? • Marshaling data into messages • Provide reliable transmission across unreliable links? • Event-driven mode of programming Cpr E 308

  17. Producer-Consumer using Message Passing Cpr E 308

More Related