1. Basic concepts of shared memory segments
Shared Memory is a memory area shared by several processes. It can be said that it is the fastest form of IPC, because it does not need any intermediate operations, it just maps the memory segment directly to the address space of the calling process. The usage of shared memory segments is similar to that of message communication. First, use the shmget system call to create a shared memory segment for the process. When the shared memory is required, use the System Call shmat to map the applied shared memory segment to the private space of the process, then the process can access it in the same way as accessing other virtual address spaces. Multiple processes access the same shared memory is mutually exclusive, but mutual exclusion control must be performed by all processes associated with the shared memory. Therefore, shared memory is often used with semaphores.
2. system operations on shared memory
A. Create a shared memory system call Syntax:
# Include <sys/SHM. h>
Int shmget (key_t key, int size, int flags );
The key value of the key shared memory, which can be ipc_private or an integer.
Size shared memory Byte Length
Flags shared memory permission bit.
After the shmget call is successful, if the key is specified with a new integer and ipc_creat is set in flags
Returns a newly created shared memory segment identifier. If the specified key already exists
Associated identifier. -1 is returned if the request is unsuccessful.
B. Add a shared memory to the system call syntax of the calling process:
# Include <sys/SHM. h>
Char * shmat (INT shmid, char * shmaddr, int flags)
The identifier of the shared memory created by shmget.
Shmaddr is 0, indicating that the pointer specified by the caller points to the shared segment.
Flags shared memory permission bit
After the shmat call is successful, the first address of the added shared memory is returned.
C. The system call syntax for separating the shared memory from the calling process:
# Include <sys/SHM. h>
Int shmdt (char * shmadr );
Pointer pointing to additional shared memory in the shmadr Process
If the shmdt call succeeds, the additional count is decreased. If the count is 0, the shared memory is deleted. Call failed
-1 is returned.
Perform the following exercises to create a shared memory segment, and then call shmat twice to map it to different locations in the process data space (the starting address is determined by the kernel ), then, write to the shared memory through the connection address shmadd1 and read data from the shared memory and output the data through the connection address shmadd2:
View plaincopy to clipboardprint?
- /* Pai_memory.c */
- # Include <sys/types. h>
- # Include <sys/IPC. h>
- # Include <sys/SHM. h>
- # Include <stdio. h>
- # Include <stdlib. h>
- # Define bufsz 2048
- Int main ()
- {
- Int shmid, I;
- Int * PTR;
- Int key;
- Char * shmadd1, * shmadd2;
- /* Create shared memory */
- Key = ftok (".", 1 );
- If (shmid = shmget (Key, bufsz, 0666) <0)
- {
- Perror ("shmget ");
- Exit (1 );
- }
- Else
- Printf ("created shared-memory: % d/N", shmid );
- System ("IPCS-M ");
- /* Map the shared memory field to the data space of the process,
- Here, the second parameter is 0, indicating the address selected by the kernel.
- Return the first address of the data space in the shared memory segment */
- If (shmadd1 = shmat (shmid, 0, 0) <(char *) 0)
- {
- Perror ("shmat1 ");
- Exit (1 );
- }
- Else
- Printf ("attached shared-memory/N ");
- If (shmadd2 = shmat (shmid, 0, 0) <(char *) 0)
- {
- Perror ("shmat1 ");
- Exit (1 );
- }
- Else
- Printf ("attached shared-memory/N ");
- System ("IPCS-M ");
- /* Write data from address 1 to the shared memory */
- PTR = (int *) shmadd1;
- For (I = 0; I <6; I ++)
- {
- * PTR ++ = I;
- }
- /* Read data from address 2 from the shared memory segment */
- PTR = (int *) shmadd2;
- For (I = 0; I <6; I ++)
- {
- Printf ("value: % d/N", * PTR ++ );
- }
- /* Delete shared memory */
- If (shmdt (shmadd1) <0 ){
- Perror ("shmdt ");
- Exit (1 );
- }
- Else
- Printf ("deleted shared-memory/N ");
- If (shmdt (shmadd2) <0 ){
- Perror ("shmdt ");
- Exit (1 );
- }
- Else
- Printf ("deleted shared-memory/N ");
- System ("IPCS-M ");
- Exit (0 );
- }