You are on page 1of 51

Operating Systems

Unit II : Process Management & CPU Scheduling

Prof.G.Apparao
Professor
Department of CSE
GITAM Institute of Technology (GIT)
Visakhapatnam – 530045
Email: agidutur@gitam.edu
19ECS204: OPERATING SYSTEMS - Syllabus
Process Management: Process concepts, process scheduling, Operations on processes, Interprocess
communication
CPU Scheduling: Multithreaded programming, Multi-core Programming, Multi-threading Models ,
Scheduling-criteria, scheduling algorithms, algorithm evaluation, Multiple processor scheduling,
algorithm evaluation
Learning Outcomes:
• After completion of this unit, You will be able to

1. demonstrate the concepts of Process, thread and CPU scheduling(L2)


2. list out different scheduling algorithms(L1)
3. analyze scheduling algorithms with different examples (L4)
Process Management
Process Management:
• A process can be thought of as a program in execution
• It needs certain resources like CPU cycles, memory, files and i/o
devices to accomplish its task.
• These resources can be allocated at the time of creation or while it is
executing
• The OS takes the responsibility of creation, deletion, scheduling,
synchronizing, communicating and handling deadlocks for both
processes and threads.
1. Process
• 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. Process in Memory
Process (Cont.)
• Program is passive entity stored on secondary storage disk (executable file), process is
active
• Program becomes process when executable file loaded into memory(Main)

• 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


2. Process States
• As a process executes, it changes state. The state defines the current activity of the process. When it is
under execution it will be in one of following the states:
• new: The process is being created. A program which is going to be picked up by the OS into the
main memory is called a new process.
• running: Instructions are being executed
• Blok/waiting: The process is waiting for some event to occur. When a process waits for a certain
resource to be assigned or for the input from the user then the OS move this process to the block
or wait state and assigns the CPU to the other processes.
• ready: The process is waiting to be assigned to a processor
• terminated: The process has finished execution. All the context of the process (Process Control
Block) will also be deleted the process will be terminated by the Operating system.
Note: at any time one process can be running on any processor and many processes may be in ready
and waiting
Process States
• Suspend ready
A process in the ready state, which is moved to secondary memory from the main
memory due to lack of the resources (mainly primary memory) is called in the
suspend ready state.
If the main memory is full and a higher priority process comes for the execution then
the OS have to make the room for the process in the main memory by throwing the
lower priority process out into the secondary memory. The suspend ready processes
remain in the secondary memory until the main memory gets available.
• Suspend wait
Instead of removing the process from the ready queue, it's better to remove the
blocked process which is waiting for some resources in the main memory. Since it is
already waiting for some resource to get available hence it is better if it waits in the
secondary memory and make room for the higher priority process. These processes
complete their execution once the main memory gets available and their wait is
finished.
3. Process Control Block (PCB)
Each process is represented in the OS by PCB (also called task
control block)
It contains many pieces of information associated with a specific
process
• Process state – new, ready, running, waiting, halted or
terminated etc
• Program counter – The counter indicates the address of the next
instruction to be executed for this process
• CPU registers – contents of all process-centric registers
• The registers vary in number and type, depending on the
computer architecture. They include accumulators, index
registers, stack pointers, and general-purpose registers, plus any
condition-code information.
• Along with the program counter, this state information must be
saved when an interrupt occurs, to allow the process to be
continued correctly afterward
Diagram of Process State

Department of CSE Operating Systems 9


Process Control Block (PCB)
• CPU scheduling information- process priorities, pointers to scheduling queues, and any other
scheduling parameters.
• Memory-management information – memory allocated to the process

• This information may include such items as the value of the base and limit registers and the
page tables, or the segment tables, depending on the memory system used by the operating
system
• Accounting information – CPU used, clock time elapsed since start, time limits, account
numbers, job or process numbers, and so on.
• I/O status information – I/O devices allocated to process, list of open files etc.

Note: PCB serves like a repository for any information that may vary from process to process.
Process Scheduling

• The objective of multiprogramming is to have some process running at all times, to


maximize CPU utilization.

• The objective of time sharing is to switch the CPU among processes so frequently that
users can interact with each program while it is running

