Introduction
We know that two main methods for inter-process communication are shared memory and message passing. In the shared memory method, two processes communicate with each other using common shared memory. The shared memory is present in the address space of the communicating process.
Unlike the message passing technique, in shared memory, only two copies of the data are created. The first copy is from the input file into the shared memory, and the second copy is from the shared memory to the output file.
In this article, we will see how this method is technically implemented. We shall analyze the various system calls used in the shared memory method for inter-process communication.
Properties of shared memory:
- It is a comparatively faster method than the message queue.
- It does not require an extra kernel buffer.
- Race condition can be possible.
- It is a relatively safe method.
Also see, Introduction to Process Synchronisation, Multiprogramming vs Multitasking
System call used for shared memory
shmget()
It is used to obtain access to a shared memory segment by a process involved in the inter-process communication. A process allocates a shared memory by using the shmget() function.
Syntax:
#include <sys/shm.h>
int shmget (key_t key, size_t size, int shmflg)
- Key: It is an access value associated with the shared memory segment
- Size: It represents the size (number of bytes) that has been requested for the shared memory segment.
Upon successful allocation of the shared memory segment, then shmget() returns an identifier. It returns -1 in case of an error.
2. shmat()
It is used to attach the shared memory segments. Once the shared memory segment is created by a process, the other process that wants to access that segment for inter-process communication should attach itself to the segment. This is done with the help of shmat(). In order to access the shared memory segment, the process must attach to the address space of the process that created the segment.
Syntax:
#include <sys/shm.h>
void *shmat (int shm_id, const void *shm_addr, int shmflg);
- First parameter: The first parameter is the shm_id which is the shared memory id. It is the identifier that is returned by the shmget().
- Second parameter: The second parameter represents the address at which the segment is supposed to be attached to the current process. It is always a NULL pointer. It is kept NULL so that the system can choose the address at which the memory is available.
- Third parameter: The third parameter represents the flag value.
If the shmat() system call is successful, it will return the address of the attached memory segment. In case there is an error, it will return -1.
3. shmdt()
This function call is used to detach memory segments. When the work of a process completes, i.e., it has completed sharing the information from the shared memory segment; it must be detached from the shared memory segment. The shmdt() function will detach the shared memory segment from the current process.
Syntax:
#include <sys/shm.h>
int shmdt (const void *shm_addr)
- The shmdt() takes the pointer to the address that is returned by the shmget() as the parameter.
After successfully detaching the process from the shared memory segment, it returns 0. In case of an error, it returns -1.
4. shmctl()
Once the use of the shared memory is over, it is important to deallocate it. It should not unnecessarily use any memory. So to deallocate the shared memory segment, the shmctl() function call is used. The shmctl() function returns the information about a shared memory segment, and it can be modified also.
Syntax:
#include <sys/shm.h>
int shmctl (int shm_id, int command, struct shmid_ds *buf);
- First parameter: The first parameter shm_id is an identifier that is returned by the shmget() function.
- Second parameter: It represents the various command values such as IPC_STAT, IPC_SET, IPC_RMID.
- Third parameter: The third parameter, *buf, is a pointer to the structure of type struct shmid_ds, which is defined in <sys/shm.h> containing the modes and permissions for the shared memory.
5. ftok()
Purpose: ftok() generates a unique key for IPC (Inter-Process Communication) mechanisms like shared memory, semaphores, and message queues.
Parameters: It takes a file path (pathname) and a project identifier (proj_id) as input.
Key Generation: The key is based on the inode number of the file specified by pathname and the least significant 8 bits of proj_id.
Return Value: Returns a key_t key on success, -1 on failure (with errno set accordingly).
Usage: Essential for identifying and accessing IPC resources tied to a specific file, ensuring efficient communication between processes in Unix-like operating systems