L2 - Process Abstraction
L2 - Process Abstraction
Process Abstraction
Lecture 2
Overview
n Introduction to Process Management
n Process Abstraction:
q Memory Context
n Code & Data
n Function call
n Dynamically allocated memory
q Hardware Context
q OS Context
n Process State
q Process Control Block and Process Table
n Observation:
q If there is only one program executing at any
point in time, how can we utilize hardware
resources effectively?
n Solution:
q Allow multiple programs to share the hardware
n e.g. Multiprogramming, Time-sharing
Introduction to Process Management
n As the OS, to be able to switch from running
program A to program B requires:
1. Information regarding the execution of program
A needs to be stored
2. Program A's information is replaced with the
information required to run program B
n Hence, we need:
q An abstraction to describe a running program
q aka process
Key Topics
Process Abstraction
Process Scheduling
Alternative to Process
Memory Hardware OS
Context Context Context
• Code • Registers • Process
• Data • PC Properties
• ... •… • Resources
used
• ...
Recap: C Sample Program and Assembly Code
int i = 0;
i = i + 20;
C Code Fragment
4096 Data
(for global variables)
The entire
memory
space
"Free" Memory
Recap: Generic Computer Organization
Instruction Cache
Fetch Unit
… … …
Dispatch Unit
Memory
Data Cache
INT FP …… MEM
Registers
Functional Units
n Cache:
q Duplicate part of the memory for faster access
q Usually split into instruction cache and data cache
n Fetch unit:
q Loads instruction from memory
q Location indicated by a special register: Program
Counter (PC)
Recap: Component Description (cont)
n Functional units:
q Carry out the instruction execution
q Dedicated to different instruction type
n Registers:
q Internal storage for the fastest access speed
q General Purpose Register (GPR):
n Accessible by user program (i.e. visible to compiler)
q Special Register:
n Program Counter (PC)
n etc
Recap: Basic Instruction Execution
n Instruction X is fetched
q Memory location indicated by Program Counter
n Instruction X dispatched to the corresponding
Functional Unit
q Read operands if applicable
n Usually from memory or GPR
q Result computed
q Write value if applicable
n Usually to memory or GPR
n Instruction X is completed
q PC updated for the next instruction
Recap: What you should know J
n An executable (binary) consists of two major
components:
q Instructions and Data
n Consider:
q How do we allocate memory space for variables
i, j and a?
n Can we just make use of the "data" memory space?
Data
(for global variables)
The entire
memory
space Stack growth
direction
Stack
Pointer
Stack
(for function invocations)
void g()
{
h();
...
} Stack Frame
void h() for f()
{ …
... …
}
Illustration: Stack Memory Usage (2 / 5)
void f()
{
...
g();
...
}
void g()
{ At this Stack Frame
h(); point
for g()
...
} Stack Frame
void h() for f()
{ …
... …
}
Illustration: Stack Memory Usage (3 / 5)
void f()
{
...
g();
...
} Stack Frame
for h()
void g()
{ Stack Frame
h(); for g()
...
} Stack Frame
void h() for f()
{ At this
point
…
... …
}
Illustration: Stack Memory Usage (4 / 5)
void f()
{
...
g();
...
}
void g()
{ Stack Frame
h(); for g()
... At this
} point
Stack Frame
void h() for f()
{ …
... …
}
Illustration: Stack Memory Usage (5 / 5)
void f()
{
...
g();
... At this
point
}
void g()
{
h();
...
} Stack Frame
void h() for f()
{ …
... …
}
Illustration: Stack Frame v1.0
Free
Memory
Stack
Pointer
Local Variables
Parameters
Return PC Stack Frame
Stack
for g()
Other info
Stack Frame
After function f() calls g()
for f()
Topmost stack frame
… belongs to g()
…
Function Call Convention
n Different ways to setup stack frame:
q Known as function call convention
q Main differences:
n What information is stored in stack frame or registers?
n Which portion of stack frame is prepared by caller /
callee?
n Which portion of stack fame is cleared by caller / callee?
n Who between caller / callee to adjust the stack pointer?
n No universal way
q Hardware and programming language dependent
Saved FP
Return PC
Stack Frame
After function f() calls g()
for f()
Topmost stack frame
… belongs to g()
…
Stack Frame Setup / Teardown [Updated]
n On executing function call:
q Caller: Pass arguments with registers and/or stack
q Caller: Save Return PC on stack
q Transfer control from caller to callee
q Callee: Save registers used by callee. Save old FP, SP
q Callee: Allocate space for local variables of callee on stack
q Callee: Adjust SP to point to new stack top
n Examples:
q In C, the malloc() function call
q In C++, the new keyword
q In Java, the new keyword
n Question:
q Can we use the existing "Data" or "Stack" memory
regions?
Dynamically Allocated Memory
n Observations:
q These memory blocks have different behaviors:
1. Allocated only at runtime, i.e. size is not known
during compilation time è Cannot place in Data
region
2. No definite deallocation timing, e.g. can be
explicitly freed by programmer in C/C++, can be
implicitly freed by garbage collector in Java è
Cannot place in Stack region
n Solution:
q Setup a separate heap memory region
Illustration for Heap Memory
Text
(for instructions)
Data
(for global variables)
The entire
memory Heap
space (for dynamic allocation)
Stack
(for function invocations)
Managing Heap Memory
n Heap memory is a lot trickier to manage due
to its nature:
q Variable size
q Variable allocation / deallocation timing
n You can easily construct a scenario where
heap memory are allocated /deallocated in
such a way to create "holes" in the memory
q Free memory block squeezed in between of
occupied memory block
n We will learn more in the memory
management (much) later in the course
Checkpoint: Contexts updated
n Information describing a process:
q Memory context:
n Text, Data, Stack and Heap
q Hardware context:
n General purpose registers, Program Counter, Stack
pointer, Stack frame pointer, ….
OS Context
Process Id & Process State
Ready Running
Ready State: Running State:
process waiting to run process is executing
Context
switch
New Terminated
admit
switch: scheduled exit
create
Ready Running
switch:
release CPU
Blocked
release cpu
event wait
blocked
...
queue
Notes:
•More than 1 process can be in ready + blocked queues
•May have separate event queues
•Queuing model gives global view of the processes, i.e. how the OS views them
Checkpoint: Contexts updated
n When a program is under execution, there
are more information:
q Memory context:
n Text and Data, Stack and Heap
q Hardware context:
n General purpose registers, Program Counter, Stack
pointer, Stack frame pointer, …
q OS context:
n Process ID, Process State, …
Process Table &
Process Control Block
Putting it together
Process Control Block & Table
n The entire execution context for a process
q Traditionally called Process Control Block (PCB) or
Process Table Entry
Interesting Issues:
n Scalability
q How many concurrent processes can you have?
n Efficiency
q Should provide efficient access with minimum space
wastage
Illustration of a Process Table
PC, FP,
SP, ……
GPRs Text
PCB1 Memory
Region
Data
PCB2 Info
PID
PCB3
Process Heap
……… State
Process
Process
Control Block
Table
Stack
Memory Space
of a Process
Process interaction with OS
System Calls
int main()
{ Library call that
int pid; has the same
name as a
/* get Process ID */ system call
pid = getpid();
return 0;
} Library call that
make a system
n System Calls invoked in this example:
call
q getpid()
{ SystemCallHandlerXXX()
... {
getpid(); Perform the task 5
... }
}
User Mode Kernel Mode
Process interaction with OS
Exception and Interrupt
Ops!
Exception
n Executing a machine level instruction can cause
exception
n For example:
q Arithmetic Errors
n Overflow, Underflow, Division by Zero
q Memory Accessing Errors
n Illegal memory address, Mis-aligned memory access
q Etc
n Exception is Synchronous
q occur due to program execution
n Effect of exception:
q Have to execute a exception handler
q Similar to a forced function call
Interrupt
n External events can interrupt the execution of
a program
n Usually hardware related, e.g.:
q Timer, Mouse Movement, Keyboard Pressed etc
n Interrupt is asynchronous
q Events that occurs independent of program
execution
n Effect of interrupt:
q Program execution is suspended
q Have to execute an interrupt handler
Exception/Interrupt Handler: Illustration
void f()
1. Exception/Interrupt
{ occurs:
... n Control transfer to a
Statement S1; handler routine
... 1
automatically
}
n OS ç è Process interactions
q System calls
q Exception / Interrupt
References
n Modern Operating System (3rd Edition)
q Section 2.1