关于malloc的睡眠有关问题系列之一

关于malloc的睡眠问题系列之一
引:有说法,
1 .malloc会睡眠 ,kmalloc使用参数gfp_kernel时睡眠 vmalloc可睡眠
问 为什么会睡眠?

2. 用户态的内存申请函数 不能睡眠
与核心内存申请函数 不能睡眠 是同一个原因吗?


类似问题:
http://topic.csdn.net/u/20120928/14/2ab50def-c7d5-4ec0-958e-c43ffd88d8d1.html?seed=1653068410&r=79847412#r_79847412

------------鸟文查 man malloc
Unlike its standard C library counterpart (malloc(3)), the kernel version takes two more arguments. The flags argument further qualifies malloc ’s operational characteristics as follows:
M_ZERO
  Causes the allocated memory to be set to all zeros.
M_NOWAIT
  Causes malloc, realloc, and reallocf to return NULL if the request cannot be immediately fulfilled due to resource shortage. Note that M_NOWAIT is required when running in an interrupt context.

M_WAITOK
  Indicates that it is OK to wait for resources. If the request cannot be immediately fulfilled, the current process is put to sleep to wait for resources to be released by other processes. The malloc, realloc, and reallocf functions cannot return NULL if M_WAITOK is specified.

M_USE_RESERVE
  Indicates that the system can dig into its reserve in order to obtain the requested memory. This option used to be called M_KERNEL but has been renamed to something more obvious. This option has been deprecated and is slowly being removed from the kernel, and so should not be used with any new programming.
Exactly one of either M_WAITOK or M_NOWAIT must be specified.

-------------
malloc, realloc and reallocf may sleep when called with M_WAITOK. free never sleeps.

--------------以上英文链接

http://www.gsp.com/cgi-bin/man.cgi?section=9&topic=malloc

-------------------现在小结:就是由于用户态的malloc使用了M_WAITOK ,所以导致睡眠。//

下一步,一直想找到malloc源码级别的解释睡眠的原因,现在还没找到,希望大家提供线索。


------解决方案--------------------
你说的所谓睡眠,是不是指“调度”?

在你访问全局资源的时候,多个进程间存在冲突,这时需要让后来的进程挂起,等待资源被释放。

肯定会有调度,除非在内核程序的关键部分,这部分因为中断级别高,比调度器的中断级别还高,所以,肯定不允许调度,此时也不用保护,因为当前CPU时间片不能被打断。理论上是这样,但还要考虑多核问题,所以也需要自旋锁作为同步机制。

------解决方案--------------------
这个不是标准的malloc,你按照他的man描述用多线程代码测试下吧,帮助理解。
------解决方案--------------------
英文解释了,因为资源不足,挂起,直到请求被满足