Using Shared Memory
Once the shared memory is attached, your process can use it like any other region of memory. However, since multiple processes can see this same region of memory, care must be exercised when changing its content.
In the globvar utility, one semaphore is used as the locking semaphore. Whenever the shared memory is searched or modified, the globvar utility waits on the semaphore first (recall that it was initialized to the value of 1). This ensures that no more than one process at a time will be working with the shared memory. When the task has been completed, the semaphore is notified to release the lock.
It should be noted that some values were accessed in the shared memory without the locking semaphore. Examine lines 15–19 ...
Get Advanced UNIX Programming now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.