os_cp:shmem_semaphores
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| os_cp:shmem_semaphores [2024/06/05 20:21] – [What is a semaphore] jkonczak | os_cp:shmem_semaphores [2026/06/11 09:02] (current) – [POSIX semaphore API] jkonczak | ||
|---|---|---|---|
| Line 23: | Line 23: | ||
| be associated with the address range. | be associated with the address range. | ||
| \\ | \\ | ||
| - | This is done using the '' | + | This is done using the '' |
| < | < | ||
| function with '' | function with '' | ||
| Line 36: | Line 36: | ||
| processes, and is done by adding the '' | processes, and is done by adding the '' | ||
| file descriptor of '' | file descriptor of '' | ||
| - | \\ | ||
| - | < | ||
| - | '' | ||
| - | UNIX-like system supports it. | ||
| - | </ | ||
| When the file descriptor passed to '' | When the file descriptor passed to '' | ||
| Line 46: | Line 41: | ||
| < | < | ||
| Writing the changes back to the file must be done manually by calling the | Writing the changes back to the file must be done manually by calling the | ||
| - | '' | + | '' |
| function. | function. | ||
| The file descriptor passed to '' | The file descriptor passed to '' | ||
| - | Such descriptors are returned by the '' | + | Such descriptors are returned by the '' |
| function, which has identical arguments as the '' | function, which has identical arguments as the '' | ||
| returned descriptor refers to a region of main memory associated with a given | returned descriptor refers to a region of main memory associated with a given | ||
| Line 61: | Line 56: | ||
| \\ | \\ | ||
| To ensure that the file is large enough, one can use the following functions: | To ensure that the file is large enough, one can use the following functions: | ||
| - | * '' | + | * '' |
| - | * '' | + | * '' |
| To clean up the memory mapping, one has to use the | To clean up the memory mapping, one has to use the | ||
| - | '' | + | '' |
| function. (Provided one wishes to flush data to a backing ordinary file, one | function. (Provided one wishes to flush data to a backing ordinary file, one | ||
| must call '' | must call '' | ||
| Line 232: | Line 227: | ||
| POSIX standardizes two implementations of semaphores: | POSIX standardizes two implementations of semaphores: | ||
| - | [[https:// | + | [[https:// |
| - | and < | + | and < |
| While the standard %%C++%% libraries include [[https:// | While the standard %%C++%% libraries include [[https:// | ||
| there are no plans to include them in the standard C library (unlike other | there are no plans to include them in the standard C library (unlike other | ||
| Line 249: | Line 244: | ||
| There are two vital differences between mutexes and semaphores: | There are two vital differences between mutexes and semaphores: | ||
| + | < | ||
| - A mutex locked by some thread //belongs// to it and only the thread is allowed to unlock it.\\ A semaphore can be decremented/ | - A mutex locked by some thread //belongs// to it and only the thread is allowed to unlock it.\\ A semaphore can be decremented/ | ||
| - A mutex can be configured recursive, and then it can be locked multiple times by the same thread and will be unlocked only after corresponding number of unlocks. | - A mutex can be configured recursive, and then it can be locked multiple times by the same thread and will be unlocked only after corresponding number of unlocks. | ||
| Line 254: | Line 250: | ||
| There are two ways of creating a POSIX semaphore: | There are two ways of creating a POSIX semaphore: | ||
| - | * __Named semaphore: | + | * __Named semaphore: |
| - | * __Unnamed semaphore: | + | * __Unnamed semaphore: |
| To decrement a semaphore (possibly waiting thereby), one shall call: \\ | To decrement a semaphore (possibly waiting thereby), one shall call: \\ | ||
| < | < | ||
| - | <a href=" | + | <a href=" |
| '' | '' | ||
| < | < | ||
| Line 267: | Line 263: | ||
| To increment a semaphore, one shall call: \\ | To increment a semaphore, one shall call: \\ | ||
| < | < | ||
| - | <a href=" | + | <a href=" |
| '' | '' | ||
| < | < | ||
| Line 274: | Line 270: | ||
| logging and debugging purposes) using:\\ | logging and debugging purposes) using:\\ | ||
| < | < | ||
| - | <a href=" | + | <a href=" |
| '' | '' | ||
| < | < | ||
| Line 304: | Line 300: | ||
| #include < | #include < | ||
| #include < | #include < | ||
| + | #include < | ||
| #define MAX_ELEMENTS_PLUS_ONE 4 | #define MAX_ELEMENTS_PLUS_ONE 4 | ||
| Line 335: | Line 332: | ||
| int main() { | int main() { | ||
| - | struct ring_buffer buffer; | + | struct ring_buffer |
| - | initBuffer(&buffer); | + | initBuffer(buffer); |
| printf(" | printf(" | ||
| " | " | ||
| Line 352: | Line 349: | ||
| switch (cmd[0]) { | switch (cmd[0]) { | ||
| case ' | case ' | ||
| - | put(&buffer, &item); | + | put(buffer, &item); |
| break; | break; | ||
| case ' | case ' | ||
| - | get(&buffer, &item); | + | get(buffer, &item); |
| printf(" | printf(" | ||
| break; | break; | ||
os_cp/shmem_semaphores.1717618881.txt.gz · Last modified: (external edit)