Processes
Most basic function of kernel is to run programs
How do we achieve that with just 1 (or small number) cpu and memory?
Want to create illusion that there are many cpus; 1 for every program
A process is a program in execution
- A program is static; a process is the activity of executing that program
Processes have states that change over time
Processes need resources (CPU, memory, I/O, ...) to actually execute
Processes have contexts - all of the machine and kernel-related states
A context is comprised of CPU context (values of registers, stack pointer, program counter, ...) and memory context (code, variables, things in memory, ...)
A process has 3 memory areas: stack, text, data
Text contains all of the code
Data contains global variables, heap
Stack contains activation records
An activation record is information pertaining to a procedure call
- If a procedure calls itself 3 times, there will be 3 activation records
Activation records store:
- pointer where to return to
- link to previous record (so we know where the new top of the stack is after this is popped off the stack)
- local variables
Stack pointer register contains the location of the top of the stack
Return address keeps track of where to return to after procedure
All this is for 1 process; but we want to run many processes
In reality, if only 1 cpu, only 1 process could be running
Multiprogramming: voluntarily giving up CPU to another process
A process running may need a resource (keystroke, anything needed to get work done). If it doesn't get that resource, the process is just sitting there doing nothing. It can't make any progress, so let's give CPU to another process that can
yield(p) - give CPU to process p
Context switching: allocating of CPU from one process to another
- Save the context of one process, restore context of process we want to run
Switch text, data, and stack because each process has its own text, data, and stack
Yield is so important, we don't want it written by programmers. We want it written by OS implementer and put in the kernel
Kernel is code that executes. Is it a process then? No it is not!
- Kernel supports processes; can be thought of as an extension of all processes. Processes run inside the kernel.
Kernel has its own text and data. It has 1 stack per process
Yield is in the kernel, so calling yield causes a jump into the kernel
Yield is a system call - a function in the kernel that is callable by a process