2021-22 COMP3230A
What is a “process”?
How to represent a “process”?
Resources use by a process
Process states
Important data structures
Operations on processes
Principles of Operating Systems 2
ILO2a - explain how OS manages processes/threads and
discuss the mechanisms and policies in efficiently sharing
of CPU resources.
Principles of Operating Systems 3
Required Reading
Operating Systems: Three Easy Pieces by Arpaci-Dusseau et. al
Chapter 4, Abstraction: The Process
http://pages.cs.wisc.edu/~remzi/OSTEP/cpu-intro.pdf
Chapter 5, Interlude: Process API
http://pages.cs.wisc.edu/~remzi/OSTEP/cpu-api.pdf
Reference
Chapter 3 of Operating Systems, 3rd edition by Deitel et. al
Principles of Operating Systems 4
Program itself is a lifeless thing
What is a Process?
A process is a program in execution
An instance of a program running in a computer
A process is an entity that can be assigned to and executed on a CPU
A unit of activity characterized by
the execution of a sequence of instructions,
with an execution state, and
an associated set of system resources
Principles of Operating Systems 5
Torepresent a running program, the OS needs to keep
track of the following information:
Examples
The memory that the process Program code
can access (or reference) is Memory
Data
part of the process
Resources I/O in use
in use Physical memory
During execution, process
updates/ stores data in CPU Execution Register set
registers, e.g., program
state Current process
counter, stack pointer, etc. state
Principles of Operating Systems 6
The process’s view of its memory is called the address space, which is
a range of memory locations (or a range of memory addresses)
Process address space consisting of a few “regions”:
max
Stack Stores local variables,
arguments to function, return
value for active procedure calls
Where dynamically allocated
Heap
memory is placed
Stores global and
Data segment
static variables
Text segment
Stores the program code that
0 the processor executes 7
In the process execution life cycle, it moves through a series of
discrete process states.
Process state - an indicator of the nature of the current
activity of a process
new (initial): The process is just being created
running: The process is executing on a processor
blocked: The process is waiting for some event (e.g., I/O or
communication) to happen before it can proceed
ready: The process is ready to run on a processor and is waiting to be
assigned to a processor
terminated (final / zombie): The process has finished execution but has
not yet been cleaned up; why not just discard it?
Principles of Operating Systems 8
With terminated state, it When a process blocks waiting for
allows parent process to resources (e.g. I/O), it transitions from
examine the return code of running to blocked
the process and see if it
executed successfully
When a process is
dispatched to be OS decides to
executed by the CPU, it switch a
transitions from ready process from
to running running to
ready
When the waiting event occurred,
the blocked process transitions
Switch to the ready state after the process and its from blocked to ready
associated data structures have been allocated
9
To manage a process, OS makes use of a data structure to
maintain information about a process
Process Control Block (PCB) or Process Descriptor
PCB typically includes
Process identification number (PID) - a unique ID
Current process state
Program counter - indicates the address of next instruction
Register context - a snapshot of the register contents in which the
process was last running before it transitioned out of the running state
Scheduling information - process priority, pointers to scheduling queues,
etc.
Principles of Operating Systems 10
Credentials - determines the resources this process can access
Memory Management information - concerning memory areas
allocated to the process
Accounting information - CPU usage statistics, time limits, etc.
A pointer to the process’s parent process
Pointers to the process’s child processes
Pointers to allocated resources
Example: Linux process descriptor
: struct task_struct
in /usr/src/linux/include/linux/sched.h
at around 450 lines of statements
Principles of Operating Systems 11
Tomanage many OSkeeps pointers to each
processes, OS needs process’s PCB in a table
someway to quickly Example - Linux “process
access process’s PCB table” is organized in a form
of hashed table
When a process is
“completely” terminated,
OS removes the process
from the process table and
frees all of the process’s
resources
12
Current
per core
PCB
Ready
Blocked
OS maintains a ready
list and a blocked list
that store references
to processes not
currently running
13
You can list the processes’ details in Linux/Mac OS X by using ps
command
Usage: ps [option]
When executed without any options, only processes that are associated with
the current terminal are shown.
Use man-page to learn how to use
Some Useful options (Linux)
-e: Select all processes
-f: Show in full format
w: Wide output
f: ASCII-art process hierarchy (forest)
Principles of Operating Systems 14
A Linux system program similar to ps
Processes are organized in hierarchy
As every process has a parent process, their relationship can be viewed by using
pstree
Usage: pstree [option]
Use man-page to learn how to use
Example:
15
Operating systems provide fundamental services to processes
including:
Creating processes
Destroying processes
Suspending processes
Resuming processes
Changing process’s priority (for scheduling)
Waiting for a process (parent process waits for the child process)
Check process’s status
Interprocess communication (IPC)
:
Principles of Operating Systems 16
A process spawns a new process
The process that creates a new process is now called the parent
process
The newly created process is called the child process
It also can create other processes, thus forming a tree of processes
When the parent process is destroyed, modern operating
systems typically responds in this way
Keeps the child processes and allows them to proceed independently of
the terminated parent process
Principles of Operating Systems 17
Actions taken
Assign a unique process ID
Allocate memory for the process
Space for PCB must be allocated
Initialize the process control block
Save the process ID, parent ID
Set program counter and stack pointer to appropriate values
Set links so it is in the appropriate queue
Create other data structures
Memory, files, accounting
Set the process state to Ready and put it to the Ready queue
Principles of Operating Systems 18
Unix
fork() system function
A system call that creates a new process by duplicating the calling process
exec () family of functions
OS replaces the current program image with a new program image
Windows API
CreateProcess() function
Creates a new process and its primary thread and loads program for execution
http://msdn.microsoft.com/en-us/library/ms682512(VS.85).aspx
CreateProcess() similar to fork() + exec ()
Principles of Operating Systems 19
Process executes last statement and asks the operating system to
delete itself
exit() or return from main()
A process may terminate involuntarily
Parent may terminate execution of children processes (by sending a
termination signal)
A number of error and fault conditions can lead to termination of process
Return termination status from child to parent
Parent can obtain this info by calling wait(), waitid() or waitpid()
Process’ resources are de-allocated by OS afterward
Principles of Operating Systems 20
A process is in terminated state
When a process exits, OS still keeps the PCB of the process, so
that the parent can get the information later
exit status
resource usage
Before the parent process collects the information by calling
wait() or waitpid(), the deceased process is kept in the
Terminated state, and we called it as the “zombie” process
Principles of Operating Systems 21
Temporarily deactivate the process, such that it is not being
considered for processor scheduling
Why doing so:
Upon user request
Request by the parent process
OS may decide to suspend a blocked process so as to free up the memory for
another ready process
A suspended process must be resumed by another process
Difference between suspension and blocked
blocking is triggered by internal activity of the process, while suspend is
coming from external
Principles of Operating Systems 22
Principles of Operating Systems 23
Principles of Operating Systems 24
Signals are used in UNIX systems to notify a process that a
particular event has occurred
Sometimes being referred as “Software Interrupt”
Basically implemented as system calls (kill(), signal(),
sigaction(), raise(), pause(), sigsuspend(), etc.)
Each signal is represented by a value/symbolic name
SIGINT: value = 2, generated when Ctrl-c is pressed
SIGCHLD: value = 17, generated when child process finishes execution
or is terminated
Principles of Operating Systems 25
A signal is generated by one software entity (in the
occurrence of an event) to a target software entity
Synchronous signal – is triggered by the current instruction of the
current running process itself and is delivered to that process by
the OS immediately
e.g., illegal memory access, division by zero
interrupt
Process Illegal op invoke signal Process
X CPU OS X
Jump to Deliver
Memory OS the
Dereference Identify
protection Interrupt SIGSEGV
a NULL the cause
exception handling signal
pointer of
routine
exception 26
Asynchronous signals – are generated by external events /
activities, which are not triggered by the current activity / action
of the target process at the time of receiving the signal
i.e., arrive at unpredictable times during execution of the program
e.g., by the timer alarm
e.g., parent process using kill() system call to kill the child process
Process Process
X OS Y
signal
27
A process can decide whether it wants to catch, ignore or
mask a signal
Catching a signal involves specifying a routine (signal handler) in
advance so that the OS will invoke that handler when the process
receives that signal
the signal() or sigaction() system calls can be used by the program to specify the
signal handler routine to the OS
Catching – Using OS's default action to handle the signal
Ignore – Inform OS that it does not want to handle that signal
Masking a signal is to instruct the OS not to deliver signals of that type
until the process clears the signal mask
SIGKILL and SIGSTOP cannot be caught, blocked or ignored
Principles of Operating Systems 28
How OS determines what a process will respond to a
particular signal
A process's PCB contains a pointer to a vector of signal handlers
(logically order by the signal number)
Each entry corresponds to the handler function for that entry (signal)
A child process inherits the setting from its parent
However, if use exec…() function to load a new program
image, any signals that have the custom-made handlers will
be reset to default setting
Principles of Operating Systems 29
To manage and control a running process (application), OS
needs some mechanisms to keep track on the current status
of the process
Various data structures are needed
Process control block (PCB)
Process table and lists
OS provides a set of operations for us to work with processes
Signals were introduced in Unix systems to allow interactions
between User Mode processes; the kernel also uses them to
notify processes of system events.
Principles of Operating Systems 30