• To meet these objectives , the Process scheduler selects a process from a set of several
process in memory for execution on the CPU.

• For a single-processor system, there will never be more than one running process.

• If there are more processes, the rest will have to wait until the CPU is free and can be
rescheduled.
Department of CSE Operating Systems 11
PROCESS SCHEDULING
1. scheduling queues of processes:

OS manages various types of queues for each process states. The PCB related to the process is also stored
in the Queue of the same state.

When processes are present in a state, they are represented in a Queue.All the processes are taken and
then whenever they move to a state, they will be added to a particular Queue and Queues are
implemented as Linked List.

Queueing diagram represents queues, resources, flows

Department of CSE Operating Systems 12


PROCESS SCHEDULING

1. scheduling queues of processes:

• Maintains scheduling queues of processes


1. Job Queue: As process enters the system, they are put into a job queue , which consists of all
processes in the system.(Before the processes are created ,they are put in a job queue). It is
maintained in the secondary memory. The long term scheduler (Job scheduler) picks some of the
jobs and put them in the primary memory.

2. Ready Queue: the processes that are residing in main memory and are ready and waiting to execute
are kept on a list called ready queue. This queue is generally stores a linked list. The short term
scheduler picks the job from the ready queue and dispatch to the CPU for the execution.

a) A ready queue header contains pointers to the first and final PCBs in the list.

b) Each PCB includes a pointer field that points to the next PCB in the ready queue.

3. System also contains other queues ,Department


when process
of CSE isOperating
allocatedSystemsCPU, it executes for a while and quits,
13 is
Ready Queue And Various I/O Device Queues
Example:
Suppose a process makes an i/o request to a
shared device, such as tape or disk and these
devices are busy with the i/o request of some
other process. So a process has to wait for these
devices.
Waiting Queue
When the process needs some IO operation in
order to complete its execution, OS changes the
state of the process from running to waiting. The
context (PCB) associated with the process gets
stored on the waiting queue which will be used by
the Processor when the process finishes the IO.

Device Queue: the list of processes waiting for a


particular I/O device is kept in a queue called
Device queue.
i.e., each device has its own device queue
Department of CSE Operating Systems 14
The common representation of process scheduling is a queuing diagram, Queueing diagram represents queues, resources, flows

1. Rectangular box represents a queue( a ready


queue and set of device queues) that serves
queues
2. Arrows indicate flow of processes in the
system
3. Circles represents resources.
Initially the process is kept in ready queue and it
waits until it is selected for execution or dispatched.
Once the process is allocated CPU and is executing,
one of the event may occur:
• The process could issue an I/O request and then be
placed in an I/O queue.

Note: A process continues this cycle until it
The process could create a new child process and wait
terminates, at which time it is removed from all
for the child’s termination.
queues and has its PCB and resources deallocated
• The process could be removed forcibly from the CPU,
Department of CSE Operating Systems 15
as a result of an interrupt, and be put back in the ready
Context Switch

• 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 not 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
Context Switch
• When an interrupt occurs, it causes the OS to change a CPU from its current task and to run a kernel routine.

• When an interrupt occurs, the system needs to save the current context(status) of the process running on the
CPU so that it can restore the context when its processing is done(suspended or resuming)

• This context is represented in the PCB of the process.

• Switching the CPU to another process requires saving the current process context in its PC and restoring the
context of another process. This task is called context switch.

• When a context switch occurs, the kernel saves the context of old process in its PCB and loads the saved
context of the new process scheduled to run.

• In this time the system does no useful work while switching.

• Context switch time are highly dependent on h/w support-memory speed,number of regs to be copied.

• Takes a few milliseconds


Context Switch: CPU Switch From Process to Process
2. Schedulers

• When a process is under execution, it migrates among the various scheduling queues
throughout its lifetime.

• The OS must select the process for scheduling of these queues in some order.

• The selection process is carried out by the appropriate scheduler

Department of CSE Operating Systems 19


2. Schedulers

• 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
Department of CSE Operating Systems 20
Operations on Processes

• System must provide mechanisms for:

• process creation

• process termination
Process Creation
• 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
A Tree of Processes in Linux

Process Creation (Cont.)


