Several IPC mechanisms are available for POSIX systems, including shared memory and message passing. Here, we explore the POSIX API for shared memory.
POSIX shared memory is organized using memory-mapped files, which associate the region of shared memory with a file. A process must first create a shared-memory object using the shm_open() system call, as follows:
shm_fd = shm_open(name, O_CREAT | O_RDWR, 0666); Parameters: name: The first parameter specifies the name of the shared-memory object. Processes that wish to access this shared memory must refer to the object by this name. O_CREAT | O_RDWR : The subsequent parameters specify that the shared-memory object is to be created if it does not yet exist (O_CREAT) and that the object is open for reading and writing (O_RDWR). The last parameter establishes the directory permissions of the shared-memory object.
A successful call to shm_open() returns an integer file descriptor for the shared-memory object. Once the object is established, the ftruncate() function is used to configure the size of the object in bytes. The call
sets the size of the object to 4, 096 bytes. Finally, the mmap() function establishes a memory-mapped file containing the shared-memory object. It also returns a pointer to the memory-mapped file that is used for accessing the shared-memory object.
Programs showing POSIX shared memory API for producer and consumer
The above programs use the producer–consumer model in implementing shared memory.
- The producer establishes a shared memory object and writes to shared memory, and the consumer reads from shared memory.
- The producer, creates a shared-memory object named OS and writes the famous string “Hello World!” to shared memory.
- The program memory-maps a shared-memory object of the specified size and allows writing to the object. (Obviously, only writing is necessary for the producer.)
- The flag MAP SHARED specifies that changes to the shared memory object will be visible to all processes sharing the object. Notice that we write to the shared-memory object by calling the sprintf() function and writing the formatted string to the pointer ptr.
- After each write, we must increment the pointer by the number of bytes written. The consumer process, reads and outputs the contents of the shared memory.
- The consumer also invokes the shm_unlink() function, which removes the shared-memory segment after the consumer has accessed it.
Silberschatz’s Operating System concepts
This article is contributed by Mayank Rana. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Rabin Cryptosystem with Implementation
- Introduction to Project Lombok in Java and How to get started?
- Simplified International Data Encryption Algorithm (IDEA)
- Introduction to Linux Operating System
- How to Install Ulauncher in Ubuntu?
- Game Theory (Normal-form Game) | Set 6 (Graphical Method [2 X N] Game)
- Game Theory (Normal-form Game) | Set 7 (Graphical Method [M X 2] Game)
- SFML Graphics Library | Quick Tutorial
- Game Theory (Normal-form Game) | Set 4 (Dominance Property-Pure Strategy)
- Goldbach's Weak Conjecture for Odd numbers
- Game Theory (Normal-form Game) | Set 5 (Dominance Property-Mixed Strategy)
- Reduce the number to minimum multiple of 4 after removing the digits
- Transportation Problem | Set 7 ( Degeneracy in Transportation Problem )
- Transportation Problem | Set 6 (MODI Method - UV Method)
Improved By : HamidRamazani