1. Basic Features
1) Two or more processes that share the same block of memory that is maintained by the system kernel, whose address space is typically mapped between the heap and the stack.
, each process has its own share memory, is actually a piece of space in the kernel, is the mapping of different processes share memory, the operation of share memory is the kernel of the operation of shared memory, the operation of the form and ordinary memory is no different.
2) No need to copy information, the fastest kind of IPC mechanism, so suitable for large file transmission.
3) need to consider the problem of synchronous access, not over there has not finished writing, this way to study.
4) The kernel maintains a shared memory object in the form of a SHMID_DS structure (describing size, location, etc.) for each shared memory.
struct Shmid_ds { struct ipc_perm shm_perm; Owners and their rights size_t Shm_segsz; Size (in bytes) time_t shm_atime; Last load time time_t shm_dtime; Last unload time time_t shm_ctime; Finally change the time pid_t shm_cpid; Create process PID pid_t shm_lpid; Last load/unload process PID shmatt_t shm_nattch;//Current Load Count ...};
struct Ipc_perm { key_t __key;//Key value uid_t uid; Valid for Master ID gid_t gid; Valid genus group ID uid_t cuid; Valid Creator ID gid_t cgid; Effectively create group ID unsigned short mode; Permission Word unsigned short __seq;//serial number};
2. Common functions:
1) Create/Get shared memory
int Shmget (key_t key, size_t size, int shmflg);
A. The function creates shared memory with the key parameter for the value, or gets the existing shared memory.
B. The size parameter is the number of bytes of shared memory, and it is recommended to take an integer multiple of the memory page bytes (4096). If you want to create shared memory, you must specify the size parameter. The size parameter is 0 if only to get the existing shared memory.
C. SHMFLG Value:
0-Gets, does not exist or fails.
Pc_creat-Creates, does not exist that is created, already exists that is acquired, unless ... or a ipc_excl.
IPC_EXCL-exclusion, existing or failed
D. Successful return of shared memory identity, failed return-1.
2) load shared memory, that is, create a mapping
void* shmat (int shmid, const void* shmaddr,int SHMFLG);
A. Map the shared memory identified by the Shmid parameter to the address space of the calling process.
B. The mapping address can be specified manually by the SHMADDR parameter, or it can be set to null and automatically selected by the system.
C. SHMFLG Value:
0-Use shared memory in read-write mode.
Shm_rdonly-Use shared memory in read-only mode.
Shm_rnd-works only when the SHMADDR parameter is not NULL. Represents an integer multiple of the memory page to which the parameter is taken, as a mapped address.
D. Successful return of the mapped address, failed to return-1.
E. The kernel adds 1 to the load count of the shared memory. (The mappings are counted, similar to the single case)
3) unmount shared memory as de-mapping
int SHMDT (const void* SHMADDR);
A. From the address space of the calling process, remove the shared memory-mapped area that is pointed to by the SHMADDR parameter.
B. Successful return 0, failure return-1.
C. The kernel will reduce the load count of this shared memory by 1.
4) Destroy/control shared memory
int shmctl (int shmid, int cmd, struct shmid_ds* buf);
A. CMD value:
Ipc_stat-Gets the properties of the shared memory, output through the BUF parameter.
Ipc_set-Sets the properties of the shared memory, entered by the BUF parameter, only the following three properties can be set:
Shmid_ds::shm_perm.uid
Shmid_ds::shm_perm.gid
Shmid_ds::shm_perm.mode
Ipc_rmid-Tags delete shared memory. Instead of actually deleting the shared memory, just make a delete tag that prevents it from being loaded, but the load that has been loaded still remains only if the shared memory is loaded with a count of 0 o'clock, it is actually deleted.
B. Successful return 0, failure return-1.
3. Programming model
Wshm.c
#include <stdio.h> #include <unistd.h> #include <sys/shm.h>int main (void) {printf ("Create shared memory ... \ n"); key_t key = Ftok (".", +), if (key = =-1) {perror ("Ftok"); return-1;} int shmid = Shmget (key, 4096, 0644 | Ipc_creat | IPC_EXCL); if (Shmid = =-1) {perror ("Shmget"); return-1;} printf ("Load shared memory ... \ n"); void* shmaddr = Shmat (shmid, NULL, 0); if (shmaddr = = (void*)-1) {perror ("Shmat"); return-1;} printf ("Write shared memory ... \ n"); sprintf (shmaddr, "I am the data written by the%u process. ", Getpid ());p rintf (" Press < Enter > Unload Shared memory (0x%08x/%d) ... ", Key, Shmid), GetChar (); if (SHMDT (shmaddr) = =-1) {perror (" Shmdt "); return-1;} printf ("Press < Enter > Destroy Shared Memory (0x%08x/%d) ...", Key, Shmid); GetChar (); if (Shmctl (Shmid, Ipc_rmid, NULL) = =-1) {perror ("SHMC TL "); return-1;} printf ("Done! \ n "); return 0;}
Using the IPCS-M command we can get the shared memory just created in the system:
Rshm.c
#include <stdio.h> #include <sys/shm.h>int shmstat (int shmid) {struct Shmid_ds shm;if (Shmctl (Shmid, Ipc_sta T, &SHM) = =-1) {perror ("Shmctl"); return-1;} printf ("------------------------------------------------\ n");p rintf ("Shared memory information \ n");p rintf ("----+-------- --------+--------------------------\ n ");p rintf (" | Key Values | 0x%08x\n ", Shm.shm_perm.__key);p rintf (" there | Valid Master ID | %u\n ", Shm.shm_perm.uid);p rintf (" person | Valid Genus Group ID | %u\n ", Shm.shm_perm.gid);p rintf (" and | Valid Creator ID | %u\n ", Shm.shm_perm.cuid);p rintf (" its | Create Group ID effectively | %u\n ", Shm.shm_perm.cgid);p rintf (" right | Permission Word | % #o \ n ", Shm.shm_perm.mode);p rintf (" limited | Serial number | %u\n ", Shm.shm_perm.__seq);p rintf ("----+----------------+--------------------------\ n ");p rintf (" size (bytes) | %u\n ", Shm.shm_segsz);p rintf (" Last Load Time | %s ", CTime (&shm.shm_atime));p rintf (" Last Unload Time | %s ", CTime (&shm.shm_dtime));p rintf (" Last Change Time | %s ", CTime (&shm.shm_ctime));printf ("Create Process ID | %u\n ", Shm.shm_cpid);p rintf (" Last load/unload process ID | %u\n ", Shm.shm_lpid);p rintf (" Current Load Count | %u\n ", Shm.shm_nattch);p rintf ("---------------------+--------------------------\ n "); return 0;} int shmset (int shmid) {struct Shmid_ds shm;if (Shmctl (Shmid, Ipc_stat, &shm) = =-1) {perror ("Shmctl"); return-1;} Shm.shm_perm.mode = 0600;shm.shm_segsz = 8192;if (Shmctl (Shmid, Ipc_set, &shm) = =-1) {perror ("Shmctl"); return-1;} return 0;} int main (void) {printf ("Get shared memory ... \ n"), key_t key = Ftok ("."), if (key = =-1) {perror ("Ftok"); return-1;} int shmid = shmget (key, 0, 0), if (Shmid = =-1) {perror ("Shmget"); return-1;} printf ("Load shared memory ... \ n"); void* shmaddr = Shmat (shmid, NULL, 0); if (shmaddr = = (void*)-1) {perror ("Shmat"); return-1;} Shmstat (Shmid);p rintf ("read shared memory ... \ n");p rintf ("Shared Memory (0x%08x/%d):%s\n", Key, Shmid, shmaddr);p rintf ("Unload shared memory ... \ n"); if (SHMDT (shmaddr) = =-1) {perror ("Shmdt"); return-1;} Shmstat (Shmid);p rintf ("Set shared memory ... \ n"); Shmset (shmid); SHMStat (Shmid);p rintf ("It's done!" \ n "); return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
XSI inter-process communication-----shared memory