test_info.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <sys/mman.h>
  5. #include <sys/shm.h>
  6. #include <sys/ipc.h>
  7. #include <string.h>
  8. #include <sys/wait.h>
  9. void print_shmidds(struct shmid_ds *shminfo)
  10. {
  11. struct ipc_perm shm_perm = shminfo->shm_perm;
  12. printf("ipc_perm:\n");
  13. printf("ipc_perm_key: %d\n", shm_perm.__key);
  14. printf("uid: %d\n", shm_perm.uid);
  15. printf("gid: %d\n", shm_perm.gid);
  16. printf("cuid: %d\n", shm_perm.cuid);
  17. printf("cgid: %d\n", shm_perm.cgid);
  18. printf("mode: %d\n", shm_perm.mode);
  19. printf("seq: %d\n", shm_perm.__seq);
  20. printf("\n");
  21. printf("shmid_ds:\n");
  22. printf("shm_atime: %lu\n", shminfo->shm_atime);
  23. printf("shm_dtime: %lu\n", shminfo->shm_dtime);
  24. printf("shm_ctime: %lu\n", shminfo->shm_ctime);
  25. printf("shm_segsz: %lu\n", shminfo->shm_segsz);
  26. printf("shm_cpid: %d\n", shminfo->shm_cpid);
  27. printf("shm_lpid: %d\n", shminfo->shm_lpid);
  28. printf("shm_nattch: %lu\n", shminfo->shm_nattch);
  29. printf("\n");
  30. }
  31. const int SHM_SIZE = 9999;
  32. int main()
  33. {
  34. int shmid;
  35. char *shmaddr;
  36. key_t key = 6666;
  37. // 测试shmget
  38. shmid = shmget(key, SHM_SIZE, 0666 | IPC_CREAT);
  39. if (shmid < 0)
  40. {
  41. perror("shmget failed");
  42. exit(EXIT_FAILURE);
  43. }
  44. // 测试shmat
  45. shmaddr = shmat(shmid, 0, 0);
  46. // IPC_STAT:
  47. printf("\n");
  48. printf("IPC_STAT:\n");
  49. struct shmid_ds shminfo2;
  50. if (shmctl(shmid, IPC_STAT, &shminfo2) == -1)
  51. {
  52. // 获取共享内存段信息
  53. perror("shmctl");
  54. exit(EXIT_FAILURE);
  55. }
  56. print_shmidds(&shminfo2);
  57. // 测试shmctl
  58. // IPC_INFO
  59. printf("IPC_INFO:\n");
  60. struct shminfo shmmetainfo;
  61. if (shmctl(shmid, IPC_INFO, &shmmetainfo) == -1)
  62. { // 获取共享内存段信息
  63. perror("shmctl");
  64. exit(EXIT_FAILURE);
  65. }
  66. printf("shmmax: %lu\n", shmmetainfo.shmmax);
  67. printf("shmmin: %lu\n", shmmetainfo.shmmin);
  68. printf("shmmni: %lu\n", shmmetainfo.shmmni);
  69. printf("shmseg: %lu\n", shmmetainfo.shmseg);
  70. printf("shmall: %lu\n", shmmetainfo.shmall);
  71. // SHM_INFO
  72. printf("\n");
  73. printf("SHM_INFO:\n");
  74. struct shm_info shm_info;
  75. if (shmctl(shmid, SHM_INFO, &shm_info) == -1)
  76. { // 获取共享内存段信息
  77. perror("shmctl");
  78. exit(EXIT_FAILURE);
  79. }
  80. printf("__used_ids: %lu\n", shm_info.__used_ids);
  81. printf("shm_tot: %lu\n", shm_info.shm_tot);
  82. printf("shm_rss: %lu\n", shm_info.shm_rss);
  83. printf("shm_swp: %lu\n", shm_info.shm_swp);
  84. printf("__swap_attempts: %lu\n", shm_info.__swap_attempts);
  85. printf("__swap_successes: %lu\n", shm_info.__swap_successes);
  86. // SHM_STAT
  87. printf("\n");
  88. printf("SHM_STAT:\n");
  89. struct shmid_ds shminfo0;
  90. if (shmctl(shmid, SHM_STAT, &shminfo0) == -1)
  91. { // 获取共享内存段信息
  92. perror("shmctl");
  93. exit(EXIT_FAILURE);
  94. }
  95. print_shmidds(&shminfo0);
  96. // SHM_STAT_ANY
  97. printf("SHM_STAT_ANY:\n");
  98. struct shmid_ds shminfo1;
  99. if (shmctl(shmid, SHM_STAT_ANY, &shminfo1) == -1)
  100. { // 获取共享内存段信息
  101. perror("shmctl");
  102. exit(EXIT_FAILURE);
  103. }
  104. print_shmidds(&shminfo1);
  105. // IPC_SET
  106. printf("\n");
  107. printf("IPC_SET:\n");
  108. struct shmid_ds shminfo;
  109. shminfo.shm_atime = 1;
  110. shminfo.shm_dtime = 2;
  111. shminfo.shm_ctime = 3;
  112. shminfo.shm_segsz = 4;
  113. shminfo.shm_cpid = 5;
  114. shminfo.shm_lpid = 6;
  115. shminfo.shm_nattch = 7;
  116. if (shmctl(shmid, IPC_SET, &shminfo) == -1)
  117. { // 获取共享内存段信息
  118. perror("shmctl");
  119. exit(EXIT_FAILURE);
  120. }
  121. // IPC_RMID
  122. printf("\n");
  123. printf("IPC_RMID:\n");
  124. if (shmctl(shmid, IPC_RMID, NULL) == -1)
  125. { // 获取共享内存段信息
  126. perror("shmctl");
  127. exit(EXIT_FAILURE);
  128. }
  129. // SHM_LOCK
  130. printf("\n");
  131. printf("SHM_LOCK:\n");
  132. if (shmctl(shmid, SHM_LOCK, NULL) == -1)
  133. { // 获取共享内存段信息
  134. perror("shmctl");
  135. exit(EXIT_FAILURE);
  136. }
  137. // SHM_UNLOCK
  138. printf("\n");
  139. printf("SHM_UNLOCK:\n");
  140. if (shmctl(shmid, SHM_UNLOCK, NULL) == -1)
  141. { // 获取共享内存段信息
  142. perror("shmctl");
  143. exit(EXIT_FAILURE);
  144. }
  145. }