SlideShare a Scribd company logo
UNIT 2
Process Management
Syllabus
• Process Management: Process concept
• Process control block
• Types of scheduler
• Context switch
• Multithreading model
• Goals of scheduling and
• Different scheduling algorithms.
Process Management
• A Program does nothing unless its instructions are executed
by a CPU.
• A program in execution is called a process.
• In order to accomplish its task, process needs the
computer resources.
• There may exist more than one process in the system
which may require the same resource at the same time.
Cont..
• Therefore, the operating system has to manage all the
processes and the resources in a convenient and efficient
way.
• Some resources may need to be executed by one process
at one time to maintain the consistency otherwise the
system can become inconsistent and deadlock may occur.
Cont..
• The operating system is responsible for the following activities in
connection with Process Management
• Scheduling processes and threads on the CPUs.
• Creating and deleting both user and system processes.
• Suspending and resuming processes.
• Providing mechanisms for process synchronization.
• Providing mechanisms for process communication.
Program vs Process
• A process is a program in execution.
• For example, when we write a program in C or C++ and compile it,
the compiler creates binary code.
• The original code and binary code are both programs.
• When we actually run the binary code, it becomes a process.
• A process is an ‘active’ entity, as opposed to a program, which
is considered to be a ‘passive’ entity.
• A single program can create many processes when run multiple
times; for example, when we open a.exe or binary file multiple
times, multiple instances begin (multiple processes are created).
Whatdoes aprocesslooklikeinmemory?
 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