• Address space
• Child duplicate of parent(child process has same program and
data as of parent)
• Child has a new 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
Process Termination
• 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
Inter- Process Communication:
• Processes executing concurrently in the operating system may be either independent processes or cooperating
processes.
• A process is independent if it does not share data with any other processes executing in the system.
• A process is cooperating if it can affect or be affected by the other processes executing in the system. Clearly, any
process that shares data with other processes is a cooperating process.
• Reasons for providing an environment that allows process cooperation: 1. Information sharing, 2. Computation
speedup, 3. Modularity.
• Cooperating processes require an interprocess communication (IPC) mechanism that will allow them to exchange
data— that is, send data to and receive data from each other.
• There are two fundamental models of interprocess communication: 1. shared memory and 2. message passing.
• In the shared-memory model, a region of memory that is shared by the cooperating processes is established.
Processes can then exchange information by reading and writing data to the shared region.
• In the message-passing model, communication takes place by means of messages exchanged between the
cooperating processes.
• Message passing is useful for exchanging smaller amounts of data,
because no conflicts need be avoided. Message passing is also easier
to implement in a distributed system than shared memory.

• Shared memory can be faster than message passing, since message-


passing systems are typically implemented using system calls and
thus require the more time-consuming task of kernel intervention.

• In shared-memory systems, system calls are required only to


establish shared memory regions. Once shared memory is
established, all accesses are treated as routine memory accesses, and
no assistance from the kernel is required.
• Cooperating processes using shared memory technique:
• Example 1: A compiler is a process may produce an assembly code and a
another process called assembler consumes that code produced by compiler
process and produces object modules in turn consumed by another process
loader using the shared memory region.
• Example 2: Producer-Consumer problem:
• Cooperating processes using Message passing technique:
• This  technique allows a processes to communicate and synchronize their
actions without sharing the same address space. It is useful in distributed
environment, where communicating processes may reside on different
computers connected by a network.
• Example : Chat program is designed so that participants can communicate with
one another by exchanging messages using send(msg) and receive(msg) system
calls. 
IPC in Shared- Memory Systems:

Producer-Consumer Problem
• Paradigm for cooperating processes, producer process produces information
that is consumed by a consumer process
• unbounded-buffer places no practical limit on the size of the buffer

• bounded-buffer assumes that there is a fixed buffer size


Bounded-Buffer – Shared-Memory Solution

• Shared data
#define BUFFER_SIZE 10
typedef struct {
. . .
} item;

item buffer[BUFFER_SIZE];
int in = 0; //next free position
int out = 0; //first full position

• Solution is correct, but can only use BUFFER_SIZE-1 elements

Department of CSE Operating Systems 29


Bounded-Buffer – Producer

item next_produced;
while (true)
{
/* produce an item in next produced */
while (((in + 1) % BUFFER_SIZE) == out)
; /* do nothing */
buffer[in] = next_produced;
in = (in + 1) % BUFFER_SIZE;
}

Department of CSE Operating Systems 30


Bounded Buffer – Consumer
item next_consumed;
while (true) {
while (in == out)
; /* do nothing */
next_consumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;

/* consume the item in next consumed */


}

Department of CSE Operating Systems 31


Interprocess Communication – Shared Memory

• An area of memory shared among the processes that wish to communicate

• The communication is under the control of the users processes not the operating
system.

• Major issues is to provide mechanism that will allow the user processes to
synchronize their actions when they access shared memory.

Department of CSE Operating Systems 32


Interprocess Communication – Message Passing

• Mechanism for processes to communicate and to synchronize their actions

• Message system – processes communicate with each other without resorting to shared
variables

• IPC facility provides two operations:


• send(message)
• receive(message)

• The message size is either fixed or variable

Department of CSE Operating Systems 33


Message Passing (Cont.)

• If processes P and Q wish to communicate, they need to:


• Establish a communication link between them
• Exchange messages via send/receive
• Implementation issues:
• How are links established?
• Can a link be associated with more than two processes?
• How many links can there be between every pair of communicating processes?
• What is the capacity of a link?
• Is the size of a message that the link can accommodate fixed or variable?
• Is a link unidirectional or bi-directional?

