AMBO UNIVERSITY WOLISO CAMPUS
School of Technology and Informatics Department of Information Systems
Course Title: Operating System Course Code: INSY2051; ECTS: 5
Chapter Two
Processes and Threads
INSY2051 : Operating System
BSc(IS) 2rd Year, Second Semester, 2017 E.C
JAFAR B.
By Jafar 4/12/2025
Outlines
2
Processes
Threads
Scheduling
Interposes communication
By Jafar 4/12/2025
Process concept
3
Early systems
One program at a time was executed and a single program has
a complete control.
Modern OS allow multiple programs to be loaded in to memory
and to be executed at the same time.
This requires firm control over execution of programs.
The notion of process emerged to control the execution of
programs.
By Jafar 4/12/2025
Processes
4
What is a process?
A process is basically a program in execution.
It is a program under execution or the current execution of instructions
The entity that can be assigned to and executed on a processor
An activity of some kind which has a program, input, output, and a state.
a program in execution; process execution must progress in sequential
fashion
Conceptually, each process has its own virtual CPU.
In reality, of course, the real CPU switches back and forth from process to
process.
Provide the illusion of parallelism, which is some times called pseudo
parallelism.
By Jafar 4/12/2025
Process concept(con’t…)
5
When a program is loaded into the memory and it becomes a process, it can be
divided into four sections ─ stack, heap, text and data.
The following image shows a simplified layout of a process inside main
memory
By Jafar 4/12/2025
Cont’d
6
Stack: The process Stack contains the temporary data such as
method/function parameters, return address and local
variables.
Heap: This is dynamically allocated memory to a process during
its run time.
Text: This includes the current activity represented by the value of
Program Counter and the contents of the processor's registers.
Data: This section contains the global and static variables.
By Jafar 4/12/2025
Program Vs Process
7
• Program
o It is sequence of instructions defined to perform some task
o It is a passive entity
o A program is a static set of instructions
o Not executed by itself
Process
o It is a program in execution
o It is an instance of a program running on a computer
o It is an active entity
o A processor performs the actions defined by a process
o A process is a dynamic execution of a program
o Need CPU to Executed
By Jafar 4/12/2025
Process Creation
8
A process may create several new process via a create-process
system call , during the course of action.
The creation process is called a parent process and a new
processes are the children of that process
Each of these new process may in turn create other processes
forming a tree of processes
Process identified and managed via a process identifier Pid.
By Jafar 4/12/2025
Process Creation…
9
Address space
Child duplicate of parent ( When a process creates new process, a
duplicate of that process is created.
This new process is called the child and the process that created it is called
the parent.
The child process then replaces the copy for the code the parent process
created with the code the child process is supposed to execute )
Child has a program loaded into new program
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
By Jafar 4/12/2025
Process Creation..
10
By Jafar 4/12/2025
Process Creation
11
There are four principal events that cause processes to be created:
1. System initialization:
When an operating system is booted, typically several
processes are created.
These processes can be:
Foreground processes : processes that interact with
(human) users and perform work for them.
Background processes: processes which are not
associated with particular users, but instead have some
specific function. E.g.: process to Accept mail. Driver
update, Network configration
By Jafar 4/12/2025
Process Creation..
12
2. Execution of a process creation system call by a running process
Running process will issue system calls to create one or more
new processes to help it do its job.
Creating new processes is particularly useful when the work to
be done can easily be formulated in terms of several related, but
otherwise independent interacting processes.
A process Fetching large amount of data and execute it will
create two different process one for fetching data and another to
execute it.
By Jafar 4/12/2025
Process Creation (….)
13
3. A user request to create a new process.
In interactive systems, users can start a program by typing a
command or(double) clicking an icon.
Taking either of these actions starts a new process and runs
the selected program in it.
4. Initiation of a batch job.
users can submit batch jobs to the system (possibly
remotely).
When the operating system decides that it has the resources
to run another job, it creates a new process and runs the next
job from the input queue in it.
By Jafar 4/12/2025
Process Termination
14
o After a process has been created, it starts running and does
whatever its job is.
o However, nothing lasts forever, not even processes.
o Sooner or later the new process will terminate, usually due to one
of the following conditions:
Conditions which terminate processes
Normal exit (voluntary)
Error exit (voluntary)
Fatal error (involuntary)
Killed by another process (involuntary)
By Jafar 4/12/2025
Process Termination….
15
1.Normal exit (voluntary)
o Most processes terminate because they have done their work.
Example ,When a compiler has compiled the program, it executes a system call
to tell the operating system that it is finished.
This call is exit in UNIX and Exit Process in Windows
o Screen-oriented programs also support voluntary termination.
Example Word processors, Internet browsers and similar programs always have an
icon or menu item that the user can click to tell the process to remove any
temporary files it has open and then terminate.
2. Error exit (voluntary)
o A process is terminate if it is discovers a fatal error.
For example, if a user types the command cc foo.c to compile
the program foo.c and no such file exists, the compiler
simply exits.
By Jafar 4/12/2025
Process Termination….
16
3. Fatal error (involuntary)
o The another reason for termination is an error caused by the
process, often due to a program bug.
Examples include executing an illegal instruction,
referencing non-existent memory, or dividing by zero.
4. Killed by another process (involuntary)
o The fourth reason a process might terminate is that the process
executes a system call telling the operating system to kill some
other process.
o In UNIX this call is kill. The corresponding Win32 function is
Terminate Process.
By Jafar 4/12/2025
Process States
17
The process state define the current activity of the process.
As a process executes, it changes state
The state in which may in is differ from one system to another.
Below we see three states a process may be in:
Running : Instructions of program are being executed.
Ready: The process is waiting to be assigned to a processor.
Blocked :The process is waiting for some event to occur
By Jafar 4/12/2025
Process States…
18
Four transitions are possible among these three states.
Transition 1:Process blocks for an event to occur.
Transition 2:Scheduler picks another process to have CPU
time.
Transition 3:Scheduler picks first process get the CPU to run
again
Transition 4:event becomes occurred to awakened for blocked
process.
By Jafar 4/12/2025
Five State Process Module and Transaction
19
Five state
1. New : a process is being Created
2. Ready: process is waiting to run(runnable), temporarily stopped to let
another process run.
3. Running : process is actually using CPU.
4. Blocked/Waiting : unable to run until some external events happen.
5. Exit/Terminated :The process has finished the executions.
By Jafar 4/12/2025
Process control Block(PCB)
20
A process control Block(PCB) is a data structure maintain by the
OS for every process.
PCB is used for storing the collection of the information about
the process.
The PCB is identified by an integer process id(PID).
A PCB keeps all information needed to keep track a process.
The PCB maintained for the process through is lifetime and its
delete once the process terminate.
The Architecture of PCB is completely dependent on the OS and
may contain different information in different operating System.
PCB lies in kernel memory space .
By Jafar 4/12/2025
Process control Block(PCB) contains
21
The process identifier: It is a unique key that is used
to uniquely identify processes.
Process state: the current state of the process.i.e
where it is ready, running or waiting
Program counter: a pointer to the address of the next
instruction that requires to be executed for the
process.
Priority: priority of the process
CPU Registers: Various CPU register where process
need to be stored for the execution for running state .
I/O Information: This include a list of I/O device
allocated to the process.
Accounting Information: This include the Amount
of CPU for process , the time limits.
PCB
By Jafar 4/12/2025
CPU Switch From Process to Process
22
Context Switching
• Context switch means stopping one
process and restarting another process.
• When an event occur , the OS save the
state of an Active Process and restore the
state of the new process.
• Context Switching is purely overload b/c
system does not perform any useful work
while context switch.
Steps performed by OS during Context
Switching
Sequence of Actions:
1. OS takes control (through Interrupt)
2. Save Context of Running Process in the
process of PCB
3. Reload Context of new process from
new process PCB.
4. Return Control to the new process
By Jafar 4/12/2025
Implementation of Processes (1)
23
o To implement the process model, the operating system maintains a table (an
array of structures), called the process table, with one entry per process. these
entries process control blocks(PCB) also called task control block.
PCB Contains information associated with each process.
1. Process state:- can be ready, running, waiting, and etc.
2. Program counter:- indicates the address of the next instruction to be
executed.
3. CPU registers:- includes general-purpose registers, stack Pointers,
index registers and accumulators.
By Jafar 4/12/2025
Implementation of Processes….
24
4. Memory-management information:- includes the value of base and
limit register. The information is useful for reallocating the memory
when the process terminates.
5. CPU scheduling information:- includes the CPU scheduling
information for each and every process(Eg. process priorities,
pointers to scheduling queues, etc.
6. Accounting information:- includes the amount of CPU and real
time used, time limits, job or process numbers, account numbers etc.
7. I/O status information:- includes list of opened files
8. Event information:- for a process in the blocked (wait) state this
field contains information concerning the event for which the process
is waiting.
By Jafar 4/12/2025
Thread
25
Thread concept
Thread usage
Thread library
Thread implementation
By Jafar 4/12/2025
Thread concept
26
Process model is based on two independent concepts: resource
grouping and execution.
One way of looking at a process is that it is a way to group related
resources together.
A process has an address space containing program text and data, as
well as other resources. These resource may include open files, child
processes, pending alarms, signal handlers, accounting information, and
more.
By putting them together in the form of a process, they can be
managed more easily.
Thread is a light weight process created by a process
It is a single sequence stream within a process
By Jafar 4/12/2025
Thread concept(con’t..)
27
The other concept a process has is a thread of execution, usually
shortened to just thread.
The thread has a program counter that keeps track of which
instruction to execute next.
It has registers, which hold its current working variables.
It has a stack, which contains the execution history, with one
frame for each procedure called but not yet returned from.
Processes are used to group resources together; threads are the
entities scheduled for execution on the CPU.
The term multithreading is also used to describe the situation of
allowing multiple threads in the same process.
By Jafar 4/13/2025
Thread concept(con’t..)
28
o A thread consists of:
thread
id
program counter
register set
stack
o Threads belonging to the same process share:
its code
its data section
other OS resources
By Jafar 4/12/2025
Processes and Threads
29
Similarities Differences
• Both share CPU and only one • Unlike processes, threads
thread/process is active (running) are not independent of one
at a time. another.
• Like processes, threads within a • Unlike processes, all threads
process execute sequentially. can access every address in
• Like processes, thread can create the task.
children.
• Unlike processes, thread are
• Like process, if one thread is
blocked, another thread can run.
design to assist one other.
• Note that process might or might not
assist one anther b/c processes may be
Pr. Vs Th originated from different user
By Jafar 4/13/2025
Thread usage
30
o Several reasons for having multiple threads:
o Many applications need multiple activities are going on at once.
Decomposing such an application into multiple sequential threads that
run in quasi-parallel, the programming model becomes simpler.
o They are lighter weight than processes, they are easier (i.e.,
faster) to create and destroy than processes.
o Having multiple threads within an application provide higher
performance argument.
If there is substantial computing and also substantial I/0, having threads
allows these activities to overlap, thus speeding up the application.
o Threads are useful on systems with multiple CPUs
By Jafar 4/12/2025
Thread library
31
o Thread libraries provide programmers an API to create and
manage threads
There are three basic libraries used:
POSIX pthreads
They may be provided as either a user or kernel library, as an extension to the POSIX
standard
Systems like Solaris, Linux and Mac OS X implement pthreads specifications
WIN32 threads
• These are provided as a kernel-level library on Windows systems.
Java threads
• Since Java generally runs on a Java Virtual Machine, the implementation of threads is
based upon whatever OS and hardware the JVM is running on, i.e. either Pthreads or
Win32 threads depending on the system.
By Jafar 4/12/2025
Thread implementation
32
o There are two main ways to implement a threads package: in user
space and in the kernel.
Implementing Threads in User Space
All code and data structure are reside in user space.
Invoking a function in the library results in a local procedure call in user space not
system call.
the kernel is not aware of the existence of threads.
Advantage:
To do thread switching, it calls a run-time system procedure, which is least an order
of magnitude-may be more-faster than trapping to the kernel
They allow each process to have its own customized scheduling algorithm.
Disadvantage:
Problem of how blocking system calls are implemented
Problem of page faults
No other thread in that process will ever run unless the first thread voluntarily gives
up the CPU.
By Jafar 4/12/2025
Thread implementation….
33
Implementing Threads in kernel Space
o All code and data structure are reside in kernels pace.
o Invoking a function in the library results system call.
o The kernel is aware of the existence of threads.
Advantage:
All calls that might block a thread are implemented as system calls
If one thread in a process causes a page fault, the kernel can easily check to see
if the process has any other runnable threads, and if so, run one of them while
waiting for the required page to be brought in from the disk.
o kernel threads solve some While problems, they do not solve all problem
o what happens when a multithreaded process forks?
In many cases, the best choice depends on what the process is planning to
do next.
By Jafar 4/12/2025
Thread implementation….
34
Implementing Threads in kernel Space(con’t..)
o When a signal comes in, which thread should handle it?
Possibly threads could register their interest in certain signals but there may
be two or more threads register for the same signal.
Hybrid Implementations
o Use kernel-level threads and then multiplexes user-level threads
onto some or all of the kernel threads.
By Jafar 4/12/2025
Benefits
35
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 has
lower overhead than context switching
Scalability – multithreading can take advantage of multiprocessor
architectures
By Jafar 4/12/2025
Multicore Programming
36
A recent trend in computer architecture is to produce chips with multiple cores,
or CPUs on a single chip.
Multi-core programming provides a mechanism for more efficient use of
multiple computing cores and improved concurrency.
Multicore or multiprocessor systems putting pressure on programmers,
challenges include:
Dividing tasks
Balance
Data splitting
Data dependency
Testing and debugging
Parallelism implies a system can perform more than one task simultaneously
Concurrency supports more than one task making progress
Single processor / core, scheduler providing concurrency
By Jafar 4/12/2025
Concurrency vs. Parallelism
37
Concurrent execution on single-core system:
Parallelism on a multi-core system:
By Jafar 4/12/2025
Amdahl’s Law
38
Identifies performance gains from adding additional cores to
an application that has both serial and parallel components
S is serial portion
N processing cores
That is, if application is 75% parallel / 25% serial, moving
from 1 to 2 cores results in speedup of 1.6 times
As N approaches infinity, speedup approaches 1 / S
Serial portion of an application has disproportionate
effect on performance gained by adding additional cores
By Jafar 4/12/2025
Multicore Programming (Cont.)
39
Generally, there are two types of parallelism
Data parallelism – divides the data up amongst multiple cores
( threads ), and performs the same task on each subset of the
data.
Task parallelism – divides the different tasks to be performed
among the different cores and performs them
simultaneously.(each perform unique operation)
In most cases applications use a hybrid of the two strategies
By Jafar 4/12/2025
Multithreading Models
40
Reading assignment
Amdahl’s Law(serial and parallel components)
Multithreading Models
common ways of establishing Multithreading Models such a
relationship are:
Many-to-One
One-to-One
Many-to-Many
Implicit Threading
Thread Pools
Thread Cancellation
By Jafar 4/14/2025
Ended!
41
Chapter Two
Processes and Threads
Course Code: INSY2051 :Operating
System
BSc(IS) 2nd Year, Second Semester, 2017 E.C
By Jafar 4/12/2025