shmget,shmat of the shared memory

  • 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 called shared 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 with shmget :

 #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 the ftok function. however, the IPC_PRIVATE is a special ID to apply for private shared memory witin in current thread.
    - size_t size user wanna get memory size
  • shmflg permission flag (like 0666)

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 memory shmat, 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 the shmget

  • shmaddr: The pointer to the shared memory, if NULL, 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 attached share 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 the shmat 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.

  • 18
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值