0% found this document useful (0 votes)
3 views41 pages

unit3

Uploaded by

sanapanuj7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views41 pages

unit3

Uploaded by

sanapanuj7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

UNIT-III 3.

1 PROCESS
PROCESS STATES PROCESS CONTROL BLOCK
PROCESS 3.2 PROCESS SCHEDULING
MANAGEMENT SCHEDULING QUEUES, SCHEDULERS, CONTEXT
SWITCH
( 14 Marks) 3.3 INTER-PROCESS COMMUNICATION (IPC)
INTRODUCTION, MESSAGE PASSING SYSTEM AND
SHARED MEMORY SYSTEM
3.4 THREADS: BENEFITS, USERS AND KERNEL
THREADS,
MULTITHREADING MODELS- MANY TO ONE, ONE TO
ONE, MANY TO MANY
3.5 EXECUTE PROCESS COMMANDS:
ps, wait, sleep, kill, exit
Process
 A process is basically a program in
execution.
 A process is defined as an entity which

represents the basic unit of work to be


implemented in the system.
 When a program is loaded into the
memory and it becomes a process.
Process
 When a program is loaded into the memory and it becomes
a process, it can be divided into four sections ─ stack, heap,
data and text. 1. Stack
The process Stack contains the
temporary data such as
method/function parameters, return
address and local variables.
2. Heap
This is dynamically allocated memory to a
process during its run time.
3. Data
This section contains the global and static
variables.
4. Text
This includes the current activity represented
Simplified layout of a process inside main memory by the value of Program Counter and the
contents of the processor's registers.
Process State Block Diagram
A process state is a condition of the process at a specific instant of time
Different process states are
 New

 The process is in the stage of being


Exit created.
NEW Terminated
Interrupt  READY
Admitted
 The process has all the resources
available that it needs to run, but
READY Running the CPU is not currently working
on this process's instructions.
 RUNNING
Scheduler Dispatch
 The CPU is working on this
I/O or Event
Waiting I/O or process's instructions.
Completion Event
 WAITING / BLOCKED
Blocked Wait
 Process moves into the waiting
state if it needs to wait for a
 TERMINATED
resource, such as waiting for user
 The process has completed. It is
input, or waiting for a file to
then removed from memory
become available
PROCESS CONTROL BLOCK (PCB)
Each process is represented as a process control block (PCB) in the operating system. It contains
information associated with specific process.
Process State: It indicates current state of a process. Process state can be
new, ready, running, waiting and terminated.
Process ID: Each process is associated with a unique number which
is known process identification number.
ID
Program Counter: It indicates the address of the next instruction to
be executed for the process.
CPU Registers: The registers vary in number and type depending on
the computer architecture. Register includes accumulators, index
registers, stack pointers and general purpose registers plus any
condition code information e.g. flags.
Memory Management Information: It includes information such as value of
base and limit registers, page tables, segment tables, depending on the
memory system used by OS
Accounting Information: This information includes the amount of CPU used,
time limits, account owners, job or process number and so on. It also includes
information about listed I/O devices allocated to the process such as list of
open files.
PROCESS SCHEDULING

• Process Scheduling is an OS task that selects process out


of multiple processes of different states like ready, waiting,
and running.

• Process scheduling allows OS to allocate a time interval of


CPU execution for each process.

• Another important reason for using a process scheduling


system is that it keeps the CPU busy all the time.
• It allows you to get the minimum response time for programs.
Scheduling queues
The Operating System maintains the following important process
scheduling queues −
1. Job queue − This queue keeps all the processes currently
available in the system.
2. Ready queue − This queue keeps a set of all processes residing
in main memory, ready and waiting to execute by CPU.
3. Device queues − The processes which are blocked due to
unavailability of an I/O device are placed in this queue waiting for
device..
Scheduling queues

Device Queue /
Blocked Queue
Schedulers
Definition –
• A process migrates between the various scheduling queues
throughout its life time. The operating system must select processes
from these queues by applying certain criteria.
The selection process is carried out by the appropriate scheduler.
• Scheduler is the system program which schedules processes from
the scheduling queues.

Types of Schedulers:
1. Long Term Scheduler
2. Short Term Scheduler
3. Medium Term Scheduler
Long Term Scheduler
 It is also called job scheduler.
 Long term scheduler determines which programs are admitted to
the system for processing.
 Job scheduler selects processes from the queue and loads them
into memory for execution. Process loads into the memory for CPU
scheduling.
 The primary objective of the job scheduler is to provide a balanced
mix of jobs, such as I/O bound and processor bound.
 It also controls the degree of multiprogramming.
 Time-sharing operating systems have no long term scheduler.
 When process changes the state from new to ready, then there is
use of long term scheduler.
Short Term Scheduler (CPU Scheduler)
 It is also called CPU scheduler.
 It is the change of ready state to running state of the

