Lecture 2 - 23SU-Advanced Databases
Lecture 2 - 23SU-Advanced Databases
CPSC8735-04-23SP
LECTURE 2
THEME 1 – VIRTUALIZING CPU
Chapter 4 - Process
Chapter 5 - Process API
Chapter 6 – Direct Execution (notes in
separate file)
ILLUSION OF MANY CPUS
• You run a web browser, mail, game, music player … you never
consider about whether a CPU is available, just simply start
running a program.
• Most modern OS use time-sharing technique to divide CPU
among the processes.
• Mechanism: low-level machinery implementation, such as
Context Switch, which provides OS the capability to divide
CPU time among the processes.
• Policy: high-level intelligence, such as CPU Scheduling, which
decides when to switch to run the next process. Such policy is
developed from 1) historic data; 2) workload info; and 3)
performance metrics.
• Separation of policy and mechanism, for easy replacement and
modularity design
• Space-sharing: counterpart of time-sharing, such as memory and
disk sharing among the processes or programs
PROCESS
Local variable
Long-lived data: new Object()
Complex data structure linked-list, hashmap, object instances M E M O RY LAYO UT
O F A P RO CESS
Low address
HARDWARE SPECIAL
Registers are used to store instruction and REGISTERS OF A PROCESS
data during the process execution, as
identified in Von Neumann model
DrawSquare(){
• Program counter, etc. …
flag = DrawLine(parm1, parm2)
Registered used to manage the function …
call stack of process during the execution: return r;
}
• Stack pointer – points to the memory
location where the next value will be
pushed onto the stack or the last value
that was popped off the stack; the value
keep changing during the execution of
the process for function calls.
• Frame(Base) pointer - points to the
base or start of the stack frame for the
current function; remains fixed
throughout the execution of a function;
a fixed reference point for accessing
local variables and arguments and other
stack information.
I /O INFO R M AT IO N
FO R A P RO CESS
Machine State –What parts of machine are important to a process? what a process
can read and write? All information are maintained in its machine state.The machine
state of a process includes:
Ø Memory (address space) containing process instruction and data (static and
dynamic)
Ø Call stack – data structure holding the subroutine of a program
Ø Registers – to manage the context of a process during its execution in processor
and memory PC
Ø I/O relation - Open files, network connections (state of process)
LIFE CYCLE OF A PROCESS
Process API
• Create(): double click a program or execute a command in CLI
• Destroy(): exit vs. kill
• Wait()
• Miscellaneous Control commands: suspend, resume
• Status
PROCESS
STATE
DATA S T RU C T U R E
O F A P RO C E S S
• Simulator
• Chapter 4 (first 5 questions)
• Process-run.py
PROCESS
HIERARCHY
TREE
HOW TO
LAUNCH A
NEW PROCESS
fork() is to create a duplicate copy of a process.The copy
becomes a child process (clone() in Linux).
Return value of fork():
• Negative: fork() failed
• 0: the child process see ‘0’
• Non-0: the PID of child process seen by the parent
process
FORK(): COPY-ON-WRITE(COW)
Child First or Parent First? Policy determines.
Sometimes the parent runs first, and sometimes the child runs first. The sequence is not
deterministic (racing condition).
Unless specifically designed, most of times the child runs slower than the parent, because
allocating address space and initialization and copy for the child process takes time.
FORK()
WAIT()
WHY WAIT()?
• When a process invokes fork(), a child process is created, with its own life cycle and running independently.
o If the parent process terminates before the child, the child becomes orphaned.
o If the child terminates before the parent but the parent forgets to remove it from PCB, the child process becomes zombie
(status of ‘Z’ shown when using the command ‘ps’).
• With wait(), the parent process is suspended (changed to block state) until the child process is completed or
signaled to stop/resume.
o After the child process complete, the parent will receive PID of the terminated child from wait().This allows the parent process
identify a completed child process and remove it to avoid a zombie.
o When parent invokes wait() and child already terminates, wait() returns -1 (indicating error).
In summary, wait() helps the parent process to know when child completes and check the return status of child process
(avoid Zombie child is important).
In child
process
EXE C( )
WHAT EXEC() DOES?
• Such separation allows shell to code after the call of fork() but before the call of exec(), thus, to alter the
environment of the about-to-be-run program.
• The child process can run a different program without parent passing a lot of its properties.
• PS
• Top: dynamic real-time view of the running system
• Kill/killall
MenuMeters – a set of monitoring tools to monitor CPU, memory, disk,
network for Mac
LAB OR HOMEWORK
• Simulator
• Chapter 5 (1st 5 questions)
• fork.py
• (ignore the coding part)
REFERENCE