Shared memory is a memory shared between two or more processes. However, why do we need to share memory or some other means of communication?
To reiterate, each process has its own address space, if any process wants to communicate with some information from its own address space to other processes, then it is only possible with IPC (inter process communication) techniques. As we are already aware, communication can be between related or unrelated processes.
Usually, inter-related process communication is performed using Pipes or Named Pipes. Unrelated processes (say one process running in one terminal and another process in another terminal) communication can be performed using Named Pipes or through popular IPC techniques of Shared Memory and Message Queues.
We have seen the IPC techniques of Pipes and Named pipes and now it is time to know the remaining IPC techniques viz., Shared Memory, Message Queues, Semaphores, Signals, and Memory Mapping.
In this chapter, we will know all about shared memory.
We know that to communicate between two or more processes, we use shared memory but before using the shared memory what needs to be done with the system calls, let us see this −
Create the shared memory segment or use an already created shared memory segment (shmget())
Attach the process to the already created shared memory segment (shmat())
Detach the process from the already attached shared memory segment (shmdt())
Control operations on the shared memory segment (shmctl())
Let us look at a few details of the system calls related to shared memory.
#include <sys/ipc.h> #include <sys/shm.h> int shmget(key_t key, size_t size, int shmflg)
The above system call creates or allocates a System V shared memory segment. The arguments that need to be passed are as follows −
The first argument, key, recognizes the shared memory segment. The key can be either an arbitrary value or one that can be derived from the library function ftok(). The key can also be IPC_PRIVATE, means, running processes as server and client (parent and child relationship) i.e., inter-related process communiation. If the client wants to use shared memory with this key, then it must be a child process of the server. Also, the child process needs to be created after the parent has obtained a shared memory.
The second argument, size, is the size of the shared memory segment rounded to multiple of PAGE_SIZE.
The third argument, shmflg, specifies the required shared memory flag/s such as IPC_CREAT (creating new segment) or IPC_EXCL (Used with IPC_CREAT to create new segment and the call fails, if the segment already exists). Need to pass the permissions as well.
Note − Refer earlier sections for details on permissions.
This call would return a valid shared memory identifier (used for further calls of shared memory) on success and -1 in case of failure. To know the cause of failure, check with errno variable or perror() function.
#include <sys/types.h> #include <sys/shm.h> void * shmat(int shmid, const void *shmaddr, int shmflg)
The above system call performs shared memory operation for System V shared memory segment i.e., attaching a shared memory segment to the address space of the calling process. The arguments that need to be passed are as follows −
The first argument, shmid, is the identifier of the shared memory segment. This id is the shared memory identifier, which is the return value of shmget() system call.
The second argument, shmaddr, is to specify the attaching address. If shmaddr is NULL, the system by default chooses the suitable address to attach the segment. If shmaddr is not NULL and SHM_RND is specified in shmflg, the attach is equal to the address of the nearest multiple of SHMLBA (Lower Boundary Address). Otherwise, shmaddr must be a page aligned address at which the shared memory attachment occurs/starts.
The third argument, shmflg, specifies the required shared memory flag/s such as SHM_RND (rounding off address to SHMLBA) or SHM_EXEC (allows the contents of segment to be executed) or SHM_RDONLY (attaches the segment for read-only purpose, by default it is read-write) or SHM_REMAP (replaces the existing mapping in the range specified by shmaddr and continuing till the end of segment).
This call would return the address of attached shared memory segment on success and -1 in case of failure. To know the cause of failure, check with errno variable or perror() function.
#include <sys/types.h> #include <sys/shm.h> int shmdt(const void *shmaddr)
The above system call performs shared memory operation for System V shared memory segment of detaching the shared memory segment from the address space of the calling process. The argument that needs to be passed is −
The argument, shmaddr, is the address of shared memory segment to be detached. The to-be-detached segment must be the address returned by the shmat() system call.
This call would return 0 on success and -1 in case of failure. To know the cause of failure, check with errno variable or perror() function.
#include <sys/ipc.h> #include <sys/shm.h> int shmctl(int shmid, int cmd, struct shmid_ds *buf)
The above system call performs control operation for a System V shared memory segment. The following arguments needs to be passed −
The first argument, shmid, is the identifier of the shared memory segment. This id is the shared memory identifier, which is the return value of shmget() system call.
The second argument, cmd, is the command to perform the required control operation on the shared memory segment.
Valid values for cmd are −
IPC_STAT − Copies the information of the current values of each member of struct shmid_ds to the passed structure pointed by buf. This command requires read permission to the shared memory segment.
IPC_SET − Sets the user ID, group ID of the owner, permissions, etc. pointed to by structure buf.
IPC_RMID − Marks the segment to be destroyed. The segment is destroyed only after the last process has detached it.
IPC_INFO − Returns the information about the shared memory limits and parameters in the structure pointed by buf.
SHM_INFO − Returns a shm_info structure containing information about the consumed system resources by the shared memory.
The third argument, buf, is a pointer to the shared memory structure named struct shmid_ds. The values of this structure would be used for either set or get as per cmd.
This call returns the value depending upon the passed command. Upon success of IPC_INFO and SHM_INFO or SHM_STAT returns the index or identifier of the shared memory segment or 0 for other operations and -1 in case of failure. To know the cause of failure, check with errno variable or perror() function.
Let us consider the following sample program.
Create two processes, one is for writing into the shared memory (shm_write.c) and another is for reading from the shared memory (shm_read.c)
The program performs writing into the shared memory by write process (shm_write.c) and reading from the shared memory by reading process (shm_read.c)
In the shared memory, the writing process, creates a shared memory of size 1K (and flags) and attaches the shared memory
The write process writes 5 times the Alphabets from ‘A’ to ‘E’ each of 1023 bytes into the shared memory. Last byte signifies the end of buffer
Read process would read from the shared memory and write to the standard output
Reading and writing process actions are performed simultaneously
After completion of writing, the write process updates to indicate completion of writing into the shared memory (with complete variable in struct shmseg)
Reading process performs reading from the shared memory and displays on the output until it gets indication of write process completion (complete variable in struct shmseg)
Performs reading and writing process for a few times for simplication and also in order to avoid infinite loops and complicating the program
Following is the code for write process (Writing into Shared Memory – File: shm_write.c)
/* Filename: shm_write.c */ #include<stdio.h> #include<sys/ipc.h> #include<sys/shm.h> #include<sys/types.h> #include<string.h> #include<errno.h> #include<stdlib.h> #include<unistd.h> #include<string.h> #define BUF_SIZE 1024 #define SHM_KEY 0x1234 struct shmseg { int cnt; int complete; char buf[BUF_SIZE]; }; int fill_buffer(char * bufptr, int size); int main(int argc, char *argv[]) { int shmid, numtimes; struct shmseg *shmp; char *bufptr; int spaceavailable; shmid = shmget(SHM_KEY, sizeof(struct shmseg), 0644|IPC_CREAT); if (shmid == -1) { perror("Shared memory"); return 1; } // Attach to the segment to get a pointer to it. shmp = shmat(shmid, NULL, 0); if (shmp == (void *) -1) { perror("Shared memory attach"); return 1; } /* Transfer blocks of data from buffer to shared memory */ bufptr = shmp->buf; spaceavailable = BUF_SIZE; for (numtimes = 0; numtimes < 5; numtimes++) { shmp->cnt = fill_buffer(bufptr, spaceavailable); shmp->complete = 0; printf("Writing Process: Shared Memory Write: Wrote %d bytes\n", shmp->cnt); bufptr = shmp->buf; spaceavailable = BUF_SIZE; sleep(3); } printf("Writing Process: Wrote %d times\n", numtimes); shmp->complete = 1; if (shmdt(shmp) == -1) { perror("shmdt"); return 1; } if (shmctl(shmid, IPC_RMID, 0) == -1) { perror("shmctl"); return 1; } printf("Writing Process: Complete\n"); return 0; } int fill_buffer(char * bufptr, int size) { static char ch = 'A'; int filled_count; //printf("size is %d\n", size); memset(bufptr, ch, size - 1); bufptr[size-1] = '\0'; if (ch > 122) ch = 65; if ( (ch >= 65) && (ch <= 122) ) { if ( (ch >= 91) && (ch <= 96) ) { ch = 65; } } filled_count = strlen(bufptr); //printf("buffer count is: %d\n", filled_count); //printf("buffer filled is:%s\n", bufptr); ch++; return filled_count; }
Writing Process: Shared Memory Write: Wrote 1023 bytes Writing Process: Shared Memory Write: Wrote 1023 bytes Writing Process: Shared Memory Write: Wrote 1023 bytes Writing Process: Shared Memory Write: Wrote 1023 bytes Writing Process: Shared Memory Write: Wrote 1023 bytes Writing Process: Wrote 5 times Writing Process: Complete
Following is the code for read process (Reading from the Shared Memory and writing to the standard output – File: shm_read.c)
/* Filename: shm_read.c */ #include<stdio.h> #include<sys/ipc.h> #include<sys/shm.h> #include<sys/types.h> #include<string.h> #include<errno.h> #include<stdlib.h> #define BUF_SIZE 1024 #define SHM_KEY 0x1234 struct shmseg { int cnt; int complete; char buf[BUF_SIZE]; }; int main(int argc, char *argv[]) { int shmid; struct shmseg *shmp; shmid = shmget(SHM_KEY, sizeof(struct shmseg), 0644|IPC_CREAT); if (shmid == -1) { perror("Shared memory"); return 1; } // Attach to the segment to get a pointer to it. shmp = shmat(shmid, NULL, 0); if (shmp == (void *) -1) { perror("Shared memory attach"); return 1; } /* Transfer blocks of data from shared memory to stdout*/ while (shmp->complete != 1) { printf("segment contains : \n\"%s\"\n", shmp->buf); if (shmp->cnt == -1) { perror("read"); return 1; } printf("Reading Process: Shared Memory: Read %d bytes\n", shmp->cnt); sleep(3); } printf("Reading Process: Reading Done, Detaching Shared Memory\n"); if (shmdt(shmp) == -1) { perror("shmdt"); return 1; } printf("Reading Process: Complete\n"); return 0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