[OSTEP] 2. The Process
ch3~4: 프로세스의 구성 요소 / 상태 / 구현
Table Of Contents
CH4: The Abstraction: The Process
Virtualizing the CPU: Time sharing
- technique running one process, then stopping it and running another, and so forth.
- allows users to run many concurrent processes
- to implement this, the OS will need
- low-level machinery: mechanisms
- i.e., context switch
- high-level intelligence
- low-level machinery: mechanisms
1. The Abstraction: A Process
process
- simply, a running program
- constituted by:
- Memory
- instructions lie in memory
- data that running program reads and writes sits in memory
- thus memory that the process can address (address space) is part of process
- Registers
- program counter (PC) or instruction pointer (IP)
- tells us which instruction of the program will execute next
- stack pointer
- manage the stack for function parameters, local variables, and return addresses
- program counter (PC) or instruction pointer (IP)
- Memory
2. Process API
-
must be included in OS.
-
are available on any modern OS
-
Create
- create new process
-
Destroy
- destroy processes forcefully
-
Wait
- stop running
-
Miscellaneous Control
- suspend a process and then resume it
-
Status
- get some status information about a process
- i.e., how long it has run for, what state it is in...
3. Process Creation
how programs are transformed into processes?
- load its code and any static data into memory, into the address space of the process.
- programs initially reside on disk in some kind of executable format
- the OS read those bytes from disk and place them in memory somewhere
- In early (or simple) OS : loading process is done eagerly
- i.e., all at once before running the program
- In modefn OS: perform the process lazily
- i.e., by loading pieces of code of data only as they are needed during program execution
- to unserstand this, concept of paging and swapping is needed
- allocate the program's run-time stack (or just stack)
- the OS allocates memory for local variables, function parameters, and return addresses
- the OS initialize the stack with arguments:
argc
and theargv
array ofmain()
function
- allocate memory for the program's heap
- heap is used for explicitly requested dynamically-allocated data
- requested by
malloc()
, freed byfree()
- linked lists, hash tables, trees, etc.
- requested by
- heap is used for explicitly requested dynamically-allocated data
- other initialization tasks
- I/O setup
- each process by default has three open file descriptors(standard input, output, error)
- I/O setup
- start the program running at the entry point(
main()
)
4. Process States
a process can be in one of three states:
- Running
- process is running on a processor.
- processor is executing instructions.
- Ready
- process is ready to run but for some reason, the OS has chosen not to run it at this given moment.
- Blocked
- process has performed some kind of operation that makes it not ready to run until some other event takes place.
- i.e., I/O request to a disk
-
Being moved from ready to running : process has been scheduled
-
Being moved from running to ready : process has been descheduled
-
initial: Sometimes a system will have an initial state that the process is in when it is being created
-
final: a process could be placed in a final state where it has exited but has not yet been cleaned up
- (in UNIX-based systems, this is called the zombie state
Example
Time | Process 0 | Process 1 | Notes |
---|---|---|---|
1 | Running | Ready | |
2 | Running | Ready | |
3 | Running | Ready | |
4 | Running | Ready | Process 0 now done |
5 | - | Running | |
6 | - | Running | |
7 | - | Running | |
8 | - | Running | Process 1 now done |
- Figure 4.3: Tracing Process State: CPU Only
Time | Process 0 | Process 1 | Notes |
---|---|---|---|
1 | Running | Ready | |
2 | Running | Ready | |
3 | Running | Ready | Process 0 initiates I/O |
4 | Blocked | Running | Process 0 is blocked, |
5 | Blocked | Running | so Process1 runs |
6 | Blocked | Running | |
7 | Ready | Running | I/O done |
8 | Ready | Running | Process 1 now done |
9 | Running | - | |
10 | Running | - | Process 0 now done |
- Figure 4.4: Tracing Process State: CPU and I/O
- Process 0 issues an I/O. At that point, process 0 is blocked, giving proceses 1 a chance to run.
5. Data Structures
The OS is a program. Like any program, the OS has some key data structures
- process list
- to track the state of each process
- currently running process
- processes in ready
- blocked processes
- in xv6 kernel
// the registers xv6 will save and restore // to stop and subsequently restart a process struct context { int eip; int esp; int ebx; int ecx; int edx; int esi; int edi; int ebp; }; // the different states a process can be in enum proc_state { UNUSED, EMBRYO, SLEEPING, RUNNABLE, RUNNING, ZOMBIE }; // the information xv6 tracks about each process // including its register context and state struct proc { char* mem; // Start of process memory uint sz; // Size of process memory char* kstack; // Bottom of kernel stack // for this process enum proc_state state; // Process state int pid; // Process ID struct proc* parent; // Parent process void* chan; // If !zero, sleeping on chan int killed; // If !zero, has been killed struct file* ofile[NOFILE]; // Open files struct inode* cwd; // Current directory struct context context; // Switch here to run process struct trapframe* tf; // Trap frame for the // current interrupt };
- register context
- for a stopped process
- registers of stopped process will be saved to this memory location
- to track the state of each process