Cont..
Text section:
• This section consist of source code.
• It also includes the current activity as represented by value of program counter
and the content of processor’s register.
Stack section:
• This section consist of temporary data such as function, parameters, return
addresses and local variables.
• Stack is used for local variables, space on the stack is reserved for local variables
when they are declared. Space is freed up when variables go out of scope.
Data Section:
• This section stores global and static variables, which needs to be allocated and
initialized prior to executing main program.
Heap Section:
• It is used for dynamic memory allocation and is managed via calls to new, delete
malloc, free etc.
Cont..
• Program
• A program is a piece of code which may be a single line or
millions of lines.
• A computer program is usually written by a computer
programmer in a programming language. For example, here is a
simple program written in C programming language.
Cont…
• A computer program is a collection of instructions that performs
a specific task when executed by a computer.
• When we compare a program with a process, we can conclude
that a process is a dynamic instance of a computer program.
• A part of a computer program that performs a well-defined task is
known as an algorithm.
• A collection of computer programs, libraries and related data are
referred to as a software.
Unit 2_OS process management
Cont..
ProcessControlBlock(PCB)
• Process Control Block (PCB, also called Task Controlling Block,
Entry of the Process Table, Task Struct, or Switchframe) is a
data structure in the operating system
• OS kernel containing the information needed to manage the
scheduling of a particular process.
• The PCB is "the manifestation of a process in an operating
system."
Cont..
• A Process Control Block is a data structure maintained by the
Operating System for every process. The PCB is identified by an
integer process ID (PID).
Cont..
• 1. Process ID
• When a process is created, a unique id is assigned to the process
which is used for unique identification of the process in the
system.
• 2. Program counter
• A program counter stores the address of the last instruction of
the process on which the process was suspended.
• The CPU uses this address when the execution of this process is
resumed.
Cont..
• 3. Process State
• The Process, from its creation to the completion, goes through
various states which are new, ready, running and waiting.
• 4. Priority
• Every process has its own priority. The process with the highest
priority among the processes gets the CPU first. This is also
stored on the process control block.
Cont..
• CPU registers
• Various CPU registers where process need to be stored for
execution for running state.
• CPU Scheduling Information
• Process priority and other scheduling information which is
required to schedule the process.
Cont..
• Memory management information
• This includes the information of page table, memory limits,
Segment table depending on memory used by the operating
system.
• Accounting information
• This includes the amount of CPU used for process execution, time
limits, execution ID etc.
• IO status information
• This includes a list of I/O devices allocated to the process.
ProcessScheduling
• Process scheduling is a task of operating system to schedules
the processes of different states like ready, running, waiting.
• This task is very useful in maintain the computer system.
• Process scheduling allocates the time interval of each process
in which the process is to be executed by the central
processing unit (CPU).
Cont..
• The act of determining which process is in the ready state, and
should be moved to the running state is known as Process
Scheduling.
• The prime aim of the process scheduling system is to keep the
CPU busy all the time and to deliver minimum response time
for all programs.
• The scheduler must apply appropriate rules for swapping
processes IN and OUT of CPU.
WhatareSchedulingQueues?
• All processes when enters into the system are stored in the job
queue.
• Processes in the Ready state are placed in the ready queue.
• Processes waiting for a device to become available are placed
in device queues.
Cont..
• The process could issue an I/O request, and then be placed in an
I/O queue.
• The process could create a new subprocess and wait for its
termination.
• The process could be removed forcibly from the CPU, as a result of
an interrupt, and be put back in the ready queue.
Cont..
Cont..
• A new process is initially put in the Ready queue. It waits in the
ready queue until it is selected for execution(or dispatched).
• Once the process is assigned to the CPU and is executing, one of
the following several events can occur:
• The process could issue an I/O request, and then be placed in the I/O queue.
• The process could create a new subprocess and wait for its termination.
• The process could be removed forcibly from the CPU, as a result of an
interrupt, and be put back in the ready queue.
Cont..
Cont..
• An operating system uses two types of scheduling processes
execution, preemptive and non - preemptive.
• 1. Preemptive process:
In preemptive scheduling policy, a low priority process has to
be suspend its execution if high priority process is waiting in
the same queue for its execution.
• 2. Non - Preemptive process:
In non - preemptive scheduling policy, processes are executed in
first come first serve basis, which means the next process is
executed only when currently running process finishes its
execution.
TypesofSchedulers
• There are three types of schedulers available:
• Long Term Scheduler
• Short Term Scheduler
• Medium Term Scheduler
Cont..
Long-termScheduler
• Long-term scheduler is also known as Job Scheduler.
• It selects a balanced mix of I/O bound and CPU bound processes
from the secondary memory (new state).
• Then, it loads the selected processes into the main memory
(ready state) for execution.
• It brings the new process to the ‘Ready State’.
• It controls Degree of Multi-programming, i.e., number of process
present in ready state at any point of time.
• It is important that the long-term scheduler make a careful
selection of both IO and CPU bound process.
Short-termScheduler
• Short-term scheduler is also known as CPU Scheduler.
• It decides which process to execute next from the ready queue.
• After short-term scheduler decides the
process, Dispatcher assigns the decided process to the CPU for
execution.
Cont..
• It is responsible for selecting one process from ready state for
scheduling it on the running state.
• Dispatcher is responsible for loading the process selected by
Short-term scheduler on the CPU (Ready to Running State)
• Context switching is done by dispatcher only.
• A dispatcher does the following:
• Switching context.
• Switching to user mode.
• Jumping to the proper location in the newly loaded program.
Medium-termScheduler
• Medium-term scheduler swaps-out the processes from main
memory to secondary memory to free up the main memory when
required.
• Thus, medium-term scheduler reduces the degree of
multiprogramming.
• After some time when main memory becomes available, medium-
term scheduler swaps-in the swapped-out process to the main
memory and its execution is resumed from where it left off.
• Swapping may also be required to improve the process mix.
Cont..
Cont..
• The medium-term scheduler swaps out a process from main
memory.
• It can again swap in the process later from the point it stopped
executing.
• This can also be called as suspending and resuming the process.
• This is helpful in reducing the degree of multiprogramming.
Swapping is also useful to improve the mix of I/O bound and CPU
bound processes in the memory.
Long-term scheduler Medium-term scheduler Short-term scheduler
It is a job scheduler
It is a process swapping
scheduler.
It is a CPU scheduler
It controls the degree of
multiprogramming.
It reduces the degree of
multiprogramming.
It provides lesser control
over degree of
multiprogramming.
Speed is lesser than short-
term scheduler
Speed is in between the
long-term and short-term
schedulers.
Speed is fastest among the
other two.
It is minimal or almost
absent in time sharing
system.
It is a part of time sharing
system.
It is also minimal in time
sharing system.
It selects processes from
new state and loads them
into ready state.
It swaps-out processes from
main memory to secondary
memory and later swaps in.
It selects processes from the
ready state and assigns to
the CPU.
Operates less frequently
since processes are not
rapidly created.
Operates more frequently
than long-term scheduler
but less frequently than
short-term scheduler.
Operates very frequently to
match the speed of CPU
since CPU rapidly switches
from one process to another
Context Switch
• A context switch occurs when a computer's CPU switches from
one process or thread to a different process or thread.
• Context switching allows for one CPU to handle numerous
processes or threads without the need for additional processors.
• Any operating system that allows for multitasking relies heavily
on the use of context switching to allow different processes to run
at the same time.
Cont..
Unit 2_OS process management
Cont..
• There are three situations that a context switch is necessary:
• Multitasking - When the CPU needs to switch processes in and out of
memory, so that more than one process can be running.
• Kernel/User Switch - When switching between user mode to kernel
mode, it may be used (but isn't always necessary).
• Interrupts - When the CPU is interrupted to return data from a disk
read.
StepsofContextSwitch
• The steps in a full process switch are:
• Save the context of the processor, including program counter and
other registers.
• Update the process control block of the process that is
currently in the Running state. This includes changing the state of
the process to one of the other states (Ready; Blocked;
Ready/Suspend; or Exit).
• Move the process control block of this process to
the appropriate queue (Ready; Blocked on Event i ;
Ready/Suspend).
Cont..
• Select another process for execution.
• Update the process control block of the process selected. This
includes changing the state of this process to Running.
• Update memory management data structures. This includes
information of page table, memory limits, Segment table
depending on memory used by the operating system
• Restore the context of the processor to that which existed at the
time the selected process was last switched out of the Running
state, by loading in the previous values of the program counter
and other registers.
fork()inC
• Fork system call is used for creating a new process, which is
called child process, which runs concurrently with the process
that makes the fork() call (parent process).
• After a new child process is created, both processes will execute
the next instruction following the fork() system call.
• A child process uses the same pc(program counter), same CPU
registers, same open files which use in the parent process.
Cont..
• It takes no parameters and it returns an integer value
• Below are different values returned by fork().
• Negative Value: creation of a child process was unsuccessful.
• Zero: Returned to the newly created child process.
• Positive value: Returned to parent or caller. The value contains
process ID of newly created child process.
Example:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
// make two process which run same
// program after this instruction
fork();
printf("Hello world!n");
return 0;
}
• OutPut: Hello world!
• Hello world!
Cont..
#include <stdio.h>
#include <sys/types.h>
int main()
{
fork();
fork();
fork();
printf("hellon");
return 0;
}
Cont..
Cont..
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
void forkexample()
{
// child process because return value zero
if (fork() == 0)
printf("Hello from Child!n");
// parent process because return value non-zero.
else
printf("Hello from Parent!n");
}
int main()
{
forkexample();
return 0;
}
MultithreadingModel
• Multithreading allows the execution of multiple parts of a
program at the same time.
• These parts are known as threads and are lightweight
processes available within the process.
• Therefore, multithreading leads to maximum utilization of the
CPU by multitasking.
Cont..
• What are Threads?
• Thread is an execution unit which consists of its own program
counter, a stack, and a set of registers.
• Threads are also known as Lightweight processes.
• Threads are popular way to improve application through
parallelism.
• The CPU switches rapidly back and forth among the threads
giving illusion that the threads are running in parallel.
Cont..
As each thread has its own independent resource for process execution,
multiple processes can be executed parallely by increasing number of
threads.
Thread
 A thread is a lightweight process that can be managed
