Skip to main content
COURSE NAME : OPERATING SYSTEM
COURSE CODE : 381CCS-3
CHAPTER 3 : PROCESSES, THREADS
REFERENCE DETAIL FOR CHAPTER 3
Topic Text Reference Chapter No. Page No
Processes ,
Threads
“Operating System
Concepts”, 10th Edition,
Abraham SilberSchatz,
Peter Baer Galvin,
Greg Gagne, Wiley, 2018
Chapter 3 105-122
Chapter 4
160-162
166-168
188-194
2
 Process
Concepts
 The Process
 Process
State
 PCB
 Threads
 Process
Scheduling
 Scheduling
Queues
 CPU
Scheduling
 Context
Switch
 Operating on
Process
 Process Creation
 Process
Termination
CHAPTER 3 : PROCESSES, THREADS
3.1 PROCESS CONCEPTS
3
• Program becomes process when an executable file is loaded into
memory
 Execution of program started via GUI mouse clicks, command line entry,
etc.
 An operating system executes a variety of programs that run as a process.
3.1.1 THE PROCESS
 Process – a program in execution; process execution must progress in
sequential fashion. No parallel execution of instructions of a single process
 Multiple parts
• The program code, also called text section
• Current activity including program counter, processor registers
• Stack containing temporary data
 Function parameters, return addresses, local variables
• Data section containing global variables
• Heap containing memory dynamically allocated during run time
Figure 3.1 Layout of
a process in memory
 Program is passive entity stored on disk (executable file); process is active
• Consider multiple users executing the same
3.1 PROCESS CONCEPTS(CONT…)
4
3.1.2 PROCESS STATE
 As a process executes, it changes state
• New: The process is being created
• Running: Instructions are being executed
• Waiting: The process is waiting for some event to occur
• Ready:The process is waiting to be assigned to a
processor
• Terminated: The process has finished execution
Figure 3.2 Diagram of process state
3.1 PROCESS CONCEPTS(CONT…)
5
3. PROCESS CONTROL BLOCK
Information associated with each process(also called task control
block)
 Process state – running, waiting, etc.
 Program counter – location of instruction to next execute
 CPU registers – contents of all process-centric registers
 CPU scheduling information- priorities, scheduling queue
pointers
 Memory-management information – memory allocated to the
process
 Accounting information – CPU used, clock time elapsed
since start, time limits
 I/O status information – I/O devices allocated to process,
list of open
files
3.1.4 THREADS
 So far, process has a single thread of execution
 Consider having multiple program counters per process
• Multiple locations can execute at once
 Multiple threads of control -> threads
 Must then have storage for thread details, multiple program counters
in PCB
Figure 3.3 Process
control block (PCB).
6
 Goal -- Maximize CPU use, quickly switch processes onto CPU core
 Process scheduler: selects among available processes for next execution on
CPU
 Degree of multiprogramming: The number of processes currently in memory
 An I/O-bound process : spends more of its time doing I/O than computations.
 A CPU-bound process, spends more of its time doing computations.
3.2.1 SCHEDULING QUEUES
 Maintains scheduling queues of processes
• Ready queue – set of all processes residing in main memory, ready
and waiting to execute
• Wait queues – set of processes waiting for an event (i.e., I/O)
• Processes migrate among the various queues
3.2 PROCESS SCHEDULING
Figure 3.4 The ready queue and wait queues.
3.2.1 SCHEDULING QUEUES(CONT…)
 Queueing Diagram: It is a common representation of process.
• The circles represent the resources that serve the queues,
• The arrows indicate the flow of processes in the system.
 A new process is initially put in the ready queue, It waits there until it is
selected for execution, or dispatched.
 Once the process is allocated a CPU core and is executing, one of several
events
could occur
 The process could issue an I/O request and then be placed in an I/O wait
queue.
 The process could create a new child process and then be placed in a wait
queue while it awaits the child’s termination.
 The process could be removed forcibly from the core, as a result of an