process.
 CPU scheduler selects process among the processes

that are ready to execute and allocates CPU to one


of them.
 Short term scheduler also known as dispatcher,
execute most frequently and makes the fine grained
decision of which process to execute next.
 Short term scheduler is faster than long term

scheduler.
Medium Term Scheduler
(Swapping Scheduler)
 Medium term scheduling is part of the swapping.
 It removes the processes from the memory.

 It reduces the degree of multiprogramming.

 The medium term scheduler is in-charge of handling

the swapped out-processes.


 Running process may become suspended if it makes an

I/O request. Suspended processes cannot make any


progress towards completion.
 In this condition, to remove the process from memory

and make space for other process, the suspended


process is moved to the secondary storage.
 This process is called swapping, and the process is
said to be swapped out or rolled out.
 Swapping may be necessary to improve the process
mix.
Comparison
Long-Term Scheduler Short-Term Scheduler Medium-Term Scheduler
Long term is also known as a Short term is also known as Medium-term is also called
job scheduler CPU scheduler swapping scheduler.
It is either absent or minimal It is insignificant in the time- This scheduler is an element
in a time-sharing system. sharing order. of Time-sharing systems.

Speed is less compared to Speed is the fastest It offers medium speed.


the short term scheduler. compared to the short-term
and medium-term scheduler.

Allow you to select It only selects processes that It helps you to send process
processes from the loads and is in a ready state of the back to memory.
pool back into the memory execution.

Offers full control Offers less control Reduce the level of


multiprogramming.
SUMMARY
•Process scheduling is an OS task that schedules the processes of different states
like ready, waiting, and running.
•Process scheduling maximizes the number of interactive users, within
acceptable response times.
•A scheduler is a type of system software that allows you to handle process
scheduling.
•Three types of the scheduler are 1) Long term 2) Short term 3) Medium-term
•Long term scheduler regulates the program and select process from the queue
and loads them into memory for execution.
•The medium-term scheduler enables you to handle the swapped out-processes.
•The main goal of short term scheduler is to boost the system performance
according to set criteria
•Long term is also known as a job scheduler, whereas the short term is also
known as CPU scheduler, and the medium-term is also called swapping
scheduler.
CONTEXT SWITCH
Context Switch
• When CPU switches from current process to another process, the
system saves the state of the old process and loads the saved
state for the new process. This is called context switch.
• Context of a process is represented in the PCB.
• Context-switch time is overhead; the system does no useful work
while switching
• Time dependent on hardware support
.
Steps of Context Switch
1. Save context of processor
including program counter and
other registers.
2. Update the PCB of the running
process with its new state and
other associate information.
3. Move PCB to appropriate
queue – ready, device
(blocked),
4. Select another process for
execution.
5. Update PCB of the selected
process.
6. Restore CPU context from that
of the selected process
Inter process communication

 Independent process cannot affect or be P1 P2


affected by the execution of another process
 Cooperating process can affect or be affected
by the execution of another process
 Advantages of process cooperation
 Information sharing P1 P2
 Computation speed-up
 Modularity

 Convenience
Inter process communication

 Cooperating processes require an Inter-process


communication (IPC) mechanism that will allow them to
exchange data and information.

 Two models of IPC


 Shared memory
 Message passing
Models of IPC

a. Message passing: small amount of data, easier to


implement
b. Shared memory: allow maximum speed,
convenience of communication.
1. Message Passing
In this model, communication takes place by exchanging
messages between cooperating processes.
It allows processes to communicate and synchronize their
action without sharing the same address space.
 It is particularly useful in a distributed environment when
communication process may reside on a different computer
connected by a network.
Communication requires sending and receiving messages
through the kernel.
The processes that want to communicate with each other
must have a communication link between them. Between
each pair of processes exactly one communication link is
there.
2. Shared Memory
 Interprocess communication using shared memory requires
communicating processes to establish a region of shared
memory.
 Typically a shared memory region resides in the address
space of the process creating the shared memory segment Region
 Other processes that wish to communicate using this shared-
memory segment must attach it to their address space.
 All the processes can exchange information by reading and/or
writing data in shared memory segment.
 These processes are not under the control of the operating
system.
 The processes are responsible for ensuring that they are not
writing to the same location simultaneously.
 After establishing shared memory segment, all accesses to the
shared memory segment are treated as routine memory access
and without assistance of kernel.
CONCLUSION
• Two Types of Processes : Independent & Cooperating Processes
• Cooperating processes require an Inter-process communication
(IPC) mechanism that will allow them to exchange data and
information.
• Two models of IPC

Shared memory
Message passing
• Message passing: Small amount of data, Easier to
implement
• Shared memory: Allow maximum speed, Convenience of
communication
Threads
A thread is called a lightweight process
It has separate path of execution
 A Single process contains many threads