independently by a scheduler
 Threads are parts of a process and so are dependent.
 A thread shares the data segment, code segment, files etc.
with its peer threads.
• There are two types of threads:
• User Threads
• Kernel Threads
Cont..
• User threads, are above the kernel and without kernel support.
These are the threads that application programmers use in
their programs.
• Kernel threads are supported within the kernel of the OS itself.
All modern OSs support kernel level threads, allowing the kernel
to perform multiple simultaneous tasks and/or to service
multiple kernel system calls simultaneously.
Multithreading Models
• The user threads must be mapped to kernel threads, by one of the
following strategies:
• Many to One Model
• One to One Model
• Many to Many Model
Many-to-OneModel
• The many-to-one model maps many user-
level threads to one kernel thread.
• Thread management is done by the thread
library in user space, so it is efficient.
• However, the entire process will block if a
thread makes a blocking system call.
• Also, because only one thread can access
the kernel at a time, multiple threads are
unable to run in parallel on multicore
systems
One-to-One Model
• The one-to-one model maps each user thread to a kernel
thread.
• It provides more concurrency than the many-to-one
model by allowing another thread to run when a
thread makes a blocking system call.
• It also allows multiple threads to run in parallel on
multiprocessors.
• The only drawback to this model is that creating a user
thread requires creating the corresponding kernel thread.
• Because the overhead of creating kernel threads can
burden the performance of an application, most
implementations of this model restrict the number of
threads supported by the system.
• Linux, along with the family of Windows operating
systems, implement the one-to-one model.
Cont..
• In this model, one to one relationship between kernel and user
thread.
• In this model multiple thread can run on multiple processor.
Problem with this model is that creating a user thread requires
the corresponding kernel thread.
Many-to-Many Model
• The many-to-many model multiplexes many user-level
threads to a smaller or equal number of kernel
threads.
• The number of kernel threads may be specific to either a
particular application or a particular machine
• The many to many does not have the disadvantages of
the one to one model or the many to one model.
• There can be as many user threads as required and
their corresponding kernel threads can run in
parallel on a multiprocessor.
• Whereas the many to-one model allows the developer to
create as many user threads as she wishes, it does
not result in true concurrency, because the kernel can
schedule only one thread at a time.
• The one-to-one model allows greater concurrency, but
the developer has to be careful not to create too many
threads within an application.
Cont..
Comparison
Basis
Process Thread
Definition
A process is a program under execution i.e
an active program.
A thread is a lightweight process that can
be managed independently by a scheduler.
Context
switching time
Processes require more time for context
switching as they are more heavy.
Threads require less time for context
switching as they are lighter than
processes.
Memory Sharing
Processes are totally independent and
don’t share memory.
A thread may share some memory with its
peer threads.
Communication
Communication between processes
requires more time than between threads.
Communication between threads requires
less time than between processes .
Blocked
If a process gets blocked, remaining
processes can continue execution.
If a user level thread gets blocked, all of its
peer threads also get blocked.
Resource
Consumption
Processes require more resources than
threads.
Threads generally need less resources than
processes.
Dependency
Individual processes are independent of
each other.
Threads are parts of a process and so are
dependent.
Data and Code
sharing
Processes have independent data and code
segments.
A thread shares the data segment, code
segment, files etc. with its peer threads.
Treatment by OS
All the different processes are treated
separately by the operating system.
All user level peer threads are treated as a
single task by the operating system.
Time for
creation
Processes require more time for creation. Threads require less time for creation.
Time for
termination
Processes require more time for
termination.
Threads require less time for termination.
Unit 2_OS process management

More Related Content

PPTX
Distributed operating system
PPTX
Distributed database management system
DOCX
Levels of Virtualization.docx
PPTX
Unit 1 polynomial manipulation
PPTX
contiguous memory allocation.pptx
PDF
Hashing and Hash Tables
PPTX
Control Strategies in AI
Distributed operating system
Distributed database management system
Levels of Virtualization.docx
Unit 1 polynomial manipulation
contiguous memory allocation.pptx
Hashing and Hash Tables
Control Strategies in AI

What's hot (20)

ODP
Introduction To Data Warehousing
PPTX
Demand paging
PPTX
Associative Memory in Computer architecture
PPTX
OLAP & DATA WAREHOUSE
PPT
Star schema PPT
PPTX
Hash table
PPTX
Process synchronization in Operating Systems
PPTX
Advanced Processor Power Point Presentation
PDF
parallel Questions &amp; answers
PPTX
Importance of Normalization
PDF
Query optimization in SQL
PPTX
Demand paging
PPTX
Multi processor scheduling
PPTX
MapReduce Programming Model
PPTX
Multiprogramming&timesharing
PDF
Use of data science in recommendation system
PPTX
Asynchronous data transfer
PPT
40 demand paging
PPTX
Dichotomy of parallel computing platforms
Introduction To Data Warehousing
Demand paging
Associative Memory in Computer architecture
OLAP & DATA WAREHOUSE
Star schema PPT
Hash table
Process synchronization in Operating Systems
Advanced Processor Power Point Presentation
parallel Questions &amp; answers
Importance of Normalization
Query optimization in SQL
Demand paging
Multi processor scheduling
MapReduce Programming Model
Multiprogramming&timesharing
Use of data science in recommendation system
Asynchronous data transfer
40 demand paging
Dichotomy of parallel computing platforms
Ad

Similar to Unit 2_OS process management (20)

