linux 进程地址空间 之 mm_count vs mm_users

原创 2012年03月27日 14:58:23

首先引用 ULK上面的解释 :

The mm_users field stores the number of lightweight processes that share the mm_struct data structure (see the section "The clone( ), fork( ), and vfork( ) System Calls" in Chapter 3). The mm_count field is the main usage counter of the memory descriptor; all "users" in mm_users count as one unit in mm_count. Every time the mm_count field is decreased, the kernel checks whether it becomes zero; if so, the memory descriptor is deallocated because it is no longer in use.

We'll try to explain the difference between the use of mm_users and mm_count with an example. Consider a memory descriptor shared by two lightweight processes. Normally, its mm_users field stores the value 2, while its mm_count field stores the value 1 (both owner processes count as one).

If the memory descriptor is temporarily lent to a kernel thread (see the next section), the kernel increases the mm_count field. In this way, even if both lightweight processes die and the mm_users field becomes zero, the memory descriptor is not released until the kernel thread finishes using it because the mm_count field remains greater than zero.

If the kernel wants to be sure that the memory descriptor is not released in the middle of a lengthy operation, it might increase the mm_users field instead of mm_count (this is what the try_to_unuse( ) function does; see the section "Activating and Deactivating a Swap Area" in Chapter 17). The final result is the same because the increment of mm_users ensures that mm_count does not become zero even if all lightweight processes that own the memory descriptor die.

The mm_alloc( ) function is invoked to get a new memory descriptor. Because these descriptors are stored in a slab allocator cache, mm_alloc( ) calls kmem_cache_alloc( ), initializes the new memory descriptor, and sets the mm_count and mm_users field to 1.

Conversely, the mmput( ) function decreases the mm_users field of a memory descriptor. If that field becomes 0, the function releases the Local Descriptor Table, the memory region descriptors (see later in this chapter), and the Page Tables referenced by the memory descriptor, and then invokes mmdrop( ). The latter function decreases mm_count and, if it becomes zero, releases the mm_struct data structure.

 

简而言之呢,就是

mm_count 代表了对 mm 本身的引用,而 mm_users 代表对 mm 相关资源的引用,分了两个层次。mm_count类似于 以进程为单位。  mm_users类似于以线程为单位。

内核线程在运行时会借用其他进程的mm_struct,这样的线程叫"anonymous users",
因为他们不关心mm_struct指向的用户空间,也不会去访问这个用户空间.他们只是临时借用.
mm_count记录这样的线程.

mm_users是对mm_struct所指向的用户空间进行共享的所有进程的计数.
也就是说,会有多个进程共享同一个用户空间.

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Linux进程地址管理之mm_struct

Linux对于内存的管理涉及到非常多的方面,这篇文章首先从对进程虚拟地址空间的管理说起。(所依据的代码是2.6.32.60) 无论是内核线程还是用户进程,对于内核来说,无非都是 task_str...

Linux内核剖析 之 进程地址空间(二)

//接前一章,本节主要介绍线性区以及相关线性区的操作。 线性区 Linux通过类型为vm_area_struct的对象实现线性区。 vm_area_struct: struct vm_area_st...

linux进程地址空间(3) 内存映射(2)malloc与do_brk

下面再看函数do_brk,事实上do_brk和do_mmap几乎一样,因为它们的本质都是一样的;这里多说一下它由mm/mmap.c文件的系统调用SYSCALL_DEFINE1调用,系统调用的问题如前几...

linux进程地址空间(2) 缺页异常详解(3)写时复制COW详解

接上一篇 现在分析写时复制COW,对于写时复制,首先把握一点就是只有写操作时才有可能触发写时复制,所以首先总要判断异常flag是否含有标志FAULT_FLAG_WRITE,然后判断二级页表条目值是否...

Linux进程地址空间详解

之前写的一篇文章《a.out分段及运行时内存结构》简要介绍了Linux下的可执行文件格式和运行时的内存布局,这篇文章将更为详细得讨论Linux下进程的虚拟地址空间的布局。如下所述的内容都是基于32位系...

【图解】Linux下C程序进程地址空间布局

作者:沧海猎人   出处:http://blog.csdn.net/embedded_hunter  转载请注明出处   嵌入式技术交流QQ群:179012822 我们在学习C程序开发时经常...

Linux进程的虚拟地址空间

1.以32位系统为例,Linux系统中每个进程共有3GB的用户地址空间,当用户调用系统调用时,内核线程会代表进程运行,此时是在内核空间内运行的,故所有进程共享1GB的内核空间. 所以,总的来说,每个进...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)