Linux inter-process communication (System V) --- shared memory
Principle of shared memory IPC
The shared-memory inter-process communication mechanism is mainly used to achieve a large amount of data transmission between processes, as shown in the following figure:
Shared Memory is a memory space separately opened in the memory. This memory space has its own unique data structure, including access permissions, size, and recent access time. The data structure is defined as follows:
From/usr/include/linux/shm. hstruct shmid_ds {struct ipc_perm shm_perm;/* operation perms operation permission */int shm_segsz;/* size of segment (bytes) segment length size */_ kernel_time_t shm_atime; /* last attach time recent attach time */_ kernel_time_t shm_dtime;/* last detach time recent detach time */_ kernel_time_t shm_ctime; /* last change time recent change time */_ kernel_ipc_pid_t shm_cpid;/* pid of creator pid */_ kernel_ipc_pid_t shm_lpid; /* pid of last operator recent operation pid */unsigned short shm_nattch;/* no. of current attaches */unsigned short shm_unused;/* compatibility */void * shm_unused2;/* ditto-used by DIPC */void * shm_unused3;/* unused */};
Before using the shared memory space, the two processes need to establish a connection between the process address space and the shared memory space to mount the shared memory space to the process.
The system limits the shared memory as follows:
# Define SHMMAX 0x2000000/* max shared seg size (bytes) Maximum shared segment size */# define SHMMIN 1/* min shared seg size (bytes) minimum shared block size */# define SHMMNI 4096/* max num of segs system wide */# define SHMALL (SHMMAX/getpagesize () * (SHMMNI/16 )) # define shmseg shmmni/* max shared segs per process */
Linux shared memory management
1. Create shared memory
# Include <sys/ipc. h> # include <sys/shm. h>/** the first parameter is the key value. Generally, the ftok () function generates * The second parameter is the size of the shared memory segment to be created (in bytes) * The third parameter is used to identify the creation ID of the shared memory segment */int shmget (key_t key, size_t size, int shmflg );
2. Shared Memory Control
# Include <sys/ipc. h> # include <sys/shm. h>/** the first parameter is the shared memory identifier for the operation * The second parameter is the operation to be executed * The third parameter is the temporary shared memory variable information in the shmid_ds structure */int shmctl (int shmid, int cmd, struct shmid_ds * buf );
3. Map shared memory objects
The system calls the shmat () function to map a shared memory field to the Data Segment of the calling process and return the first address of the memory space. The function declaration is as follows:
# Include <sys/types. h> # include <sys/shm. h>/** the first parameter is the identifier of the shared memory to be operated * The second parameter is used to specify the ing address of the shared memory. If it is not 0, this parameter is used, if the value is 0, the system allocates * The third parameter to specify the access permission and ing condition for the shared memory segment */void * shmat (int shmid, const void * shmaddr, int shmflg );
4. Detach shared memory objects
After using the shared memory space, you need to use the shmdt () function call to separate it from the current process. The function declaration is as follows:
# Include <sys/types. h> # include <sys/shm. h>/** parameter is the first address of the allocated shared memory */int shmdt (const void * shmaddr );
Conventions observed between parent and child processes in the sharing
1. After a sub-process is created using the fork () function, the process inherits the shared memory mounted by the parent process. 2. If exec () is called to execute a new program, all mounted shared memory will be automatically detached. 3. If the exit () function is called in a process, all mounted shared memory will be detached from the current process.
Program instance
Apply for a shared memory segment. The parent process stores an integer in the first address, and the child process reads it.
# Include <stdio. h> # include <sys/ipc. h> # include <sys/shm. h> # include <sys/types. h> # include <unistd. h> # include <stdlib. h> # define SHM_SIZE 1024int main () {int shm_id, pid; int * ptr = NULL;/* apply for shared memory */shm_id = shmget (key_t) 1004, SHM_SIZE, IPC_CREAT | 0600);/* ing shared memory to process address space */ptr = (int *) shmat (shm_id, 0, 0 ); printf ("Attach addr is % p \ n", ptr); * ptr = 1004; printf ("The Value of Parent is: % d \ n ",* Ptr); if (pid = fork () =-1) {perror ("fork Err"); exit (0);} else if (! Pid) {printf ("The Value of Child is: % d \ n", * ptr); exit (0);} else {sleep (1 ); /* unmap */shmdt (ptr);/* Delete shared memory */shmctl (shm_id, IPC_RMID, 0);} return 0 ;}
Output result: