sharedmemory.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /************** Begin of sharedmemory.c *******************************************/
  2. #if defined(linux) && defined(HAVE_SHM)
  3. /***************************************************************************
  4. |saharedmemory.c| - saharedmemory共享内存对象 自己定义的一些操作函数的实现文件
  5. -------------------
  6. begin : |03-6-20|
  7. modify : |03-6-20|
  8. copyright : (C) |YEAR| by |wenhm(jesse)|
  9. email : |EMAIL|
  10. ***************************************************************************/
  11. #include <stdio.h>
  12. #include <ctype.h>
  13. #include <unistd.h>
  14. #include <stdlib.h>
  15. #include <sys/types.h>
  16. #include <sys/ipc.h>
  17. #include <sys/sem.h>
  18. #include <sys/shm.h>
  19. #include <errno.h>
  20. #include <string.h>
  21. ///#include "sharedmemory.h"
  22. //创建或关键字为Key的含有大小为size的共享内存对象,并将共享内存对象id放于shmid中返回。
  23. //若共享内存对象已存在则取得该对象
  24. int create_sharedmemory(key_t key, int size)
  25. {
  26. int shmid;
  27. printf("Attempting to create new sharde memory object whose size is %d\n",size);
  28. //int shmget(key_t key,int size,int shmflg);
  29. if((shmid = shmget(key,size,IPC_CREAT|0666)) == -1)
  30. {
  31. fprintf(stderr, "shared memory object apply error:%s!\n",strerror(errno));
  32. }
  33. return shmid;
  34. }
  35. //创建关键字Key为IPC_PRIVATE(即关键字由系统选定)的含有大小为size的共享内存对象,并将共享内存对象id放于shmid中返回。
  36. //因使用IPC_PRIVATE创建的共享内存的key都一样,所以key就不要了。
  37. int create_sharedmemory_private(int size)
  38. {
  39. int shmid;
  40. struct shmid_ds buf;
  41. printf("Attempting to create new sharde memory object whose size is %d\n",size);
  42. //int shmget(key_t key,int size,int shmflg);
  43. if((shmid = shmget(IPC_PRIVATE,size,IPC_CREAT|0666)) == -1)
  44. {
  45. fprintf(stderr, "sharde memory object already exists!\n");
  46. }
  47. if(shmctl(shmid,IPC_STAT, &buf) == -1)
  48. {
  49. fprintf(stderr, "shmctl(shmid, IPC_STAT, buf) error!\n");//int shmget(key_t key,int size,int shmflg);
  50. };
  51. return shmid;
  52. }
  53. //将共享内存attach到进程自己得空间内,函数返回指向映射内存的指针。
  54. void *attach_sharedmemory(int shmid )
  55. {
  56. return(shmat(shmid, 0, 0));
  57. }
  58. //将共享内存disattach,唯一的参数是共享内存映射的指针
  59. //返回值: -1 on error: errno = EINVAL (Invalid attach address passed)
  60. int disattach_sharedmemory(void* shm)
  61. {
  62. return(shmdt((char*)shm));
  63. }
  64. //获取共享内存对象shmid的大小
  65. int getsize_sharedmemory(int shmid)
  66. {
  67. struct shmid_ds buf;
  68. if(shmctl(shmid,IPC_STAT, &buf) == -1)
  69. {
  70. fprintf(stderr, "shmctl(shmid, IPC_STAT, buf) error!\n");
  71. };
  72. return buf.shm_segsz;
  73. }
  74. //删除共享内存对象
  75. int del_sharedmemory(int shmid)
  76. {
  77. /* 共享内存对象并不随程序的结束而被删除,如果我们没删除的话(将1改为0)
  78. 可以用ipcs命令查看到信号灯,用ipcrm可以删除共享内存对象的*/
  79. return shmctl(shmid,IPC_RMID,0);
  80. }
  81. #endif
  82. /** 获取模块使用次数
  83. @return long.
  84. */
  85. long GetModuleUsage()
  86. {
  87. long lModuleUsage = -1;
  88. #if defined(linux ) && defined(HAVE_SHM)
  89. //以8个8来作为共享内存的key
  90. #define MODULE_USAGE_SHMKEY 88888888
  91. gs_iModuleUsage_shmid = create_sharedmemory(MODULE_USAGE_SHMKEY, sizeof(long));
  92. void *ptrModuleUsageBuffer = attach_sharedmemory(gs_iModuleUsage_shmid);
  93. if (NULL != ptrModuleUsageBuffer)
  94. {
  95. lModuleUsage = *(long *)ptrModuleUsageBuffer;
  96. ///释放共享内存
  97. disattach_sharedmemory(ptrModuleUsageBuffer);
  98. ///del_sharedmemory(gs_iModuleUsage_shmid);
  99. }
  100. #endif
  101. return lModuleUsage;
  102. }
  103. /** 设置模块使用次数
  104. @return void.
  105. */
  106. void SetModuleUsage(long lModuleUsage)
  107. {
  108. #if defined(linux ) && defined(HAVE_SHM)
  109. gs_iModuleUsage_shmid = create_sharedmemory(MODULE_USAGE_SHMKEY, sizeof(long));
  110. void *ptrModuleUsageBuffer = attach_sharedmemory(gs_iModuleUsage_shmid);
  111. if (NULL != ptrModuleUsageBuffer)
  112. {
  113. *(long *)ptrModuleUsageBuffer = lModuleUsage;
  114. ///释放共享内存
  115. disattach_sharedmemory(ptrModuleUsageBuffer);
  116. ///del_sharedmemory(gs_iModuleUsage_shmid);
  117. }
  118. #endif
  119. }
  120. /** 释放使用的共享内存
  121. @return void.
  122. */
  123. void ReleaseModuleUsage()
  124. {
  125. #if defined(linux ) && defined(HAVE_SHM)
  126. long lModuleUsage = 0;
  127. gs_iModuleUsage_shmid = create_sharedmemory(MODULE_USAGE_SHMKEY, sizeof(long));
  128. void *ptrModuleUsageBuffer = attach_sharedmemory(gs_iModuleUsage_shmid);
  129. if (NULL != ptrModuleUsageBuffer)
  130. {
  131. lModuleUsage = *(long *)ptrModuleUsageBuffer;
  132. ///释放共享内存
  133. disattach_sharedmemory(ptrModuleUsageBuffer);
  134. if (lModuleUsage == 0)
  135. {
  136. del_sharedmemory(gs_iModuleUsage_shmid);
  137. }
  138. }
  139. #endif
  140. }
  141. /************** Begin of sharedmemory.c *******************************************/