Information Technology Reference
In-Depth Information
backhanded. The kernel allocates and initializes the process control block, al-
locates memory for the process, copies the program from disk into the newly
allocated memory, and allocates both a user-level stack for normal execution
and a kernel-level stack for handling system calls, interrupts and exceptions.
To start the program running, we need two additional steps:
Copy arguments into user memory. When starting a program, the
user sometimes gives it arguments, much like calling a procedure. For
example, when you click on a file icon in MacOS or Windows, the window
manager asks the kernel to start the application associated with the file,
passing it the file name to open. The system call to create a process then
copies the file name from the memory of the window manager process to a
special region of memory in the new process. By convention, arguments to
a process are typically copied to the base of the user-level stack, and the
user's stack pointer is incremented so those addresses are not overwritten
when the program starts running.
Transfer control to user-mode. When we start a new process, it
never trapped into the kernel to save its current state. While we could
special-case the code for starting a new process, most operating systems
re-use the same code to exit the kernel for starting a new process and for
returning from a system call. When we create the new process, we allocate
it a kernel stack, and we reserve room at the bottom of the kernel stack
for the initial values of its registers, program counter, stack pointer, and
processor status word. To start the new program, we can then switch to
the new stack and jump to the end of the interrupt handler. When the
handler executes popad and iret , the processor \returns" to the start of
the user program.
Finally, although you can think of a user program as starting with a call
to main , in fact the compiler inserts one level of indirection. It generates a
stub, at the location within the process's memory where the kernel will jump
on process start. The stub's job is to call main and then if main returns, calls
exit . Without this, a user program that returned from main would try to pop
the return program counter from main, and since there wasn't any such address
on the stack, the processor would start executing random code. Exit is a system
call that terminates the process.
2.3.6
Upcalls
We can use system calls for most of the communication between applications
and the operating system kernel. When a program needs to request some pro-
tected operation, it can trap to ask the kernel to perform the operation on its
behalf. Likewise, if there is data inside the kernel that the application needs,
the program can simply perform a system call to retrieve it.
Search WWH ::




Custom Search