Department of CSE Operating Systems 34


IPC in Message Passing Systems:
• Messages sent by a process can be either fixed or variable in size
• If fixed-size message can be sent, the system-level implementation is
straight forward, makes the programming task difficult whereas the
variable-sized is more complex but programming task becomes
simpler.
• If processes P and Q wants to communicate each other by sending
and receiving messages, they need to establish a communication
link between them.
• Two types of communication links: 
• 1) physical link:  a)Shared memory b) Hardware bus  c)  Network
•  2) Logical link:  a) Direct or indirect  b) Synchronous or asynchronous 
c) Automatic or explicit buffering
Direct Communication
• Processes must name each other explicitly:
• send (P, message) – send a message to process P
• receive(Q, message) – receive a message from process Q
• Properties of communication link
• Links are established automatically
• A link is associated with exactly one pair of communicating processes
• Between each pair there exists exactly one link
• The link may be unidirectional, but is usually bi-directional

Department of CSE Operating Systems 36


Indirect Communication

• Messages are directed and received from mailboxes (also referred to as ports)

• Each mailbox has a unique id

• Processes can communicate only if they share a mailbox

• Properties of communication link

• Link established only if processes share a common mailbox

• A link may be associated with many processes

• Each pair of processes may share several communication links

• Link may be unidirectional or bi-directional


Department of CSE Operating Systems 37
Indirect Communication

• Operations

• create a new mailbox (port)

• send and receive messages through mailbox

• destroy a mailbox

• Primitives are defined as:

send(A, message) – send a message to mailbox A

receive(A, message) – receive a message from mailbox A

Department of CSE Operating Systems 38


Indirect Communication

• Mailbox sharing

• P1, P2, and P3 share mailbox A

• P1, sends; P2 and P3 receive

• Who gets the message?

• Solutions
• Allow a link to be associated with at most two processes

• Allow only one process at a time to execute a receive operation

• Allow the system to select arbitrarily the receiver. Sender is notified who the receiver
was. Department of CSE Operating Systems 39
Synchronization
• Message passing may be either blocking or non-blocking
• Blocking is considered synchronous
• Blocking send -- the sender is blocked until the message is received
• Blocking receive -- the receiver is blocked until a message is available
• Non-blocking is considered asynchronous
• Non-blocking send -- the sender sends the message and continue
• Non-blocking receive -- the receiver receives:
● A valid message, or
● Null message
● Different combinations possible
● If both send and receive are blocking, we have a rendezvous

Department of CSE Operating Systems 40


Synchronization (Cont.)
Producer-consumer becomes trivial

message next_produced;
while (true) {
/* produce an item in next produced */
send(next_produced);
}

message next_consumed;
while (true) {
receive(next_consumed);

/* consume the item in next consumed */


}

Department of CSE Operating Systems 41


Buffering

• Queue of messages attached to the link.

• implemented in one of three ways

1. Zero capacity – no messages are queued on a link.


Sender must wait for receiver (rendezvous)

2. Bounded capacity – finite length of n messages


Sender must wait if link full

3. Unbounded capacity – infinite length


Sender never waits

Department of CSE Operating Systems 42


A thread is a basic unit of CPU utilization; it comprises a thread ID, a program counter (PC), a register set, and a stack.
Eg: A word processor may have a thread for displaying graphics, another thread for responding to keystrokes from the
user, and a third thread for performing spelling and grammar checking in the background.

A Thread shares with other threads belonging to the same process its code section, data section, and other operating-
system resources, such as open files and signals.

A traditional process has a single thread of control.


If a process has multiple threads of control, it can perform more than one task at a time.
Figure illustrates the difference between a traditional single-threaded process and a multithreaded process.
Threads: A thread is a flow of execution through the process code, with its
own program counter that keeps track of which instruction to execute next,
system registers which hold its current working variables, and a stack which
contains the execution history.

• A thread shares with its peer threads few information like code segment,
data segment and open files. When one thread alters a code segment
memory item, all other threads see that.

• A thread is also called a lightweight process.


