0% found this document useful (0 votes)
23 views

Lecture 2 - 23SU-Advanced Databases

This document discusses processes and virtualization of the CPU. It covers: 1) How operating systems use time-sharing and context switching to provide each process the illusion of having dedicated CPU time, even though the CPU is shared between processes. 2) The definition of a process as an actively running program instance, distinguished from a program which sits inactive on disk. 3) The key components of a process including its address space, call stack, registers, I/O descriptors, and life cycle stages from creation to termination. 4) Data structures like the process list and process control block that the OS uses to manage information about each process.

Uploaded by

Cheserem Titus
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Lecture 2 - 23SU-Advanced Databases

This document discusses processes and virtualization of the CPU. It covers: 1) How operating systems use time-sharing and context switching to provide each process the illusion of having dedicated CPU time, even though the CPU is shared between processes. 2) The definition of a process as an actively running program instance, distinguished from a program which sits inactive on disk. 3) The key components of a process including its address space, call stack, registers, I/O descriptors, and life cycle stages from creation to termination. 4) Data structures like the process list and process control block that the OS uses to manage information about each process.

Uploaded by

Cheserem Titus
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

ADVANCED OPERATING SYSTEM

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

• A process is one of the most fundamental abstraction that OS


provides to the user.
• A process is a program in running.
• A process is different from a program, because
o A program is lifeless
o A program sits in disk, with code and static data
o A process is an actively running entity
o A process is an instance of a program.
PROCESS
AND
PROGRAM
PREPARE A
PROGRAM TO AN
EXECUTABLE analyze the entire source code, perform
(OVERVIEW) syntax and semantic checks, optimize the
code, and generate an object code
When a compiler generates
• Compiler – convert a high- machine code, it may create
level programming source file intermediate object files that
to object file containing contain assembly code, which in
machine-readable instruction further are converted to the final
machine code by assembler.
• Assembler – a component
of compiler to help convert
the source code into low-
level machine code Resolve references between different
object files, such as function calls and
• Linker – combine multiple variable definitions to ensure they are
object files generated by well linked into an executable file.
compiler and/or assembler It also performs address resolution and
into an executable file. assigns memory addresses to symbols,
enabling the program to be loaded and
executed.
PROGRAM
RELOCATABLE
OBJECT FILE

• Compile source code to


relocatable object file
Unix> gcc –o main.c
Unix> gcc –o swap.c

• Relocatable: the object file


contains relative memory
addresses for text and data,
which allows the file to be
loaded and execute at
different memory locations
without requiring
modification of the original
machine code.
LINKING

The linker resolves the relative


addresses contained in the object
file to the correct memory
addresses. The linker performs
the relocation process, adjusting
the relative addresses in the
object file to match the final
memory layout.

Static linking vs. dynamic linking


EXE CUTA BLE FILE

Relocating text and data


LOADING

When you double-click a program or use command line to


execute a program, how does OS transform an executable
program into a process?
• The loader creates a process for the executable program,
loads it to memory (RAM) by copying the text and data of the
executable file, or more precisely, to the address space of the
process, and pass the address of entry point of the process to
CPU (into PC register). CPU starts executing the first
instruction of the executable.This procedure is called loading.
• a loader is a program in operating system that is responsible
for loading portable executables (PE file) and libraries (DLL
file).
High address

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

• A process may conduct I/O


operation such as
read/write file and network
communication. Each
opened I/O resource
returns a “descriptor”, such
as file descriptor, socket
descriptor, pipe descriptor,
etc.These I/O descriptors
are maintained by the OS
for a process.
PROCESS CONSTITUTION

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

A process can be in different


state at a given moment:
• Running: a process is in the
middle of execution.
• Ready: a process is ready to
run but OS has not yet
schedule it to run for some
(Sleeping) reason.
• Block: a process is
performing other operation
thus is not running in CPU
until it completes the other
operation, such as I/O.
PROCESS STATE:
CODE EXAMPLE

