Processes and the Kernel
Go to previous page
Processes
- Processes never execute codes, only threadexecute the code
- Processes has at least one thread.
- Execution environment created by OS for the program to run in; A structure that contains every thing that a program needs to run
- Every process has its own process space, has own array of threads
- each tag in chrome is a process --> has its own address space
- Lots of processes running will slow down the server
- Using Top to check the current state of the system
- Using PID (Process Identification) --> every process has its own unique PID
- OS needs to keep track of every process that in the system --> maintain, search, edit, etc
- fork
- Create a new process that is identical clone of the caller (parent)
- Although it has its own address space and own thread array, the program and the address space
is an identical clone copy of the parent --> the heap, stack, the global, the code, the program counter, the thread, the registers are the same
- Process considers the relationship --> with fork, we need to maintain the inside process structure and our relationship to children
- The caller fork is parent and new process is child --> needs to maintain this relationship
- Program counter is the same, so after executing, it means when they return from executing they both execute the same instruction
-> both of children and parents return from fork
- The return value for parent and children are different:
- For the fork return, parent return the PID of the child
- Child fork returns 0
- each process has its own PID --> they do not share process space --> but the content
of that address space are the same.
- _exit
- It terminates the calling process
- Keep track of why the process is terminated -> leave a message (integer --> exit status) --> why the process died
- waitpid
- Let a process wait for another to terminate and retrieve its exist status code
- the current process will be blocked until the waited process terminates --> after wake up --> waitpid retrive the reason(message) the process die
- process only allow to wait on its children --> only care about its children
- One process death does not affect its child process
- execv
- changes the program that a process is running --> does not create a new program
- keep PID, process structure, child-parent relationship same
- address space and thread changes
- execv takes two params
- the second of params is a pointer to array of arguments
- if execv does work, it does not return
- take the old address space and create a new address space --> cannot return old address space
- return if fail --> try to load program --> fail
- wrong program
- not enough memory for creating address space
- not enough space for creating threads
System Calls
- The calls that programmer use to interact with kernel.
- Everything is done by kernel. (open files, print, all the function calls)
- Make a system call (system functions) use the system calls library (it is NOT part of
kernel) -> ask kernel to do -> return to the user program
- User privileged VS kernel privileged
- User program --> unpriviledged code
- Kernel priviledged --> priviledged code
- Kernel Privilege (high-level privilege)
- Execution privilege controled by CPU
- User program has least privilege --> cannot execute/assess only permitted privileged code --> throw exception
- User program cannot directly call kernel functions or access kernel data structure
- The only way to run kernel code is by interruptions(hardware) and exceptions(software)
- To perform a system call, the application needs to cause an exception to make the system call
- li v0 0 --> v0 put the syscall code
- The kernel checks v0 to determine which system call has been requested --> using kernel ABI (Application Binary Interface)
- ABI all the information to share with user (size of Int, type, syscall code)
- Interrupts
- Interrupts are raised by hardward
- CPU receive the interrupts --> find the kernel(by using the stored address of interrupt handler) -->
CPU execuate the interrupt handler --> flip to the kernel privilege mode --> interrupt handler save the
stage, trap frame --> kernel do things
- Exceptions
- CPU raise the exceptions --> flip privilege --> call the predetermined location --> exception handler
- exception handler is part of the kernel
- MIPs treat exceptions and interrupts are the same
- System calls take parameters and return values
- The application places parameter value in kernel-specified location before the syscall,
and looks for return value in that location after the exception handler returns
- How do the application know the kernel-specified location?
- The locations are part of the kernel ABI
- Parameter and return value placement is handled by the application system call library functions.
- On MIPS, parameters go in registers a0,a1,a2,a3
- result success/fail code is in a3 on return
- return value or error code is in v0 on return
- lw a0 ... a3
- syscall --> raise exception
- If a3 return success, then v0 has return value
- If a3 return fail, then v0 has error code(from AVI)
- System Call Software Stack
- Application calls library wrapper function for desired system call
- Library function performs syscall instruction
- Kernel exception handler runs
- create trap frame to save program state
- determine this is a syscall exception
- determine which system call is being requested
- call the kernel implementation of it
- set the return value
- flip the priviledge back to kernel
- return from the exception
- library wrapper function finishes and returns from its call
- application continues execution
- System call is expensive!!!
- User program is totally isolated from implementation of Kernel
- Example on System call
- printf
- malloc --> depends, if there is no enough memory space, malloc needs to ask OS for more memory
- User and Kernel Stacks
- Every OS process thread has two stacks, although it only uses one at a time
- User Stack: while application code is executing
- located in the application's virtual memory (address space)
- Only go
- Kernel Stack: used while the thread is executing kernel code, after an exception or interrupt
- Lives in the kernel
- Stack holds the trap frames and switch frames (because kernel creates those two frames)
- In order to avoid user stack to modify kernel stack, so when flip to kernel mode --> everything(kernel's) flip to the kernel stack
- Kernerl stack can overflow the user stack, so we keep they are separated.
Multiprocessing
- Multiple processes existing at the same time
- All process share the availabe hardware resources.
- Thread execute the program instead of process --> we still have context switch, thread yield, thread_switch
- The OS ensures that processes are isolated from one another.
- kernel library --> v0 put the system call code, in a0 - a3 put the parameter to tell which system call --> raise the exception
--> switch priviledge mode and disable interruption --> execute the common exception handler --> switch the user stack to the kernel stack--
--> common exception handler save the trap frame on the kernel stack --> common exception called mips trap --> mis trap figure out which exception is it -->
turn the interruption on --> called that specific exception handler (syscall --> system call dispatcher) --> inside dispatcher (get v0 a0-a3) --> fork (e.g.) --> call sysfork
--> return a3(succeed, fail) v0 (succeed code, error code) --> system call return to mips trap --> return common exception --> restore the trap frame to the CPU and flip back user stack -->
switch to unpriviledge to priviledge mode --> user program running
Back To Top