interrupt or
having its time slice expire, and be put back in the ready queue.
7
Figure 3.5 Queueing-diagram representation of process scheduling
8
3.2.2 CPU SCHEDULING
 The role of the CPU scheduler is to select from among the processes that are in
the ready queue and allocate a CPU core to one of them.
 The CPU scheduler must select a new process for the CPU frequently.
 Processes can be described as either:
• I/O-bound process – spends more time doing I/O than computations, many
short CPU bursts
• CPU-bound process – spends more time doing computations; few very long
CPU bursts
 Swapping : remove a process from memory (and from active contention for the
CPU) Later, the process can be reintroduced into memory, and its execution can
be continued where it left off.
 a process can be “swapped out” from memory to disk, where its current
status is saved, and later “swapped in” from disk back to memory, where
its status is restored.
 Swapping is typically only necessary when memory has been
overcommitted and must be freed up.
 reduce the degree of multiprogramming.
3.2.3 CONTEXT SWITCH
 A context switch occurs when the CPU switches from one process to another.
 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 via a context switch
 Context of a process represented in the PCB
 Context-switch time is pure overhead; the system does no useful work while
switching
• The more complex the OS and the PCB  the longer the context switch
 Time dependent on hardware support
• Some hardware provides multiple sets of registers per CPU  multiple
contexts
loaded at once
9
Figure 3.6 Diagram showing context switch from process to process.
10
3.3.1 PROCESS CREATION
 Parent process create children processes, which, in turn create other processes,
forming
a tree of processes. Process identified and managed via a process identifier (pid)
 Resource sharing options
• Parent and children share all resources
• Children share subset of parent’s
resources
• Parent and child share no resources
 Execution options
• Parent and children execute concurrently
• Parent waits until children terminate
 Address space
• Child duplicate of parent
• Child has a program loaded into it
 UNIX examples
• fork() system call creates new process
• exec() system call used after a fork() to replace the process memory
space with a new program
• Parent process calls wait()waiting for the child to terminate
3.3 OPERATION ON PROCESSES
Figure 3.7 Process
creation using the
fork() system call.
11
3.3.2 PROCESS DELETION
 Process executes last statement and then asks the operating system to delete
it
using the exit() system call.
• Returns status data from child to parent (via wait())
• Process’ resources are deallocated by operating system
 Parent may terminate the execution of children processes using the abort()
system call. Some reasons for doing so:
• Child has exceeded allocated resources
• Task assigned to child is no longer required
• The parent is exiting, and the operating systems does not allow a
child to continue if its parent terminates
 Some operating systems do not allow child to exists if its parent has
terminated. If a process terminates, then all its children must also be
terminated.
• cascading termination. All children, grandchildren, etc., are terminated.
• The termination is initiated by the operating system.
 The parent process may wait for termination of a child process by using the
wait()system call. The call returns status information and the pid of the
terminated process
pid = wait(&status);
 If no parent waiting (did not invoke wait()) process is a zombie
 If parent terminated without invoking wait(), process is an
orphan
3.3 OPERATION ON PROCESSES (CONT…)
12
 A thread is a basic unit of CPU utilization; it comprises a thread ID, a
program counter (PC), a register set, and a stack.
 It shares with other threads belonging to the same process its code
section, data section, and other operating-system resources, such as
open files and signals.
 A traditional process has a single thread of control.
 If a process has multiple threads of control, it can perform more than
one task at
a time.
3.4 THREAD
Figure 3.8 Single-threaded and multithreaded processes
13
MOTIVATION
 Most modern applications are multithreaded
 Threads run within application
 Multiple tasks with the application can be implemented by separate
threads
• Update display
• Fetch data
• Spell checking
• Answer a network request
 Process creation is heavy-weight
 Thread creation is light-weight
 Can simplify code, increase
efficiency
 Kernels are generally
multithreaded
BENEFITS
 Responsiveness – may allow continued execution if part of process is