• Each thread belongs to exactly one process and no thread can exist
outside a process.
• Each thread represents a separate flow of control.
• Applications- implementing network servers and web server, shared
memory multiprocessors.
• The following figure shows the working of a single-threaded and a
multithreaded process.
S.N. Process Thread

1 Process is heavy weight or resource Thread is light weight, taking lesser resources
intensive. than a process.
2 Process switching needs interaction with Thread switching does not need to interact
operating system. with operating system.
3 In multiple processing environments, each All threads can share same set of open files,
process executes the same code but has its child processes.
own memory and file resources.
4 If one process is blocked, then no other While one thread is blocked and waiting, a
process can execute until the first process second thread in the same task can run.
is unblocked.
5 Multiple processes without using threads Multiple threaded processes use fewer
use more resources. resources.
6 In multiple processes each process One thread can read, write or change another
operates independently of the others. thread's data.
Advantages of Thread
• Threads minimize the context switching time.
• Use of threads provides concurrency within a process.
• Efficient communication.
• It is more economical to create and context switch threads.
• Threads allow utilization of multiprocessor architectures to a greater scale and efficiency.

Types of Thread
Threads are implemented in following two ways −
• User Level Threads − User managed 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.
• Kernel Level Threads − Here, 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.
Multithreaded Programming: A single application may be required to perform several similar tasks. For example, a
web server accepts client requests for web pages, images etc. A busy web server may have several clients
concurrently accessing it. If the web server ran as a traditional single-threaded process, it would be able to service
only one client at a time, and a client might have to wait a very long time for its request to be serviced.
One solution is to have the server run as a single process that accepts requests. When the server receives a request,
it creates a separate process to service that request. Process creation is time consuming and resource intensive.
If the web-server process is multithreaded, the server will create a separate thread that listens for client requests.
When a request is made, rather than creating another process, the server creates a new thread to service the
request and resumes listening for additional requests.
Benefits of Multithreaded Programming:

1. Responsiveness. Multithreading an interactive application may allow a program to continue running even if part of
it is blocked or is performing a lengthy operation, thereby increasing responsiveness to the user.

2. Resource sharing. Processes can share resources only through techniques such as shared memory and message
passing. threads share the memory and the resources of the process to which they belong by default.

3. Economy. Allocating memory and resources for process creation is costly. Because threads share the resources of
the process to which they belong, it is more economical to create and context-switch threads.

4. Scalability. The benefits of multithreading can be even greater in a multiprocessor architecture, where threads
may be running in parallel on different processing cores.
Multicore Programming: Multiple computing cores on a single processing chip where each core appears as a separate CPU
to the operating system. Such systems referred as Multicore and Multithreaded programming systems.
Identifying tasks, Balance, Data splitting, Data dependency –major challenges needed to be addressed by using AMDAHL’S
Law.
Amdahl’s Law is a formula that identifies potential performance gains from adding additional computing cores to an
application that has both serial (nonparallel) and parallel components. If S is the portion of the application
that must be performed serially on a system with N processing cores, the formula appears as follows:
speedup ≤ 1/(S + ((1−S)/N))
Multithreading Models: combined user level thread and Kernel level thread facility.
Eg: Solaris OS
In a combined system, multiple threads within the same application can run in parallel on multiple processors and a
blocking system call need not block the entire process.

Multithreading models are three types


• Many to many relationship.
• Many to one relationship.
• One to one relationship.

Many to Many Model


• The many-to-many model multiplexes any number of user threads onto an equal
or smaller number of kernel threads.
• The following diagram shows the many-to-many threading model where 6 user
level threads are multiplexing with 6 kernel level threads.
• This model provides the best accuracy on concurrency and when a thread
performs a blocking system call, the kernel can schedule another thread
for execution.
Many to One Model
Many-to-one model maps many user level threads to one Kernel-level
thread. Thread
management is done in user space by the thread library. When thread makes
a blocking system call, the entire process will be blocked. Only one thread
can access the Kernel at a time, so multiple threads are unable to run in
parallel on multiprocessors.

One to One Model


There is one-to-one relationship of user-level thread to the kernel-level
thread. This model provides more concurrency than the many-to-one model.
It also allows another thread to run when a thread makes a blocking system
call. It supports multiple threads to execute in parallel on microprocessors.

You might also like