Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. POSIX semaphores allow processes and threads to synchronize their
  2. actions.
  3.  
  4. A semaphore is an integer whose value is never allowed to fall below
  5. zero. Two operations can be performed on semaphores: increment the
  6. semaphore value by one (sem_post(3)); and decrement the semaphore
  7. value by one (sem_wait(3)). If the value of a semaphore is currently
  8. zero, then a sem_wait(3) operation will block until the value becomes
  9. greater than zero.
  10.  
  11. POSIX semaphores come in two forms: named semaphores and unnamed
  12. semaphores.
  13.  
  14. Named semaphores
  15. A named semaphore is identified by a name of the form
  16. /somename; that is, a null-terminated string of up to
  17. NAME_MAX-4 (i.e., 251) characters consisting of an initial
  18. slash, followed by one or more characters, none of which are
  19. slashes. Two processes can operate on the same named
  20. semaphore by passing the same name to sem_open(3).
  21.  
  22. The sem_open(3) function creates a new named semaphore or
  23. opens an existing named semaphore. After the semaphore has
  24. been opened, it can be operated on using sem_post(3) and
  25. sem_wait(3). When a process has finished using the semaphore,
  26. it can use sem_close(3) to close the semaphore. When all
  27. processes have finished using the semaphore, it can be removed
  28. from the system using sem_unlink(3).
  29.  
  30. Unnamed semaphores (memory-based semaphores)
  31. An unnamed semaphore does not have a name. Instead the
  32. semaphore is placed in a region of memory that is shared
  33. between multiple threads (a thread-shared semaphore) or
  34. processes (a process-shared semaphore). A thread-shared
  35. semaphore is placed in an area of memory shared between the
  36. threads of a process, for example, a global variable. A
  37. process-shared semaphore must be placed in a shared memory
  38. region (e.g., a System V shared memory segment created using
  39. shmget(2), or a POSIX shared memory object built created using
  40. shm_open(3)).
  41.  
  42. Before being used, an unnamed semaphore must be initialized
  43. using sem_init(3). It can then be operated on using
  44. sem_post(3) and sem_wait(3). When the semaphore is no longer
  45. required, and before the memory in which it is located is
  46. deallocated, the semaphore should be destroyed using
  47. sem_destroy(3).
  48.  
  49. The remainder of this section describes some specific details of the
  50. Linux implementation of POSIX semaphores.
  51.  
  52. Prior to kernel 2.6, Linux supported only unnamed, thread-shared
  53. semaphores. On a system with Linux 2.6 and a glibc that provides the
  54. NPTL threading implementation, a complete implementation of POSIX
  55. semaphores is provided.
  56.  
  57. POSIX named semaphores have kernel persistence: if not removed by
  58. sem_unlink(3), a semaphore will exist until the system is shut down.
  59.  
  60. Programs using the POSIX semaphores API must be compiled with cc
  61. -pthread to link against the real-time library, librt.
  62.  
  63. Accessing named semaphores via the filesystem
  64. On Linux, named semaphores are created in a virtual filesystem,
  65. normally mounted under /dev/shm, with names of the form sem.somename.
  66. (This is the reason that semaphore names are limited to NAME_MAX-4
  67. rather than NAME_MAX characters.)
  68.  
  69. Since Linux 2.6.19, ACLs can be placed on files under this directory,
  70. to control object permissions on a per-user and per-group basis.
  71.  
  72. An example of the use of various POSIX semaphore functions is shown
  73. in sem_wait(3).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement