Fergusson College
Ashoka Education Foundation
Established in
Ashoka Center for Business and Computer Studies, Nashik
2009
ISO 9001 : 2015 | Certificate No. : QM 01 00652 | Minority Institute
Chapter 2:
Processes and Threads
Mr. Rahul Sonawane
website: [Link]
Content
Fergusson College
• Process Concept – The processes, Process states, Process control block.
• Process Scheduling – Scheduling queues, Schedulers, context switch
• Operations on Process – Process creation with program using fork(),
Process termination
• Thread Scheduling- Threads, benefits, Multithreading Models, Thread
Libraries
[Link].(CS) Dept. ACBCS College, Nashik -Rahul Sonawane
Objectives
Fergusson College
• To introduce the notion of a process -- a program in execution, which forms
the basis of all computation
• To describe the various features of processes, including scheduling, creation
and termination, and communication
• To introduce the notion of a thread—a fundamental unit of CPU utilization
that forms the basis of multithreaded computer systems
• To discuss the APIs for the Pthreads, Windows, and Java thread libraries
• To explore several strategies that provide implicit threading
• To examine issues related to multithreaded programming
[Link].(CS) Dept. ACBCS College, Nashik -Rahul Sonawane
Fergusson College
Process Concept
• An operating system executes a variety of programs:
– Batch system – jobs
– Time-shared systems – user programs or tasks
• Textbook uses the terms job and process almost interchangeably
• Process – a program in execution; process execution must progress in sequential fashion
• 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
[Link].(CS) Dept. ACBCS College, Nashik -Rahul Sonawane
Process Concept
Fergusson (Cont.)
College
• Program is passive entity stored on disk (executable file), process is active
– Program becomes process when executable file loaded into memory
• Execution of program started via GUI mouse clicks, command line entry of its
name, etc
• One program can be several processes
– Consider multiple users executing the same program
[Link].(CS) Dept. ACBCS College, Nashik -Rahul Sonawane
Process
Fergusson in Memory
College
[Link].(CS) Dept. ACBCS College, Nashik -Rahul Sonawane
Process State
Fergusson College
• 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
[Link].(CS) Dept. ACBCS College, Nashik -Rahul Sonawane
Diagram of Process
Fergusson CollegeState
[Link].(CS) Dept. ACBCS College, Nashik -Rahul Sonawane
Process Control
Fergusson Block (PCB)
College
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
[Link].(CS) Dept. ACBCS College, Nashik -Rahul Sonawane
CPU Switch FromCollege
Fergusson Process to Process
[Link].(CS) Dept. ACBCS College, Nashik -Rahul Sonawane
Process Scheduling
Fergusson College
• Maximize CPU use, quickly switch processes onto CPU for time sharing
• Process scheduler selects among available processes for next
execution on CPU
• Maintains scheduling queues 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 the various queues
[Link].(CS) Dept. ACBCS College, Nashik -Rahul Sonawane
Ready Queue And Various
Fergusson CollegeI/O Device Queues
[Link].(CS) Dept. ACBCS College, Nashik -Rahul Sonawane
Representation
FergussonofCollege
Process Scheduling
▪ Queueing diagram represents queues, resources, flows
[Link].(CS) Dept. ACBCS College, Nashik -Rahul Sonawane
Schedulers
Fergusson College
• Short-term scheduler (or CPU scheduler) – selects which process should be executed next and
allocates CPU
– Sometimes the only scheduler in a system
– Short-term scheduler is invoked frequently (milliseconds) ⇒ (must be fast)
• Long-term scheduler (or job scheduler) – selects which processes should be brought into the ready
queue
– Long-term scheduler is invoked infrequently (seconds, minutes) ⇒ (may be slow)
– The long-term scheduler controls the degree of multiprogramming
• 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
• Long-term scheduler strives for good process mix
[Link].(CS) Dept. ACBCS College, Nashik -Rahul Sonawane
Addition of Medium
Fergusson Term Scheduling
College
▪ Medium-term scheduler can be added if degree of multiple programming needs to
decrease
▪ Remove process from memory, store on disk, bring back in from disk to continue
execution: swapping
[Link].(CS) Dept. ACBCS College, Nashik -Rahul Sonawane
Context Switch
Fergusson College
• 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 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
[Link].(CS) Dept. ACBCS College, Nashik -Rahul Sonawane
ContextCollege
Fergusson Switch
Conditions for Context Switching:
1) Clock Interrupt:
When the clock interrupt arrives, the operating system determines whether the currently
running process has executed for its allowable time slice.
2) I/O interrupts:
When an I/O interrupt arrives indicating that an I/O is over, the operating system
changes the state of all the processes in blocked state (for that I/O) to Ready state and
those in Blocked, swapped state to Ready, swapped state.
3) Process exit:
A process may exit normally or abnormally. In both the cases the operating system
schedules another process for execution and a context switch occurs.
4) Multitasking:
Most commonly, within some scheduling scheme, one process needs to be switched
out of the CPU so another process can run.
[Link].(CS) Dept. ACBCS College, Nashik -Rahul Sonawane
Operations
Fergussonon College
Processes
• System must provide mechanisms for:
– process creation,
– process termination,
– and so on as detailed next
[Link].(CS) Dept. ACBCS College, Nashik -Rahul Sonawane
Process Creation
Fergusson College
• Parent process create children processes, which, in turn create other processes,
forming a tree of processes
• Generally, 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
[Link].(CS) Dept. ACBCS College, Nashik -Rahul Sonawane
AFergusson
Tree of Processes
College in Linux
[Link].(CS) Dept. ACBCS College, Nashik -Rahul Sonawane
Process Creation
Fergusson (Cont.)
College
• 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
[Link].(CS) Dept. ACBCS College, Nashik -Rahul Sonawane
C Program ForkingCollege
Fergusson Separate Process
When a process calls the execlp, that process is completely
replaced by the new program, and the new program starts
executing at its main function.
The process ID does not change across an exec, because a
new process is not created. exec merely replaces the
current process's text, data, heap, and stack segments with
a brand new program from disk.
The combination of fork followed by exec is called spawning
a new process on some operating systems.
SYNOPSIS
#include <unistd.h>
int execlp(const char *path, const char *arg0, ...,
NULL);
[Link].(CS) Dept. ACBCS College, Nashik -Rahul Sonawane
CreatingFergusson
a Separate College
Process via Windows API
[Link].(CS) Dept. ACBCS College, Nashik -Rahul Sonawane
Process Termination
Fergusson College
• 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
[Link].(CS) Dept. ACBCS College, Nashik -Rahul Sonawane
Process Termination
Fergusson College
• 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
[Link].(CS) Dept. ACBCS College, Nashik -Rahul Sonawane
Process Termination
Fergusson College
• Kill:
• Kill command is use to send signal to a process or to kill a process. It typically uses kill SIGNAL
PID, where user knows the PID of the process. There are other ways to effectively kill a
process-killing a process by name, killing a
• Process by specifying part of the name, killing a process by pointing out the process with cursor
etc.
• a) Kill Command-Kill the Process by specifying its PID:
– For the signals, either the signal name or signal number can be used.
– User need to look-up the pid for the process and give it as an argument to kill.
• b) Kill all Command-Kill Processes by Name:
– Instead of specifying a process by its PID, user can specify the name of the process. If more
than one process runs with that name, all of them will be killed.
[Link].(CS) Dept. ACBCS College, Nashik -Rahul Sonawane
Reasons for Process
Fergusson Termination
College
Time Limit
Expired
Memory
Parent
Unavailabl
Request
e
Parent
Reasons Bounds
Terminatio
Violation
n for
Process
Terminatio
n
Privileged Protection
Instruction Error
Invalid
I/O Failure
Instruction
[Link].(CS) Dept. ACBCS College, Nashik -Rahul Sonawane
Reasons for Process
Fergusson Termination
College
• 1) Time Limit Expired:
– When the process execution does not complete within the ‘time slot’, then the process will be
terminated from the running state.
• 2) Memory Unavailable:
– If a process needs more memory than the available memory, then the process will be terminated from
the running state.
• 3) Bounds Violation (or) Access Violation:
– The process tries to access a memory location that it is not allowed to access.
• 4) Protection Error:
– The process attempts to use a resource or a file that it is not allowed to use, or it tries to use it in an
improper fashion, such as writing to a read-only file.
• 5) I/O Failure:
– A process need an I/O operation at the time of execution, but the l/O device is not available at that time.
[Link].(CS) Dept. ACBCS College, Nashik -Rahul Sonawane
Reasons for Process
Fergusson Termination
College
• 6) Invalid Instruction:
– If a process having illegal instructions and the CPU failed to execute those instructions, then the
process terminated.
• 7) Privileged Instruction:
– The process attempts to use an instruction reserved for the operating system.
• 8) Parent Termination:
– When the parent process terminated, the child process also terminated automatically. The parent
process has the authority to terminate any of its Childs.
• 9) Parent Request:
– If the parent process requests the child process about the termination, then the child process
terminated automatically.
[Link].(CS) Dept. ACBCS College, Nashik -Rahul Sonawane
Fergusson
Multiprocess College– Chrome Browser
Architecture
• Many web browsers ran as single process (some still do)
– If one web site causes trouble, entire browser can hang or crash
• Google Chrome Browser is multiprocess with 3 different types of processes:
– Browser process manages user interface, disk and network I/O
– Renderer process renders web pages, deals with HTML, Javascript. A new renderer created for each
website opened
• Runs in sandbox restricting disk and network I/O, minimizing effect of security exploits
– Plug-in process for each type of plug-in
[Link].(CS) Dept. ACBCS College, Nashik -Rahul Sonawane
FergussonThreads
College
• So far, process has a single thread of execution
• A thread is a basic unit of CPU utilization.
• It is also called a Lightweight Process(LWP).
• It has its own program counter, a register set and stack space.
• It share with the peer threads its code section, data section and os resources such as
open files and signals, collectively called a task.
• 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
[Link].(CS) Dept. ACBCS College, Nashik -Rahul Sonawane
FergussonBenefits
College
• 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 multiprocessor architectures
• Benefits of multithreading increase in multiprocessor systems because
different threads can be scheduled to different processors
[Link].(CS) Dept. ACBCS College, Nashik -Rahul Sonawane
Single and Multithreaded
Fergusson Processes
College
[Link].(CS) Dept. ACBCS College, Nashik -Rahul Sonawane
Difference betweenCollege
Fergusson Process and Thread
S.N. Process Thread
1 Process is heavy weight or resource intensive. Thread is light weight, taking lesser resources than a
process.
2 Process switching needs interaction with operating Thread switching does not need to interact with
system. operating system.
3 In multiple processing environments, each process All threads can share same set of open files, child
executes the same code but has its own memory processes.
and file resources.
4 If one process is blocked, then no other process can While one thread is blocked and waiting, a second
execute until the first process is unblocked. thread in the same task can run.
5 Multiple processes without using threads use more Multiple threaded processes use fewer resources.
resources.
6 In multiple processes each process operates One thread can read, write or change another
independently of the others. thread's data.
[Link].(CS) Dept. ACBCS College, Nashik -Rahul Sonawane
Types of Thread
Fergusson College
• Threads are implemented in following two ways −
– User Level Threads − User managed threads.
– Kernel Level Threads − Operating System managed threads acting on kernel, an
operating system core.
[Link].(CS) Dept. ACBCS College, Nashik -Rahul Sonawane
User Threads
Fergusson College
• Thread management done by user-level threads library: The library provides support for thread
creation, scheduling, and management.
• In this case, the thread management kernel is not aware of the existence of threads.
• The thread library contains code for creating and destroying threads, for passing message and
data between threads, for scheduling thread execution and for saving and restoring thread
contexts.
• Three primary thread libraries:
– POSIX Pthreads
– Win32 threads
– Java thread
[Link].(CS) Dept. ACBCS College, Nashik -Rahul Sonawane
User Threads-Advantages
Fergusson College& Disadvantages
• Advantages
• Thread switching does not require Kernel mode privileges.
• User level thread can run on any operating system.
• Scheduling can be application specific in the user level thread.
• User level threads are fast to create and manage.
• Disadvantages
• In a typical operating system, most system calls are blocking.
• Multithreaded application cannot take advantage of multiprocessing.
[Link].(CS) Dept. ACBCS College, Nashik -Rahul Sonawane
Kernel Threads
Fergusson College
• Supported by the Kernel directly. Kernel performs the thread creation, scheduling and management inside
the kernel
• In this case, thread management is done by the Kernel. There is no thread management code in the
application area.
• Kernel threads are supported directly by the operating system.
• Any application can be programmed to be multithreaded. All of the threads within an application are
supported within a single process.
• The Kernel maintains context information for the process as a whole and for individuals threads within the
process.
• Examples
– Windows XP/2000
– Solaris
– Linux
– Tru64 UNIX
– Mac OS X
[Link].(CS) Dept. ACBCS College, Nashik -Rahul Sonawane
Multithreading
Fergusson CollegeModels
• Many systems provide support for both user-level and kernel level threads. This
provides different multithreading models
– Many-to-One: maps many user level threads into one kernel level thread.
• The entire process will block if a thread makes a blocking system call
• Since only one thread can access kernel at a time, multiple threads cannot run
concurrently and thus cannot make use of multiprocessors. eg: Greek threads, GNV
poratble threads.
[Link].(CS) Dept. ACBCS College, Nashik -Rahul Sonawane
Multithreading
Fergusson CollegeModels
One-to-One: maps each user level thread to a kernel level thread
• Creating a user level thread results in creating a kernel thread
• More overhead and allows parallelism
• Because of the overhead most implementations limit the number of kernel threads
created.
• eg : Windows 95,98,NT,2000 and XP
[Link].(CS) Dept. ACBCS College, Nashik -Rahul Sonawane
Multithreading
Fergusson CollegeModels
– Many-to-Many: Multiplexes many user level threads to a smaller or equal
number of kernel threads
• Has the advantages of both the many-to-one and one-to-one model
• Solaris2, IRIS, HP-UX takes this approach
[Link].(CS) Dept. ACBCS College, Nashik -Rahul Sonawane
Two Level College
Fergusson Model
[Link].(CS) Dept. ACBCS College, Nashik -Rahul Sonawane
Thread
Fergusson Libraries
College
• A thread library provides the programmer an API for creating and
managing threads.
• There are two primary ways of implementing a thread library.
– The first approach is to provide a library entirely in user space with
no kernel support. All code and data structures for the library exist in
user space. This means that invoking a function in the library results
in a local function call in user space and not a system call.
– The second approach is to implement a kernel-level library supported
directly by the OS. In this case, code and data structures for the
library exist in kernel space. Invoking a function in the API for the
library typically results in a system call to the kernel.
[Link].(CS) Dept. ACBCS College, Nashik -Rahul Sonawane
Thread
Fergusson Libraries
College
• Three main thread libraries are in use today:
– POSIX Pthreads. Pthreads, the threads extension of the POSIX
standard, may be provided as either a user- or kernel-level library.
– Win32. The Win32 thread library is a kernel-level library available on
Windows systems.
– Java. The Java thread API allows thread creation and management
directly in Java programs. However, because in most instances the
JVM is running on top of a host OS, the Java thread API is typically
implemented using a thread library available on the host system.
[Link].(CS) Dept. ACBCS College, Nashik -Rahul Sonawane
Pthreads
Fergusson College
• It refers to the POSIX standard (IEEE 1003.1C) defining an API for thread creation and
synchronization.
• API specifies behavior of the thread library, implementation is up to developer of the library
• Common in UNIX operating systems (Solaris, Linux, Mac OS X)
[Link].(CS) Dept. ACBCS College, Nashik -Rahul Sonawane
Pthreads
Fergusson College
All Pthread programs include Pthread.h header file.
Some Pthread libraries :-
• pthread_create - create a new thread
• pthread_join - wait for termination of another thread
• pthread_exit - terminate the calling thread
[Link].(CS) Dept. ACBCS College, Nashik -Rahul Sonawane
Fergusson College
Thank YouVisit Us
[Link]
[Link].(CS) Dept. ACBCS College, Nashik -Rahul Sonawane