PPTX
UNIT I-Processes.pptx
PPTX
CPU Scheduling Criteria CPU Scheduling Criteria (1).pptx
PDF
Process management- This ppt contains all required information regarding oper...
PDF
Module 2 - PPT.pdfModule 2 - PPT.pdfModule 2 - PPT.pdfModule 2 - PPT.pdfModul...
PPTX
UNIT 2 OS.pptx Introduction of Operating System
PPTX
Os unit 3 , process management
PDF
Engg-0505-IT-Operating-Systems-2nd-year.pdf
PPTX
How Operating system works.
PPTX
Operating Systems chap 2_updated2.pptx
PPTX
Process management1
PPTX
PROCESS.pptx
PPTX
Operating Systems chap 2_updated2 (1).pptx
PDF
Operating System-Concepts of Process
PPTX
Evolution, Strutcture and Operations.pptx
PDF
Lecture 2- Processes.pdf
PPTX
AOS_Module_4ssssssssssssssssssssssss.pptx
PPTX
Lecture 4 process cpu scheduling
PPTX
Operating system 28 fundamental of scheduling
PDF
operating systems hybrid notes for computerscience.pdf
PDF
Operating Systems PPT 1 (1).pdf
UNIT I-Processes.pptx
CPU Scheduling Criteria CPU Scheduling Criteria (1).pptx
Process management- This ppt contains all required information regarding oper...
Module 2 - PPT.pdfModule 2 - PPT.pdfModule 2 - PPT.pdfModule 2 - PPT.pdfModul...
UNIT 2 OS.pptx Introduction of Operating System
Os unit 3 , process management
Engg-0505-IT-Operating-Systems-2nd-year.pdf
How Operating system works.
Operating Systems chap 2_updated2.pptx
Process management1
PROCESS.pptx
Operating Systems chap 2_updated2 (1).pptx
Operating System-Concepts of Process
Evolution, Strutcture and Operations.pptx
Lecture 2- Processes.pdf
AOS_Module_4ssssssssssssssssssssssss.pptx
Lecture 4 process cpu scheduling
Operating system 28 fundamental of scheduling
operating systems hybrid notes for computerscience.pdf
Operating Systems PPT 1 (1).pdf
Ad

More from JayeshGadhave1 (16)

PPTX
production-170629054926 (1) production system
PPTX
Threat intelligence life cycle steps by steps
PPTX
C_02_BME_34_41_50_57_60 non contact temprature detection
PPTX
autocollometer Mechanical engineering project
PPTX
74351a41-b6ff-4739-99d9-ff5ad260914c.pptx
PPTX
Data modelling it's process and examples
PPTX
Templet for digital poster-Sec-D.pptx
PPTX
Regression
PPTX
productlifecycle
PPTX
Process cooperation and synchronisation
PPT
Lisa_Chang.ppt
PPT
Unit 1_Operating system
PDF
Peizo electric effect
PDF
pptonboilers.pdf
PPTX
Sorting techniques
PDF
cryptography.pdf
production-170629054926 (1) production system
Threat intelligence life cycle steps by steps
C_02_BME_34_41_50_57_60 non contact temprature detection
autocollometer Mechanical engineering project
74351a41-b6ff-4739-99d9-ff5ad260914c.pptx
Data modelling it's process and examples
Templet for digital poster-Sec-D.pptx
Regression
productlifecycle
Process cooperation and synchronisation
Lisa_Chang.ppt
Unit 1_Operating system
Peizo electric effect
pptonboilers.pdf
Sorting techniques
cryptography.pdf

Recently uploaded (20)

PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PPTX
Lecture Notes Electrical Wiring System Components
PPT
Project quality management in manufacturing
PPTX
Internet of Things (IOT) - A guide to understanding
PPTX
Welding lecture in detail for understanding
PPTX
web development for engineering and engineering
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PDF
Well-logging-methods_new................
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPTX
OOP with Java - Java Introduction (Basics)
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
Construction Project Organization Group 2.pptx
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
UNIT 4 Total Quality Management .pptx
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Automation-in-Manufacturing-Chapter-Introduction.pdf
Lecture Notes Electrical Wiring System Components
Project quality management in manufacturing
Internet of Things (IOT) - A guide to understanding
Welding lecture in detail for understanding
web development for engineering and engineering
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
Well-logging-methods_new................
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
OOP with Java - Java Introduction (Basics)
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Construction Project Organization Group 2.pptx
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
CYBER-CRIMES AND SECURITY A guide to understanding
UNIT 4 Total Quality Management .pptx
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...

