4. The Abstraction: The ProcessOperating System: Three Easy Pieces
AOS@UC 1
How to provide the illusion of many CPUs?
p CPU virtualizing
w The OS can promote the illusion that many virtual CPUs exist.
w Time sharing: Running one process, then stopping it and running another
¢ The potential cost is performance.
AOS@UC 2
A Process
p Comprising of a process:
w Memory (address space)
¢ Instructions
¢ Data section
w Registers (processor architectural state (?) )
¢ Program counter
¢ Stack pointer
¢ …
AOS@UC 3
A process is a running program.
Process API
p These APIs are available on any modern OS.
w Create
¢ Create a new process to run a program
w Destroy
¢ Halt a runaway process
w Wait
¢ Wait for a process to stop running
w Miscellaneous Control
¢ Some kind of method to suspend a process and then resume it
w Status
¢ Get some status info about a process
w …
AOS@UC 4
Process Creation
1. Load a program code into memory, into the address space of the
process.
w Programs initially reside on disk in executable format (code + static data).
w OS perform the loading process lazily.
¢ Loading pieces of code or data only as they are needed during program
execution.
2. The program’s run-time stack is allocated.
w Use the stack for local variables, function parameters, and return address.
w Initialize the stack with arguments à argc and the argv array of main()
function
AOS@UC 5
Process Creation (Cont.)
3. The program’s heap is created.
w Used for explicitly requested dynamically allocated data.
w Program request such space by calling malloc() and free it by calling
free().
4. The OS do some other initialization tasks.
w input/output (I/O) setup
¢ Each process by default has three open file descriptors.
¢ Standard input, output and error
5. Start the program running at the entry point, namely main().
w The OS transfers control of the CPU to the newly-created process.
AOS@UC 6
Loading: From Program To Process
AOS@UC 7
codestatic data
heap
stack
Process
Memory
codestatic data
heap
Program
Disk
Loading:Takes on-disk programand reads it into the
address space of process
CPU
Process States
p A process can be one of three states.
w Running
¢ A process is running on a processor.
w Ready
¢ A process is ready to run but for some reason the OS has chosen not to run it
at this given moment.
w Blocked
¢ A process has performed some kind of operation.
¢ When a process initiates an I/O request to a disk, it becomes blocked and thus
some other process can use the processor.
AOS@UC 8
Process State Transition
AOS@UC 9
Running Ready
Blocked
Descheduled
Scheduled
I/O: doneI/O: initiate
Data structures
p The OS has some key data structures that track various relevant pieces
of information.
w Process list
¢ Ready processes
¢ Blocked processes
¢ Current running process
w Register context
p PCB(Process Control Block)
w A C-structure that contains information about each process.
AOS@UC 10
Example) The xv6 kernel Proc Structure
AOS@UC 11
// the registers xv6 will save and restore// to stop and subsequently restart a processstruct context {
int eip; // Index pointer registerint esp; // Stack pointer registerint ebx; // Called the base registerint ecx; // Called the counter registerint edx; // Called the data registerint esi; // Source index registerint edi; // Destination index registerint ebp; // Stack base pointer register
};
// the different states a process can be inenum proc_state { UNUSED, EMBRYO, SLEEPING,
RUNNABLE, RUNNING, ZOMBIE };
Example) The xv6 kernel Proc Structure (Cont.)
AOS@UC 12
// the information xv6 tracks about each process// including its register context and statestruct proc {
char *mem; // Start of process memoryuint sz; // Size of process memorychar *kstack; // Bottom of kernel stack
// for this processenum proc_state state; // Process stateint pid; // Process IDstruct proc *parent; // Parent processvoid *chan; // If non-zero, sleeping on chanint killed; // If non-zero, have been killedstruct file *ofile[NOFILE]; // Open filesstruct inode *cwd; // Current directorystruct context context; // Switch here to run processstruct trapframe *tf; // Trap frame for the
// current interrupt};
p Disclaimer: This lecture slide set is used in AOS course in University of Cantabria. Was initially
developed for Operating System course in Computer Science Dept. at Hanyang University. This
lecture slide set is for OSTEP book written by Remzi and Andrea Arpaci-Dusseau (at
University of Wisconsin)
AOS@UC 13