-
Shared Memory
Definition of shared memory
Linux allow two different processes to see the same resource, by opening up a memory space in physical memory, which is calledshared memory
, Access this shared memory, so that the two threads can communicate with each other. -
Note: Shared memory operations are not blocked by default. If multiple threads read and write shared memory at the same time, data confusion may get occurred. Shared memory requires other mechanisms to ensure data synchronization between processes, such as
semaphores
that are not provided in shared memory. -
New a shared memory (
shmget
)
users could apply for shared memory from the kernel withshmget
:
#include <sys/shm.h>
int shmget(key_t key, size_t size, int shmflg);
key
is unique identification number,which set by the user at will (keep unique character), typically through theftok
function. however, theIPC_PRIVATE
is a special ID to apply for private shared memory witin in current thread.
- size_t size
user wanna get memory sizeshmflg
permission flag (like0666
)
eg:
uint keytry = 0;
while( -1 == (uartWrId = shmget( UARTWR_SMKEY, sizeof(struct modBuff), 0666)))
{
if(keytry >= 500)
break;
keytry ++;
usleep(10000);
}
if ( -1 == uartWrId)
{
perror("Failed to locate BaseComm write share memory");
return;
}
uartWrVoid = shmat(uartWrId, (void *)0, 0);
if ( (void *)-1 == uartWrVoid)
{
perror("Failed to make BaseComm write share memory accessible");
return;
}
-
NOTE : that shared memory is measured in “pages”, and one page is 0x1000 (4KB) bytes, so it is generally recommended to apply for a shared memory size,which is an integral multiple of 4KB!
-
Link the shared memory (
shmat
)
After opening up the shared memory, user should associate it with the shared memoryshmat
, getting the starting address of the shared memory, thus starting to access the shared memory resource.
#include <sys/shm.h>
void *shmat(int shmid, const void *shmaddr, int shmflg);
-
shmid
: The unique ID of the shared memory to access, which is the return value of theshmget
-
shmaddr
: The pointer to the shared memory, ifNULL
, to be specified by the kernel. -
shmflg
: Shared memory operation permissions flag
SHM_RDONLY
: Read permission, only read data in shared memory
0
: Read and write permissions, can read and write shared memory data
Return value
:
success : Return the pointer to the shared memory.
failed:(void *) -1
is returned. -
Detach a shard memory(
shmdt
)
shmdt
call function api will help detach/separate the attachedshare memory section
from the current thread memory space, keep in mind that the shared memory would still existed in the system memory not freed or destroyed automatically until the system restart or explicitly deleted.
#include <sys/shm.h>
int shmdt(const void *shmaddr);
-
shmaddr
: A pointer to the starting address of the shared memory segment in the current process address space. which typically get through theshmat
function. -
return value
:
success : 0
failed : -1 -
Shared memory control function(shmctl)
#include <sys/shm.h>
int shmctl(int shmid, int cmd, struct shmid_ds *buf);
shmid
: the unique ID of the shared memory.cmd
: Operation cmd-
IPC_STAT
: Get the current shared memory status -
IPC_SET
: Set the status of shared memory -
IPC_RMID
: Marks shared memory to be deleted (here just mark, rather tha really delete the shared memory)
(When the shared memory is only marked as deleted, it will not be deleted immediately. The shared memory will not be deleted until all threads are detached from the shared memory.) buf
:
Note:
cmd == IPC_STAT, buf
as an output parameter, users will be getting the related attribute information of the shared memory
cmd == IPC_SET, buf
as an input parameter, sets the user’s custom attributes into shared memory.
cmd == IPC_RMID, buf
is meaningless. In this case, buf
can be specified as NULL
.
Return value:
success :The return value is greater than or equal to 0
failed : - 1.