CS3103 Operating System
2025/2026 Sem B
Chapter 2: Process
Department of Computer Science
City University of Hong Kong
1
Process and Thread
• Process: • Thread:
– a program in execution – A thread is the unit of
execution within a process
2
Process
正在載入⋯
3
Process
• Process – a program in execution
• A process includes
– program counter
– stack
– data section
– …
Memory layout of a process
4
Process State
• As a process executes, it changes state
– new: The process is being created
– ready: The process is waiting to be assigned to CPU
– running: Instructions are being executed
– 正在載入⋯
waiting: The process is waiting for some event to occur
– terminated: The process has finished execution
5
Process Control Block (PCB)
• Each process is represented in OS by a Process Control Block
(PCB) z kind of data structure
– Also called a task control block
process
.
• Information recorded in OS for eachn process
– Process ID (PID) Rumning,
, Ready
New ,
– Process state 2 tta
Waiting....
– Program counter โ
– Register values
– CPU scheduling information (e.g., priority)
– List of open files
– I/O status information
– …… 6
Process Scheduling
• Several queues are involved in the
scheduling of processes
– Job queue – set of all processes in the system
– Ready queue – set of all processes residing in
main memory, ready and waiting to execute
– Device queues – set of processes waiting for an
I/O device
• Processes migrate among these queues
7
Process Scheduling
8
vereut y
Scheduler _
~ using
• Long-term scheduler (or job scheduler) – selects which _ ⼀
processes should be brought into the ready queue
7 □
m I
☆
– Invoked infrequently (seconds, minutes)
Ci
– To arrange the execution of some job at some time
↓
– Controls the degree of multiprogramming
>
ready queue
• Short-term scheduler (or CPU scheduler) – selects whichshntten t _
Run af
process should be executed next and allocates CPU the same time
"
– Invoked frequently (milliseconds) = long select start
>
->
, > run
-
.
– To create concurrency; switching must be sufficiently fast
ー
of to handle multiple
- the
ability a
system tasks
seemingly at once.
9
CPU-bound vs I/O-bound
• Processes can be classified into
– CPU-bound process: spends more time doing computations
• Few long CPU bursts
– I/O-bound process: spends more time doing I/O than computations
• Many short CPU bursts
CPU-bound process
I/O-bound process
10
Context Switch
• When CPU switches to another process,
the system must save the state of the
old process and load the saved state for
the new process
正在載入⋯
– Context-switch time is overhead; does no
useful work while switching
– Time dependent on hardware support
11
Context Switch
• A context switch scenario example
the
bermal
for using
l -
switch
to
>
-
01 on
ehedwier
processed
d
the , taste and
determed
be
the 12
start
Process Tree
at have
only schedule"
the
• Unix Example:
grocess create
may
> one
-
proces process we
new the
a
not
but designedkonlel
met
Crew , Provide
lodi
editand
µ
13
Process Creation
• Child may duplicate parent’s memory int main()
{
space, or load new memory contents Pid_t pid;
(in particularly, load a new program) /* fork another process */
pid = fork();
• UNIX examples if (pid < 0) { /* error occurred
*/
– fork system calls creates new process fprintf(stderr, "Fork
Failed");
• The return value of fork(): -
return value exit(-1);
– In the parent process: the process ID (PID) of }
the newly created child process else if (pid == 0) { /* child
process */
– In the child process: 0 execlp("/bin/ls", "ls",
– exec system calls used after a fork to NULL);
}
replace the process’ memory space with else { /* parent process */
a new program and execute /* parent wait for the child
to complete */
wait (NULL);
printf ("Child Complete"); 14
p Aauarent diff
!ameexwrl□
forks ) duplicated to do task with
"
y
parent.
littl lictl <
_
and child do
< >
-
parent can
↓
!
at the time
do
forkl ) thing same
do n independently
246
↓
.
otme
←
thiags
m
do wanit " ↓
Besides :
exit(0);
31296/431495
Process Creation
• Parent process creates children processes
CIC .
-
·ส stop
the parentlet it wait
and
.
&
>
-
∞
Result
=
o
→
when the child
back to
∞
finish
child PID
exer
15
Process Creation
• Steps:
1. Load a program code into memory.
• Programs initially reside on disk in executable format.
• OS perform the loading process lazily.
– Loading pieces of code or data only as they are needed
during program execution.
2. The program’s run-time stack is allocated.
• Use the stack for local variables, function parameters,
and return address.
• Initialize the stack with arguments argc and the
argv array of main() function
16
Process Creation
1. The program’s heap is created.
• For explicitly requested dynamically allocated data.
• Program request such space by calling malloc()
and free it by calling free().
2. The OS does some other initialization tasks.
• E.g., input/output (I/O) setup
– Each process by default has three open file descriptors.
– Standard input, output and error
3. Start the program running at the entry point,
namely main().
• The OS may transfer control of the CPU to the
newly-created process.
17
Example: Process on Android
• By default, every app runs in its own Linux process
– When launching Twitter, there is one process
– When playing AngryBird, there is one process
• Some application may use several processes
– When using Facebook, there are four processes
18
Process Termination
• Process executes last statement and asks the OS to delete it (exit)
– Output data from child to parent (wait)
– Process’ resources are de-allocated by OS
• Parent may terminate execution of children processes (abort)
– Happens when
• Child has exceeded allocated resources
• Task assigned to child is no longer required
• If parent is exiting
– Some OS does not allow child to continue if its parent terminates
» All children terminated - cascading termination
19
When error occured -
easy to know where
goes .
wrong
、 Processes Cooperation
V
• Independent process cannot affect or be affected by the needed
execution of another process
is
LICD
• Cooperating process can affect or be affected by the execution
of another process
• Advantages of process cooperation
– Information sharing
– Computation speed-up
– Modularity C breaking down complex
– … task into smaller processes
20
Producer-Consumer Problem
• Producer produces information that is consumed by Consumer
– unbounded-buffer places no practical limit on the size of the buffer
– bounded-buffer assumes that there is a fixed buffer size
↑ mechanism is
needed
nunnef
buffer
tmotki"n re m
型
21
processes
have diff storage space- > won't share variable
Interprocess Communication (IPC)
ร
>
-
need to
share via send/receive
• Mechanism for processes to communicate and synchronize message
• Message system – processes communicate with each other
without resorting to shared variables
• IPC facilities provide two operations
– send(message)
message size fixed or variable
– receive(message)
• If P and Q wish to communicate, they need to
– establish a communication link between them
– exchange messages via send/receive
22
Implementation Issues
• How are links established?
• Can a link be associated with more than two processes?
• How many links can there be between every pair of
communicating processes?
• What is the capacity of a link?
• Is the size of a message that the link can accommodate fixed or
variable?
• Is a link unidirectional or bi-directional?
•…
23
Communication Models
(a) Message passing (b) Shared memory
24
Communication Models
Message Passing Model Shared Memory Model
Message passing facility is used for communication. Shared memory region is used for communication.
It can be used in a distributed environment where It is used for communication between processes on a
communicating processes reside on remote machines single processor or multiprocessor systems where the
connected through a network. communicating processes reside on the same machine.
OS provides mechanism for communication and
Need the programmer to write programs to explicitly
synchronization of actions performed by the
coordinate the access of data by different processes.
communicating processes.
It provides maximum speed of computation as
It is time consuming as message passing is implemented
communication is done through shared memory so system
through kernel intervention (system calls).
calls are made only to establish the shared memory.
25
Direct Communication
• Processes must name each other explicitly:
– send(P, message) – send a message to process P
– receive(Q, message) – receive a message from process Q
• Properties of communication link
– Links are established automatically
– A link is associated with exactly one pair of communicating processes
– Between each pair there exists exactly one link
– The link may be unidirectional, but is usually bi-directional
26
Indirect Communication
• Messages are directed and received from mailboxes (also
referred to as ports)
– Each mailbox has a unique id
– Processes can communicate only if they share a mailbox
• Properties of communication link
– Link established only if processes share a common mailbox
– A link may be associated with many processes
– Each pair of processes may share several communication links
– Link may be unidirectional or bi-directional
27
Indirect Communication
• Mailbox operations
– create a new mailbox
– send and receive messages through mailbox
– destroy a mailbox
• Primitives are defined as:
send(A, message) – send a message to mailbox A
receive(A, message) – receive a message from mailbox A
28
Indirect Communication
• Mailbox sharing
– P1, P2, and P3 share mailbox A
– P1, sends; P2 and P3 receive
– Who gets the message?
• Possible choices: 正在載入⋯
– Allow a link to be associated with at most two processes
– Allow only one process at a time to execute a receive operation
– Allow the system to select arbitrarily the receiver. The sender is
notified of who the receiver was
29
Synchronization
• Message passing may be either blocking or non-blocking
– Blocking is considered synchronous
• Blocking send has the sender block until the message is received
• Blocking receive has the receiver block until a message is available
– Non-blocking is considered asynchronous
• Non-blocking send has the sender send the message and continue
• Non-blocking receive has the receiver receive a valid message or null
30