02_brk/sbrk

Posted liujun5319

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了02_brk/sbrk相关的知识,希望对你有一定的参考价值。

  1 #include<stdio.h>
  2 #include<unistd.h>
  3 main()
  4 {
  5     //situation 1
  6     //int *p = sbrk(4); // Here we allocate 1024(decides to the system) bytes of memory and move(not allocate) 4 bytes forward and then return.
  7     //*p = 8888;
  8     //printf("%d ",*p);
  9
 10
 11     //situation 2
 12     //int *p = sbrk(0);//Segmentation fault(core dumped)
 13     //*p = 8888;
 14     //printf("%d ",*p);
 15
 16
 17     //situation 3
 18     //int *p = sbrk(0);
 19     //brk(p+1);
 20     //*p = 8888;
 21     //printf("%d ",*p);
 22
 23     //situation 4
 24     //int *p = sbrk(4);
 25     //*(p+20) = 100;// You will not get a core dumped here ,but this use is illegal and you may get an unpredictable error.
 26
 27     //situation 5
 28     //int *p = sbrk(0);
 29     //brk(p+4); // Here we
 30     //printf("%d ",*(p+20)); //0
 31     //*(p+20) = 100;
 32     //printf("%d ",*(p+20)); //100
 33     //*(p+1023) = 100;
 34     //*(p+1024) = 100; You will get a core dumped
 35
 36     /*
 37     int *p1 = sbrk(4); //Allocate and move
 38     int *p2 = sbrk(4); //move
 39     int *p3 = sbrk(4); //move
 40     int *p4 = sbrk(4); //move
 41     int *p5 = sbrk(4); //move
 42     int *p6 = sbrk(4); //move
 43     int *p7 = sbrk(0); //move
 44     int *p8 = sbrk(0); //move
 45     brk(p8-1);
 46
 47     printf("%p ",p1);
 48     printf("%p ",p2);
 49     printf("%p ",p3);
 50     printf("%p ",p4);
 51     printf("%p ",p5);
 52     printf("%p ",p6);
 53     printf("%p ",p7);
 54     printf("%p ",p8);
 55
 56     *p6 = 8888;
 57     *p7 = 9999;
 58     *p8 = 10000;
 59     printf("%d,%d,%d ",*p6,*p7,*p8);*/
 60
 61
 62
 63     int *p = sbrk(0); //Allocate and return a point(the value is null)
 64     printf("%p ",p);
 65     brk(p+4);//don‘t move
 66     printf("%p ",p);
 67     brk(p+4);//don‘t move
 68     printf("%p ",p);
 69     brk(p+4);//don‘t move
 70     printf("%p ",p);
 71     brk(p);//don‘t move
 72     printf("%p ",p);
 73
 74
 75 }













































































以上是关于02_brk/sbrk的主要内容,如果未能解决你的问题,请参考以下文章

在malloc,为什么要使用brk?为什么不直接使用mmap?

高并发内存池详解(C/C++/Windows/Linux)

高并发内存池详解(C/C++/Windows/Linux)

项目管理文件夹模板

S-Docker_02_基本概念_02_容器

24c02程序 高手请进