thread
 Threads are executed simultaneously

Multithreading:
• It refers to the ability of an O.S to support multiple threads
of execution within asingle process.
• In a multi-threaded environment multiple processes and
multiple threads can be considered as in case of multiuser
O.S. such as UNIX.
 E.g. Web Browser, Gmail etc.
Single thread vs Multithreads
Benefits of thread

 Responsiveness- Interactive Application

 Resource Sharing- Share resources of process

 Economy- Cost effective as compared to process

 Full CPU Utilization- Threads run in parallel


Types of thread
Operating System managed threads acting on kernel,
User managed threads.
an operating system core.

User Threads
User Threads

Threads User
User space
Library space
Kernel
Kernel space
space

Kernel Threads
P

(a) Pure user-level (b) Pure kernel-level


Types of thread
User Level Threads
• Implemented by user and
managed by run-time system User Threads
(user-level library).
• OS is not aware of existence of
threads. Threads User
• OS manages the user level Library space

threads as if they are single Kernel


space
threaded process.
• OS doesn’t need to interfere,
everything can be without it. P

• They do not modify OS.


• Threads are cheap and fast at
user level.
Types of thread
 Kernel Thread
User Threads

• OS managed threads.
• OS knows about and manages the thread.
User
• Kernel has a thread table which keeps tracks space
of all threads in the system. Kernel
space
• System calls create and manage threads.
Kernel Threads
• Slow and inefficient than user level.
• Hardware support is needed.
• Context switching time is more. Eg – P
Windows, Solaris etc.
Multithreading model

Many systems provide support for both user and kernel threads, resulting in
different multithreading models.
Following are three multithreading model:

 Many to One
 One to One

 Many to Many
Many to One Multithreading model
• In the many-to-one model, many user-level
User Thread
threads are all mapped onto a single kernel
thread.
• Thread management is handled by the
thread library in user space, which is very
efficient.
• The entire process will block if a thread
makes a blocking system call.
• Because a single kernel thread can
access the kernel at a time, multiple
threads are unable to run in parallel on
multiple CPUs.
K Kernel Thread
• e.g. Green threads for Solaris ,GNU
Portable Threads.
One to One Multithreading model
 The one-to-one model creates a separate
kernel thread to handle each user thread.
• Provides more concurrency than many-to-
one model by allowing another thread to run
when a thread makes a blocking call
• Also allows multiple threads to run in parallel User
Thread
across multiple CPUs.
• However the overhead of managing the
one-to-one model is more significant,
involving more overhead and slowing K K K K Kernel
down the system. Thread
• Most implementations of this model place
a limit on how many threads can be
created.
• Eg Linux and Windows NT/XP/2000.
Many to Many Multithreading model
 The many-to-many model multiplexes
many user-level threads to a smaller
or equal number of kernel threads. User Thread

 The number of kernel threads may be


specific to either a particular
application or a particular machine
User Thread
 Developers can create as many user
threads as necessary, and the
corresponding kernel threads can run
in parallel on multiprocessors.
 Also, when a thread performs a
blocking system call, the kernel can K K Kernel Thread
K
schedule another thread for execution
CONCLUSION
• There are two types of Threads:
User Level & Kernel Level Thread
• There are 3 types of Multi-Threading Models:
• Many to One- Many user-level threads to
one kernel thread.
• One to One - Each user thread to a kernel
thread.
• Many to Many- Many user-level threads to
many kernel thread
PROCESS COMMANDS
• Process is a program in execution.
• Process commands can be executed to perform different operations
related to processes.
• There are following 5 process commands:
• 1. $ps – Process Status Display the characteristics of a process (
i.e terminal number, PID No and command name)
• 2. $wait: Waits until all background processes are completed and
then exits.
• 3. $sleep Used to execute commands after certain amount of time
by sleeping for given seconds.
• 4. $kill Used to stop execution of particular process.
• 5. $exit: Used to exit the shell
ps (Process Status)
• It displays the characteristics of a process (i.e terminal number, PID
No etc.)

• The options of ps commands are:


• ps –f Full listing showing PPID of each process

• ps –u username Displays processes of user username

• ps –a Processes of all users

• ps -e Processes including user and system processes.


wait Command
• This command waits until all background processes are completed
and then exits
• Eg wait 1215 Background process

process id of the last


running process

read the status value of


wait command.
Wait Command
• This command waits until all background processes are completed
and then exits
• Eg wait 1215
sleep command

• sleep
• It is used to execute commands after certain
amount of time by sleeping for given seconds

• Eg
sleep 30
kill command

• kill
• The kill command can kill a process given its
process ID.

• Eg kill 1000
exit command
• exit
• The exit command is used to exit the shell where it is presently
running.
• Eg exit

You might also like