Chinaunix首页 | 论坛 | 博客
  • 博客访问: 558965
  • 博文数量: 142
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1452
  • 用 户 组: 普通用户
  • 注册时间: 2013-09-12 16:28
文章分类

全部博文(142)

文章存档

2016年(10)

2015年(60)

2014年(72)

我的朋友

分类: LINUX

2015-11-09 17:17:13


点击(此处)折叠或打开

  1. gwwu@hz-dev2.aerohive.com:~/test/sharememory>more mmap_anon.c
  2. #include <unistd.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <sys/mman.h>
  6. #include <semaphore.h>
  7. int main(int argc, char *argv[])
  8. {
  9.     sem_t mutex;
  10.     int i,nloop;
  11.     int *ptr;
  12.    
  13.     if(argc != 2) {
  14.         printf("format: a.out \n");
  15.         exit(0);
  16.     }
  17.     nloop = atoi(argv[1]);
  18.     printf("nloop = %d\n",nloop);
  19.     ptr = mmap(NULL,sizeof(int),PROT_READ|PROT_WRITE,MAP_SHARED|MAP_ANONYMOUS,-1,0);
  20.     sem_init(&mutex,1,1);

  21.     if(fork() == 0) {
  22.         for(i = 0; i < nloop; i++) {
  23.             sem_wait(&mutex);
  24.             printf("child: %d\n",(*ptr)++);
  25.             sem_post(&mutex);
  26.         }
  27.   // exit(0);
  28.     }
  29.     
  30.     printf("pid is %d,ppid is %d\n",getpid(),getppid());
  31.     for(i = 0;i < nloop; i++) {
  32.         sem_wait(&mutex);
  33.         printf("parent: %d\n",(*ptr)++);
  34.         sem_post(&mutex);
  35.     }
  36. // exit(0);
  37.     sleep(100);
  38.     exit(0);
  39. }

编译运行:

