Therefore unix systems provide reentrant versions of

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: s (not discussed here), then pshared is nonzero. We use Posix semaphores only in the context of concurrent threads, so pshared is 0 in all of our examples. Programs perform È and Î operations on semaphore sem by calling the sem wait and sem post functions, respectively. #include <semaphore.h> int sem wait(sem t *sem); int sem post(sem t *sem); returns: 0 if OK, -1 on error Figure 11.17 shows a version of the threaded counter program from Figure 11.8, called goodcnt.c, that uses semaphore operations to properly synchronize access to the shared counter variable. The code follows directly from Figure 11.16, so there are just a few aspects of it to point out: ¯ ¯ First, in a convention dating back to Dijkstra’s original semaphore paper, a binary semaphore used for safe sharing is often called a mutex because it provides each thread with mutually exclusive access to the shared data. We have followed this convention in our code. Second, the Sem init, P, and V functions are Unix-style error-handling wrappers for the sem init, sem wait, and sem pos...
View Full Document

Ask a homework question - tutors are online