Unit 2_OS process management

  • 2. Syllabus • Process Management: Process concept • Process control block • Types of scheduler • Context switch • Multithreading model • Goals of scheduling and • Different scheduling algorithms.
  • 3. Process Management • A Program does nothing unless its instructions are executed by a CPU. • A program in execution is called a process. • In order to accomplish its task, process needs the computer resources. • There may exist more than one process in the system which may require the same resource at the same time.
  • 4. Cont.. • Therefore, the operating system has to manage all the processes and the resources in a convenient and efficient way. • Some resources may need to be executed by one process at one time to maintain the consistency otherwise the system can become inconsistent and deadlock may occur.
  • 5. Cont.. • The operating system is responsible for the following activities in connection with Process Management • Scheduling processes and threads on the CPUs. • Creating and deleting both user and system processes. • Suspending and resuming processes. • Providing mechanisms for process synchronization. • Providing mechanisms for process communication.
  • 6. Program vs Process • A process is a program in execution. • For example, when we write a program in C or C++ and compile it, the compiler creates binary code. • The original code and binary code are both programs. • When we actually run the binary code, it becomes a process. • A process is an ‘active’ entity, as opposed to a program, which is considered to be a ‘passive’ entity. • A single program can create many processes when run multiple times; for example, when we open a.exe or binary file multiple times, multiple instances begin (multiple processes are created).
  • 7. Whatdoes aprocesslooklikeinmemory?  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
  • 8. Cont.. Text section: • This section consist of source code. • It also includes the current activity as represented by value of program counter and the content of processor’s register. Stack section: • This section consist of temporary data such as function, parameters, return addresses and local variables. • Stack is used for local variables, space on the stack is reserved for local variables when they are declared. Space is freed up when variables go out of scope. Data Section: • This section stores global and static variables, which needs to be allocated and initialized prior to executing main program. Heap Section: • It is used for dynamic memory allocation and is managed via calls to new, delete malloc, free etc.
  • 9. Cont.. • Program • A program is a piece of code which may be a single line or millions of lines. • A computer program is usually written by a computer programmer in a programming language. For example, here is a simple program written in C programming language.
  • 10. Cont… • A computer program is a collection of instructions that performs a specific task when executed by a computer. • When we compare a program with a process, we can conclude that a process is a dynamic instance of a computer program. • A part of a computer program that performs a well-defined task is known as an algorithm. • A collection of computer programs, libraries and related data are referred to as a software.
  • 13. ProcessControlBlock(PCB) • Process Control Block (PCB, also called Task Controlling Block, Entry of the Process Table, Task Struct, or Switchframe) is a data structure in the operating system • OS kernel containing the information needed to manage the scheduling of a particular process. • The PCB is "the manifestation of a process in an operating system."
  • 14. Cont.. • A Process Control Block is a data structure maintained by the Operating System for every process. The PCB is identified by an integer process ID (PID).
  • 15. Cont.. • 1. Process ID • When a process is created, a unique id is assigned to the process which is used for unique identification of the process in the system. • 2. Program counter • A program counter stores the address of the last instruction of the process on which the process was suspended. • The CPU uses this address when the execution of this process is resumed.
  • 16. Cont.. • 3. Process State • The Process, from its creation to the completion, goes through various states which are new, ready, running and waiting. • 4. Priority • Every process has its own priority. The process with the highest priority among the processes gets the CPU first. This is also stored on the process control block.
  • 17. Cont.. • CPU registers • Various CPU registers where process need to be stored for execution for running state. • CPU Scheduling Information • Process priority and other scheduling information which is required to schedule the process.
  • 18. Cont.. • Memory management information • This includes the information of page table, memory limits, Segment table depending on memory used by the operating system. • Accounting information • This includes the amount of CPU used for process execution, time limits, execution ID etc. • IO status information • This includes a list of I/O devices allocated to the process.
  • 19. ProcessScheduling • Process scheduling is a task of operating system to schedules the processes of different states like ready, running, waiting. • This task is very useful in maintain the computer system. • Process scheduling allocates the time interval of each process in which the process is to be executed by the central processing unit (CPU).
  • 20. Cont.. • The act of determining which process is in the ready state, and should be moved to the running state is known as Process Scheduling. • The prime aim of the process scheduling system is to keep the CPU busy all the time and to deliver minimum response time for all programs. • The scheduler must apply appropriate rules for swapping processes IN and OUT of CPU.
  • 21. WhatareSchedulingQueues? • All processes when enters into the system are stored in the job queue. • Processes in the Ready state are placed in the ready queue. • Processes waiting for a device to become available are placed in device queues.
  • 22. Cont.. • The process could issue an I/O request, and then be placed in an I/O queue. • The process could create a new subprocess and wait for its termination. • The process could be removed forcibly from the CPU, as a result of an interrupt, and be put back in the ready queue.
  • 24. Cont.. • A new process is initially put in the Ready queue. It waits in the ready queue until it is selected for execution(or dispatched). • Once the process is assigned to the CPU and is executing, one of the following several events can occur: • The process could issue an I/O request, and then be placed in the I/O queue. • The process could create a new subprocess and wait for its termination. • The process could be removed forcibly from the CPU, as a result of an interrupt, and be put back in the ready queue.
  • 26. Cont.. • An operating system uses two types of scheduling processes execution, preemptive and non - preemptive. • 1. Preemptive process: In preemptive scheduling policy, a low priority process has to be suspend its execution if high priority process is waiting in the same queue for its execution. • 2. Non - Preemptive process: In non - preemptive scheduling policy, processes are executed in first come first serve basis, which means the next process is executed only when currently running process finishes its execution.
  • 27. TypesofSchedulers • There are three types of schedulers available: • Long Term Scheduler • Short Term Scheduler • Medium Term Scheduler
  • 29. Long-termScheduler • Long-term scheduler is also known as Job Scheduler. • It selects a balanced mix of I/O bound and CPU bound processes from the secondary memory (new state). • Then, it loads the selected processes into the main memory (ready state) for execution. • It brings the new process to the ‘Ready State’. • It controls Degree of Multi-programming, i.e., number of process present in ready state at any point of time. • It is important that the long-term scheduler make a careful selection of both IO and CPU bound process.
  • 30. Short-termScheduler • Short-term scheduler is also known as CPU Scheduler. • It decides which process to execute next from the ready queue. • After short-term scheduler decides the process, Dispatcher assigns the decided process to the CPU for execution.
  • 31. Cont.. • It is responsible for selecting one process from ready state for scheduling it on the running state. • Dispatcher is responsible for loading the process selected by Short-term scheduler on the CPU (Ready to Running State) • Context switching is done by dispatcher only. • A dispatcher does the following: • Switching context. • Switching to user mode. • Jumping to the proper location in the newly loaded program.
  • 32. Medium-termScheduler • Medium-term scheduler swaps-out the processes from main memory to secondary memory to free up the main memory when required. • Thus, medium-term scheduler reduces the degree of multiprogramming. • After some time when main memory becomes available, medium- term scheduler swaps-in the swapped-out process to the main memory and its execution is resumed from where it left off. • Swapping may also be required to improve the process mix.
  • 34. Cont.. • The medium-term scheduler swaps out a process from main memory. • It can again swap in the process later from the point it stopped executing. • This can also be called as suspending and resuming the process. • This is helpful in reducing the degree of multiprogramming. Swapping is also useful to improve the mix of I/O bound and CPU bound processes in the memory.
  • 35. Long-term scheduler Medium-term scheduler Short-term scheduler It is a job scheduler It is a process swapping scheduler. It is a CPU scheduler It controls the degree of multiprogramming. It reduces the degree of multiprogramming. It provides lesser control over degree of multiprogramming. Speed is lesser than short- term scheduler Speed is in between the long-term and short-term schedulers. Speed is fastest among the other two. It is minimal or almost absent in time sharing system. It is a part of time sharing system. It is also minimal in time sharing system. It selects processes from new state and loads them into ready state. It swaps-out processes from main memory to secondary memory and later swaps in. It selects processes from the ready state and assigns to the CPU. Operates less frequently since processes are not rapidly created. Operates more frequently than long-term scheduler but less frequently than short-term scheduler. Operates very frequently to match the speed of CPU since CPU rapidly switches from one process to another
  • 36. Context Switch • A context switch occurs when a computer's CPU switches from one process or thread to a different process or thread. • Context switching allows for one CPU to handle numerous processes or threads without the need for additional processors. • Any operating system that allows for multitasking relies heavily on the use of context switching to allow different processes to run at the same time.
  • 39. Cont.. • There are three situations that a context switch is necessary: • Multitasking - When the CPU needs to switch processes in and out of memory, so that more than one process can be running. • Kernel/User Switch - When switching between user mode to kernel mode, it may be used (but isn't always necessary). • Interrupts - When the CPU is interrupted to return data from a disk read.
  • 40. StepsofContextSwitch • The steps in a full process switch are: • Save the context of the processor, including program counter and other registers. • Update the process control block of the process that is currently in the Running state. This includes changing the state of the process to one of the other states (Ready; Blocked; Ready/Suspend; or Exit). • Move the process control block of this process to the appropriate queue (Ready; Blocked on Event i ; Ready/Suspend).
  • 41. Cont.. • Select another process for execution. • Update the process control block of the process selected. This includes changing the state of this process to Running. • Update memory management data structures. This includes information of page table, memory limits, Segment table depending on memory used by the operating system • Restore the context of the processor to that which existed at the time the selected process was last switched out of the Running state, by loading in the previous values of the program counter and other registers.
  • 42. fork()inC • Fork system call is used for creating a new process, which is called child process, which runs concurrently with the process that makes the fork() call (parent process). • After a new child process is created, both processes will execute the next instruction following the fork() system call. • A child process uses the same pc(program counter), same CPU registers, same open files which use in the parent process.
  • 43. Cont.. • It takes no parameters and it returns an integer value • Below are different values returned by fork(). • Negative Value: creation of a child process was unsuccessful. • Zero: Returned to the newly created child process. • Positive value: Returned to parent or caller. The value contains process ID of newly created child process.
  • 44. Example: #include <stdio.h> #include <sys/types.h> #include <unistd.h> int main() { // make two process which run same // program after this instruction fork(); printf("Hello world!n"); return 0; } • OutPut: Hello world! • Hello world!
  • 45. Cont.. #include <stdio.h> #include <sys/types.h> int main() { fork(); fork(); fork(); printf("hellon"); return 0; }
  • 47. Cont.. #include <stdio.h> #include <sys/types.h> #include <unistd.h> void forkexample() { // child process because return value zero if (fork() == 0) printf("Hello from Child!n"); // parent process because return value non-zero. else printf("Hello from Parent!n"); } int main() { forkexample(); return 0; }
  • 48. MultithreadingModel • Multithreading allows the execution of multiple parts of a program at the same time. • These parts are known as threads and are lightweight processes available within the process. • Therefore, multithreading leads to maximum utilization of the CPU by multitasking.
  • 49. Cont.. • What are Threads? • Thread is an execution unit which consists of its own program counter, a stack, and a set of registers. • Threads are also known as Lightweight processes. • Threads are popular way to improve application through parallelism. • The CPU switches rapidly back and forth among the threads giving illusion that the threads are running in parallel.
  • 50. Cont.. As each thread has its own independent resource for process execution, multiple processes can be executed parallely by increasing number of threads.
  • 51. Thread  A thread is a lightweight process that can be managed independently by a scheduler  Threads are parts of a process and so are dependent.  A thread shares the data segment, code segment, files etc. with its peer threads. • There are two types of threads: • User Threads • Kernel Threads
  • 52. Cont.. • User threads, are above the kernel and without kernel support. These are the threads that application programmers use in their programs. • Kernel threads are supported within the kernel of the OS itself. All modern OSs support kernel level threads, allowing the kernel to perform multiple simultaneous tasks and/or to service multiple kernel system calls simultaneously.
  • 53. Multithreading Models • The user threads must be mapped to kernel threads, by one of the following strategies: • Many to One Model • One to One Model • Many to Many Model
  • 54. Many-to-OneModel • The many-to-one model maps many user- level threads to one kernel thread. • Thread management is done by the thread library in user space, so it is efficient. • However, the entire process will block if a thread makes a blocking system call. • Also, because only one thread can access the kernel at a time, multiple threads are unable to run in parallel on multicore systems
  • 55. One-to-One Model • The one-to-one model maps each user thread to a kernel thread. • It provides more concurrency than the many-to-one model by allowing another thread to run when a thread makes a blocking system call. • It also allows multiple threads to run in parallel on multiprocessors. • The only drawback to this model is that creating a user thread requires creating the corresponding kernel thread. • Because the overhead of creating kernel threads can burden the performance of an application, most implementations of this model restrict the number of threads supported by the system. • Linux, along with the family of Windows operating systems, implement the one-to-one model.
  • 56. Cont.. • In this model, one to one relationship between kernel and user thread. • In this model multiple thread can run on multiple processor. Problem with this model is that creating a user thread requires the corresponding kernel thread.
  • 57. Many-to-Many Model • The many-to-many model multiplexes many user-level threads to a smaller or equal number of kernel threads. • The number of kernel threads may be specific to either a particular application or a particular machine • The many to many does not have the disadvantages of the one to one model or the many to one model. • There can be as many user threads as required and their corresponding kernel threads can run in parallel on a multiprocessor. • Whereas the many to-one model allows the developer to create as many user threads as she wishes, it does not result in true concurrency, because the kernel can schedule only one thread at a time. • The one-to-one model allows greater concurrency, but the developer has to be careful not to create too many threads within an application.
  • 59. Comparison Basis Process Thread Definition A process is a program under execution i.e an active program. A thread is a lightweight process that can be managed independently by a scheduler. Context switching time Processes require more time for context switching as they are more heavy. Threads require less time for context switching as they are lighter than processes. Memory Sharing Processes are totally independent and don’t share memory. A thread may share some memory with its peer threads. Communication Communication between processes requires more time than between threads. Communication between threads requires less time than between processes . Blocked If a process gets blocked, remaining processes can continue execution. If a user level thread gets blocked, all of its peer threads also get blocked. Resource Consumption Processes require more resources than threads. Threads generally need less resources than processes. Dependency Individual processes are independent of each other. Threads are parts of a process and so are dependent. Data and Code sharing Processes have independent data and code segments. A thread shares the data segment, code segment, files etc. with its peer threads. Treatment by OS All the different processes are treated separately by the operating system. All user level peer threads are treated as a single task by the operating system. Time for creation Processes require more time for creation. Threads require less time for creation. Time for termination Processes require more time for termination. Threads require less time for termination.