[ProgClub list] Memory management in C

John Elliot jj5 at progclub.org
Sat Oct 1 20:37:32 UTC 2011


On 2/10/2011 7:06 AM, Asher Glynn wrote:
>> I'm reading a book about the design of the UNIX operating system and it
>> talks about how code executes in 'user mode' or in 'kernel mode' but there
>> is no context switch that takes place. So kernel mode code just runs in the
>> context of whichever process invoked it.
>
> Wha?

I'm right about this one man. There is no context switch to another 
process for a syscall. The syscall gets processed in kernel mode in the 
context of the process that invoked it.

> Check http://en.wikipedia.org/wiki/System_call - Typical implementations

Yep, I might spend some time fixing up that article, because there is 
nothing that really draws out the distinction between kernel/user mode 
and how processes work.

Here, let me quote at length from page 15 of The Design of the UNIX 
Operating System by Maurice J. Bach:

   The execution of user processes on UNIX systems is divided into two 
levels: user and kernel. When a process executes a system call, the 
'execution mode' of the process changes from 'user mode' to 'kernel 
mode': the operating system executes and attempts to service the user 
request, returning an error code if it fails. Even if the user makes no 
explicit requests for operating system services, the operating system 
still does bookkeeping operations that relate to the user process, 
handling interrupts, scheduling processes, managing memory, and so on. 
Many machine architectures (and their operating systems) support more 
levels than the two outlined here, but the two modes, user and kernel, 
are sufficient for UNIX systems.
   The differences between the two modes are:
   * Processes in user mode can access their own instructions and data 
but not kernel instructions and data (or those of other processes). 
Processes in kernel mode, however, can access kernel and user addresses. 
For example, the virtual address space of a process may be divided 
between addresses that are accessible only in kernel mode and addresses 
that are accessible in either mode.
   * Some machine instructions are privileged and result in an error 
when executed in user mode. For example, a machine may contain an 
instruction that manipulates the processor status register; processes 
executing in user mode should not have this capability.
   Put simply, the hardware views the world in terms of kernel mode and 
user mode and does not distinguish among the many users executing 
programs in those modes. The operating system keeps internal records to 
distinguish the many processes executing on the system.


More information about the list mailing list