Process1 is scheduled to run in CPU after process0 completes.


PROCESS
STATE – CODE
EXAMPLE

Process0 is running in CPU until it initiates an I/O operation, which


makes it blocked from being executed in CPU. Process1 is then
scheduled to run in CPU until it completes. Process0 is then scheduled
to re-run in CPU, although it has competed I/O before process1
completes (2 decisions being made here à policy plays the role).
DATA
ST RUCT UR ES
D E FINE D FO R A
P RO CESS

Process List: OS tracks all processes


in a list, together with the state of each
process.
Process Control Block (PCB): each
entry in the process list is a PCB.
Register Context: In context switch,
OS saves the contents of the physical
registers of the stopping process into
the memory location, and copy them
back later when resuming the process
for running.
Zombie State – a process exits but
not yet being cleaned up
XV6 - version 6 UNIX
Different OS has different structure to track the info of a process, but
essentially, they are alike. Below table provide a different perspective.

DATA S T RU C T U R E
O F A P RO C E S S

Process Control Block (PCB)


LAB OR HOMEWORK

• 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(): CODE EXAMPLE


FORK(): PARENT VS. CHILD

• Same program (exact copy of text)


• The contents of address space such as parameter and data for the child initially is copied from
the parent, thus the child process is the same as parent in terms of data.
• The child share the memory address space of the parent until the child writes in the address
space of parent, when the address space (page) is duplicated for the child (copy-on-write).
• Entry point different: the child does not start running at main() like the parent does (it starts
where fork() takes place)
• Return value different: fork() returns 0 in the child process itself, while returns a PID of child
process in the parent process.
• Variable changed by the child is not transparent to the parent process (because of copy-on-
write).
• The running sequence of parent and child is not deterministic.
FORK(): PROCESS LAYOUT FOR PARENT
AND CHILD
child parent child parent

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?

• What OS does when exec() is called in a child process?


1. OS load a new executable program (designated in exec) and its data from disk to memory.
2. OS overwrite the child process by reinitializing its stack, heap and other parts of memory.
3. OS pass the argument to argv[] and start executing the new program from its entry point (vs.
fork() starting from the next line immediately after fork()).
• OS does not create a new process for Exec(). So PID stay unchanged from the child process.
• Replaces current process image with the new program image.
• Once successfully executed, the exec() does not “return” (unless there is exception)
- All I/O descriptors (file, socket, pipe) opened before exec() stay opened after exec()
• This property is very useful for implementing filters (piping).
WHY FORK() BUT NOT DIRECTLY
EXEC()?

• 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.

The shell does below behind the scene:


1. fork() a child process
2. Close the standard output (on screen)
3. Open file ‘newfile.txt’
4. execute ‘wc’
Through above procedure it creates the environment for ‘wc’ thus redirect the
output of execution to a file (instead of on the screen).
Similar to Unix system call pipe ()
EXIT()

• A process terminates its execution by making an exit() system call.


• When a child process terminates, either normally by calling exit(), or
abnormally due to a fatal exception or signal, an exit status is returned to the
operating system and a (SIGCHLD) signal is sent to the parent process. The
exit status can then be retrieved by the parent process via the wait().
• exit() system call is not always explicit in a program. A process can also
terminate and return if control reaches the end of the function.
• exit() frees all the resources held by a process(except for PCB itself).
Short research: fork() vs. vfork() vs. exec() vs. clone()

PROCESS: SYSTEM CALLS


BRAIN
TICKER

How many processes will be generated by above code block?


OTHER USEFUL SYSTEM CALL ABOUT A
PROCESS

• 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

• 1. Operating System: Three Easy Pieces by Remzi H. Arpaci-Dusseau and


Andrea C. Arpaci-Desseau
• 2. Nesco Academy at https://nesoacademy.org/
• 3. Dr. Hui Liu at Bing Hampton Univ.at
https://www.cs.binghamton.edu/~huilu/cs350/

You might also like