点击(此处)折叠或打开

  1. gwwu@hz-dev2.aerohive.com:~/test/sharememory>gcc -g mmap_anon.c -o mmap_anon -Wall -lpthread
  2. gwwu@hz-dev2.aerohive.com:~/test/sharememory>./mmap_anon 10
  3. nloop = 10
  4. pid is 16598,ppid is 16270
  5. parent: 0
  6. parent: 1
  7. parent: 2
  8. parent: 3
  9. parent: 4
  10. parent: 5
  11. parent: 6
  12. parent: 7
  13. parent: 8
  14. parent: 9
  15. child: 10
  16. child: 11
  17. child: 12
  18. child: 13
  19. child: 14
  20. child: 15
  21. child: 16
  22. child: 17
  23. child: 18
  24. child: 19
  25. pid is 16599,ppid is 16598
  26. parent: 20
  27. parent: 21
  28. parent: 22
  29. parent: 23
  30. parent: 24
  31. parent: 25
  32. parent: 26
  33. parent: 27
  34. parent: 28
  35. parent: 29
  36. ^Z------------------------为了查看进程,CTRL+Z暂停进程
  37. [1]+ Stopped ./mmap_anon 10 
  38. gwwu@hz-dev2.aerohive.com:~/test/sharememory>cd /proc/16598/
  39. gwwu@hz-dev2.aerohive.com:/proc/16598>more maps --------------查看父进程的maps
  40. 00400000-00401000 r-xp 00000000 fd:02 35897850 /home/gwwu/test/sharememory/mmap_anon
  41. 00600000-00601000 rw-p 00000000 fd:02 35897850 /home/gwwu/test/sharememory/mmap_anon
  42. 362da00000-362da20000 r-xp 00000000 fd:00 1966082 /lib64/ld-2.12.so
  43. 362dc1f000-362dc20000 r--p 0001f000 fd:00 1966082 /lib64/ld-2.12.so
  44. 362dc20000-362dc21000 rw-p 00020000 fd:00 1966082 /lib64/ld-2.12.so
  45. 362dc21000-362dc22000 rw-p 00000000 00:00 0
  46. 362de00000-362df89000 r-xp 00000000 fd:00 1966086 /lib64/libc-2.12.so
  47. 362df89000-362e189000 ---p 00189000 fd:00 1966086 /lib64/libc-2.12.so
  48. 362e189000-362e18d000 r--p 00189000 fd:00 1966086 /lib64/libc-2.12.so
  49. 362e18d000-362e18e000 rw-p 0018d000 fd:00 1966086 /lib64/libc-2.12.so
  50. 362e18e000-362e193000 rw-p 00000000 00:00 0
  51. 362e600000-362e617000 r-xp 00000000 fd:00 1966094 /lib64/libpthread-2.12.so
  52. 362e617000-362e817000 ---p 00017000 fd:00 1966094 /lib64/libpthread-2.12.so
  53. 362e817000-362e818000 r--p 00017000 fd:00 1966094 /lib64/libpthread-2.12.so
  54. 362e818000-362e819000 rw-p 00018000 fd:00 1966094 /lib64/libpthread-2.12.so
  55. 362e819000-362e81d000 rw-p 00000000 00:00 0
  56. 7fda79e20000-7fda79e23000 rw-p 00000000 00:00 0
  57. 7fda79e2f000-7fda79e30000 rw-s 00000000 00:04 13146523 /dev/zero (deleted)  ------这个就是匿名mmap的内存映射
  58. 7fda79e30000-7fda79e32000 rw-p 00000000 00:00 0
  59. 7fffb6887000-7fffb689c000 rw-p 00000000 00:00 0 [stack]
  60. 7fffb69ff000-7fffb6a00000 r-xp 00000000 00:00 0 [vdso]
  61. ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
  62. gwwu@hz-dev2.aerohive.com:/proc/16598>
  63. gwwu@hz-dev2.aerohive.com:/proc/16598>
  64. gwwu@hz-dev2.aerohive.com:/proc/16598>
  65. gwwu@hz-dev2.aerohive.com:/proc/16598>cd ../16599
  66. gwwu@hz-dev2.aerohive.com:/proc/16599>more maps----------查看子进程的maps
  67. 00400000-00401000 r-xp 00000000 fd:02 35897850 /home/gwwu/test/sharememory/mmap_anon
  68. 00600000-00601000 rw-p 00000000 fd:02 35897850 /home/gwwu/test/sharememory/mmap_anon
  69. 362da00000-362da20000 r-xp 00000000 fd:00 1966082 /lib64/ld-2.12.so
  70. 362dc1f000-362dc20000 r--p 0001f000 fd:00 1966082 /lib64/ld-2.12.so
  71. 362dc20000-362dc21000 rw-p 00020000 fd:00 1966082 /lib64/ld-2.12.so
  72. 362dc21000-362dc22000 rw-p 00000000 00:00 0
  73. 362de00000-362df89000 r-xp 00000000 fd:00 1966086 /lib64/libc-2.12.so
  74. 362df89000-362e189000 ---p 00189000 fd:00 1966086 /lib64/libc-2.12.so
  75. 362e189000-362e18d000 r--p 00189000 fd:00 1966086 /lib64/libc-2.12.so
  76. 362e18d000-362e18e000 rw-p 0018d000 fd:00 1966086 /lib64/libc-2.12.so
  77. 362e18e000-362e193000 rw-p 00000000 00:00 0
  78. 362e600000-362e617000 r-xp 00000000 fd:00 1966094 /lib64/libpthread-2.12.so
  79. 362e617000-362e817000 ---p 00017000 fd:00 1966094 /lib64/libpthread-2.12.so
  80. 362e817000-362e818000 r--p 00017000 fd:00 1966094 /lib64/libpthread-2.12.so
  81. 362e818000-362e819000 rw-p 00018000 fd:00 1966094 /lib64/libpthread-2.12.so
  82. 362e819000-362e81d000 rw-p 00000000 00:00 0
  83. 7fda79e20000-7fda79e23000 rw-p 00000000 00:00 0
  84. 7fda79e2f000-7fda79e30000 rw-s 00000000 00:04 13146523 /dev/zero (deleted)-----这个就是匿名mmap的内存映射
  85. 7fda79e30000-7fda79e32000 rw-p 00000000 00:00 0
  86. 7fffb6887000-7fffb689c000 rw-p 00000000 00:00 0 [stack]
  87. 7fffb69ff000-7fffb6a00000 r-xp 00000000 00:00 0 [vdso]
  88. ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0

阅读(971) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~