"Copyright Notice: respect for the original, reproduced please retain the source: blog.csdn.net/shallnet or .../gentleliu, the article is for learning communication only, do not use for commercial purposes"
System V shared memory is similar to POSIX shared memory, and System V shared memory is called the Shmget function and the Shamat function.
The Shmget function creates a shared memory area, or accesses an existing memory area, similar to the system calling the open and POSIX shared memory shm_open functions of shared memory. The Shmget function prototype is:
#include <sys/ipc.h> #include <sys/shm.h> int shmget (key_t key, size_t size, int shmflg);
Key: Function Ftok return value, or ipc_private, when using ipc_private, preferably two process space is shared, such as parent-child process, otherwise the current process generated shared memory identifier (return value), in another process is not easy to obtain;
The Ftok function prototype is: key_t ftok (const char *pathname, int proj_id); The parameter pathname is the file absolute pathname, and proj_id is an integer identifier that converts an existing pathname and an integer identifier into a key_t value (the return value), called the IPC key.
Size: Creates a new shared memory size, which is a parameter that is not 0 when a new piece of shared memory is created. If you are reading a piece of shared memory, the value can be 0.
SHMFLG: Combination of Read and write permission values. Ipc_creat (Create new shared memory) or ipc_creat| IPC_EXCL (when the shared memory that will be created already exists, the attempt to create will return eexist). In fact, the combination of ipc_creat and IPC_EXCL is similar to the o_creat and o_excl combinations of the open function.
The function returns the identity of the shared memory area. The SHMXXX function operation shared memory will use this function to return a value. This function is similar to the POSIX shared memory Shm_open function function.
When Shmget creates or opens a shared memory area, it is necessary to use the function Shmat to connect the slice of shared memory to the current process space, and when a process has finished using shared memory, use the function SHMDT to disconnect and share the memory link.
#include <sys/types.h> #include <sys/shm.h> void *shmat (int shmid, const void *shmaddr, int shmflg) ; int SHMDT (const void *SHMADDR);
Shmid: Is the shared memory identifier returned by the function Shmget function.
SHMADDR: Connect to the address of the calling process address space, if the parameter is NULL, the system chooses an appropriate address, if SHMADDR is not empty and SHMFLG specifies the option shm_rnd, Then the corresponding shared memory is linked to a Shmlab constant that is rounded down by the address specified by the SHMADDR parameter. If SHMADDR is not empty and SHMFLG does not specify SHM_RND, the shared memory address is linked to the address specified by the SHMADDR parameter.
SHMFLG: You can specify SHM_RND and shm_rdonly (read-only), and if you specify the SHM_RDONLY option, the calling process has read access to that slice of shared memory, otherwise the process will have read and write access to that slice of memory.
The function shmdt does not delete the specified shared memory, it just disconnects from the slice's shared memory. When a process terminates, the shared memory linked by the process is automatically disconnected.
The Shmat function successfully returned the current process shared memory address, failed to return (void *) -1;SHMDT successfully returned 0, failed to return 1;
Removing shared memory requires the function Shmctl call the Ipc_rmid command to complete.
#include <sys/ipc.h> #include <sys/shm.h> int shmctl (int shmid, int cmd, struct shmid_ds *buf);
Shmid: Shared memory area identification;
CMD: Operation command for Shared memory, command Ipc_rmid destroy (destroy) a piece of shared memory, destroy all shmat,shmdt,shmctl to the memory operation will be invalidated, destroy the shared memory to wait until the shared memory reference count becomes 0; ipc_ The SET command sets the SHMID_DS structure member, IPC_STAT returns the current shared memory structure, and the remaining commands view the man manual.
BUF: For pointing to shmid_ds data structure;
Example of system V shared memory:
Server process:
int Sln_shm_get (char *shm_file, void **mem, int mem_len) {int shmid; key_t key; if (NULL = = fopen (shm_file, "w+")) {printf ("fopen:%s\n", Strerror (errno)); return-1; } key = Ftok (shm_file, 0); if (Key < 0) {printf ("Ftok:%s\n", Strerror (errno)); return-1; } Shmid = Shmget (key, Mem_len, ipc_creat); if (Shmid < 0) {printf ("Shmget:%s\n", Strerror (errno)); return-1; } *mem = (void *) Shmat (shmid, NULL, 0); if (void *)-1 = = *mem) {printf ("Shmat:%s\n", Strerror (errno)); return-1; } return shmid;} int main (int argc, const char *argv[]) {char *shm_file = NULL; char *shm_buf = NULL; int shmid; Shmid = Sln_shm_get (Shm_ipc_filename, (void * *) &shm_buf, Shm_ipc_max_len); if (Shmid < 0) {return-1; } snprintf (Shm_buf, Shm_ipc_max_len, "Hello system V Shaare memory ipc! This was write by server. "); Sleep (15); printf ("System V server Delete Share mEmory segment!\n "); SHMDT (SHM_BUF); Shmctl (Shmid, Ipc_rmid, NULL); The server destroy the slice after 15 seconds, at which point the client process will not get the contents of the shared memory return 0;
Client process:
int Sln_shm_get (char *shm_file, void **mem, int mem_len) { int shmid; key_t key; Key = Ftok (shm_file, 0); if (Key < 0) { printf ("Ftok:%s\n", Strerror (errno)); return-1; } Shmid = Shmget (key, Mem_len, ipc_creat); if (Shmid < 0) { printf ("Shmget:%s\n", Strerror (errno)); return-1; } *mem = (void *) Shmat (shmid, NULL, 0); if (void *)-1 = = *mem) { printf ("Shmat:%s\n", Strerror (errno)); return-1; } return shmid;} int main (int argc, const char *argv[]) { char *shm_buf = NULL; int i; if (Sln_shm_get (Shm_ipc_filename, (void * *) &shm_buf, Shm_ipc_max_len) < 0) { return-1; } printf ("IPC Client get:%s\n", shm_buf); return 0;}
At run time, the server process is executed first, and the current system shared memory can be viewed using command IPCS:
You can see that there is a shared memory area where key is: 0x0010a797, and the shared memory ID is: 131072
#./client IPC Client Get:hello system V Shaare memory ipc! This is the write by server. #
After the server process destroy shared memory, repeat the steps above,
# IPCS------Message Queues--------Key msqid owner perms used-bytes messages------Shared Memory Segments--------key Shmid owner perms bytes nattch status------Semaphore Arrays--------Key semid owner Perms Nsems
At this point the client cannot get the previously shared memory content.
In addition, the IPCRM command can delete the specified shared memory area on the command line.
By reading the file/proc/sys/kernel/shmmax you can get the maximum amount of shared memory supported by the system.
# cat/proc/sys/kernel/shmmax33554432#
Can see my current system support maximum shared memory value is: 32M.
The example above can see that System V shared memory is similar to POSIX shared memory, but the size of the POSIX shared memory can be changed at any time by Ftruncate, while the shared memory size of System V is determined at shmget.
Similarly, System V shared memory needs to be synchronized between multiple processes most of the time, and System V can be implemented with its own semaphore, and details will be explained later in the relevant column.
Download the source code in this section:
http://download.csdn.net/detail/gentleliu/8140887
About Linux IPC (v): System V Shared memory