Shared memory: A portion of the physical memory is shared as multiple processes.
Shared memory is one of the fastest ways to share data between processes, a process writes data to a shared memory area, and all processes that share that memory can immediately see the contents.
Shared Memory Implementation steps:
First, create shared memory, use the Shmget function.
mapping shared memory, mapping the shared memory created to a specific process space, using the Shmat function.
Create shared memory Shmget:
Intshmget (key_t key, size_t size, int shmflg)
Function: Get a shared memory identifier or create a shared memory object and return the shared memory identifier.
Key:0 (ipc_private) creates a shared memory object
Size: An integer greater than 0 that is the size, in bytes, of the new shared memory. When only shared memory is acquired, specify 0.
SHMFLG:
0 indicates that the shared memory identifier is taken, and the function will complain if it does not exist;
Ipc_creat creates a shared memory if there is no shared memory with key values equal to key in the kernel, and returns the identifier of the shared memory if such shared memory exists;
Ipc_creat| IPC_EXCL: If there is no shared memory with key values equal to key in the kernel, a new message queue is created, and an error occurs if there is such shared memory;
function return value: Returns the identifier of the memory in case of success, and returns 1 for error.
Map shared memory to the address space of the calling process Shmat:
Void*shmat (int shmid, const void *shmaddr, int shmflg)
MSQID: Shared Memory identifier
SHMADDR: Specifies where the shared memory appears in the process memory address, specifying NULL directly to allow the kernel to determine a suitable address location for itself.
Shmflg:shm_rdonly read-only mode, others for read-write mode
function return value: Returns the attached shared memory address for success; error return-1, the cause of errors is in error
Disconnect the shared memory connection SHMDT:
INTSHMDT (const void *SHMADDR)
Function: Incoming shmaddr, connect the shared memory start address, return 0 if the fracture succeeds, and return 1 for error.
Parent-Child interprocess communication instance:
#include
#include
#include
#include
#include
#include
int main (int argc, char **argv) {
if (argc< 2) {//need to enter shared data
printf ("Pleaseinput the Shared DATA.N");
Exit (-1);
}
Intshmid;
Shmid= Shmget (0,1024,ipc_creat);
if (shmid==-1) {//Request shared memory failed
printf ("Createshare memory FAILED.N");
Exit (-1);
}
if (fork ()) {//parent process
CHAR*P_SHMADDR;
P_shmaddr= Shmat (shmid, NULL, 0); Map to an address in the parent process
memset (p_shmaddr,0, 1024); Initializing shared memory
strcpy (p_shmaddr,argv[1]); Copy shared data to shared memory
Wait (NULL); Wait for child process to end
Exit (0);
}
else{
Sleep (2); Waiting for the parent process to write data
CHAR*C_SHMADDR;
C_shmaddr= Shmat (shmid,null,0); Map to a child process an address, specifically by kernel
printf ("Theshare data is:%SN", c_shmaddr); Child process output shared data
Exit (0);
}
}