blocked, especially important for user interfaces
 Resource Sharing – threads share resources of process, easier than
shared
memory or message passing
 Economy – cheaper than process creation, thread switching lower
overhead than context switching
 Scalability – process can take advantage of multicore architectures
3.4.1 OVERVIEW
Figure 3.9 Multithreaded
server
architecture
14
 User threads are supported above the kernel and are managed without
kernel support
 Three primary thread libraries:
• POSIX Pthreads
• Windows threads
• Java threads
 Kernel threads are supported and managed directly by the operating
system
 Examples – virtually all general -purpose operating systems, including:
• Windows
• Linux
• Mac OS X
• iOS
• Android
3.4.2 MULTI THREADING MODEL
Figure 3.10 User and kernel threads.
15
 Each user-level thread maps to kernel thread
 Creating a user-level thread creates a kernel
thread
 More concurrency than many-to-one
 Number of threads per process sometimes
restricted due to overhead
 Examples
• Windows
• Linux
A) Many-to-one Model
 Many user-level threads mapped to single kernel thread
 One thread blocking causes all to block
 Multiple threads may not run in parallel on muticore system because
only one may be in kernel at a time
 Few systems currently use this model
 Examples:
• Solaris Green Threads
• GNU Portable Threads
Figure 3.11 Many-to-one Model
B) One-to-one Model
3.4.2 MULTI THREADING MODEL
Figure 3.12 One-to-one Model
C) Many-to-Many Model
 Allows many user level threads to be mapped to many kernel threads
 Allows the operating system to create a sufficient number of kernel
threads
 Windows with the ThreadFiber package
 Otherwise not very common
D) Two-Level Model
 Similar to M:M, except that it allows a user thread to be bound to kernel
thread
16
Figure 3.14 Two-Level Model
3.4.2 MULTI THREADING MODEL(CONT…)
Figure 3.13 Many-to-Many Model
17
A) Semantics of fork() and exec() system calls
 If one thread in a program calls fork(), does the new process
duplicate all threads, or is the new process single-threaded?
 Some UNIX systems have chosen to have two versions of fork(), one
that duplicates all threads and another that duplicates only the
thread that invoked the fork() system call.
 exec() usually works as normal – replace the running process
including all threads
B) Signal handling
 Signals are used to notify a process that a particular event has
occurred.
 A signal handler is used to process signals
• Signal is generated by particular event
• Signal is delivered to a process
• Signal is handled by one of two signal handlers: default, user-
defined
 Where should a signal be delivered for multi-threaded?
• Deliver the signal to the thread to which the signal
applies
• Deliver the signal to every thread in the process
• Deliver the signal to certain threads in the process
• Assign a specific thread to receive all signals for the
3.4.3 THREADING ISSUES
18
C) Thread Cancellation
 Terminating a thread before it has finished
 Thread to be canceled is target thread
 Two general approaches:
• Asynchronous cancellation terminates the target thread
immediately
• Deferred cancellation allows the target thread to periodically check
if it should be cancelled
D) Thread- Local Storage
 Thread-local storage (TLS) allows each thread to have its own copy of
data
 Useful when you do not have control over the thread creation process
(i.e., when using a thread pool)
 Different from local variables
• Local variables visible only during single function invocation
• TLS visible across function invocations
 Similar to static data
• TLS is unique to each thread
3.4.3 THREADING ISSUES(CONT…)
19
E) Scheduler Activation
 Both M:M and Two-level models require communication to
maintain the appropriate number of kernel threads allocated to
the application
 Typically use an intermediate data structure between user and
kernel threads –
lightweight process (LWP)
• Appears to be a virtual processor on which
process can schedule user thread to run
• Each LWP attached to kernel thread
• How many LWPs to create?
 Scheduler activations provide upcalls –
a communication mechanism from the
kernel to the upcall handler in the thread
library
 This communication allows an application to
maintain
the correct number kernel threads
3.4.3 THREADING ISSUES(CONT…)
Figure 3.15 Light weight Process