0% found this document useful (0 votes)
2 views

UNIT-2

This document covers the concepts of processes, multithreaded programming, process scheduling, and inter-process communication. It explains the structure of a process, its states, and the role of the Process Control Block (PCB), as well as the different types of schedulers and their functions. Additionally, it discusses operations on processes, including creation and inter-process communication methods such as shared memory and message passing.
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)
2 views

UNIT-2

This document covers the concepts of processes, multithreaded programming, process scheduling, and inter-process communication. It explains the structure of a process, its states, and the role of the Process Control Block (PCB), as well as the different types of schedulers and their functions. Additionally, it discusses operations on processes, including creation and inter-process communication methods such as shared memory and message passing.
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/ 31

UNIT – II

Process Concept, Multithreaded Programming, Process Scheduling, Inter-process


Communication
PART-I
• Process Concept
• Process scheduling
• Operations on processes
• Inter-process communication
• Communication in client server systems.

The Process
• A process is basically a program in execution. The execution of a process must progress in
a sequential fashion.
• A process is basically a program in execution. The execution of a process must progress in
a sequential fashion.
• When a program is loaded into the memory and it becomes a process, it can be divided
into four sections ─ stack, heap, text and data. The following image shows a simplified
layout of a process inside main memory

Fig. Process in Memory


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. Text: This includes the current activity represented by the value of Program Counter and
the contents of the processor's registers
4. Data: This section contains the global and static variables.

UNIT-2 PAGE-2
Process State
• As a process executes, it changes state. The state of a process is defined in part by the
current activity of that process. A process may be in one of the following states:
1. New. The process is being created
2. Running. Instructions are being executed.
3. Waiting. The process is waiting for some event to occur
4. Ready. The process is waiting to be assigned to a processor
5. Terminated. The process has finished execution

Fig. Process States

Process Control Block


• Each process is represented in the operating system by a process control block (PCB)—
also called a task control block
• It contains many pieces of information associated with a specific process, including these:
Process State, Program Counter, CPU Registers, CPU- Scheduling Information, Memory
Management Information, Accounting Information and I/O Status Information

Fig. Process Control Block

UNIT-2 PAGE-3
1. Process State: The current state of the process i.e., whether it is ready, running, waiting, or
whatever.
2. Program Counter : The counter indicates the address of the next instruction to be executed
for this process
3. CPU Registers: The registers vary in number and type, depending on the computer
architecture need to be stored for execution for running state.
4. CPU Scheduling Information: Process priority and other scheduling information which is
required to schedule the process.

Fig. Diagram showing CPU switch from process to process

Threads
• A process is a program that performs a single thread of execution.
• For example, when a process is running a word-processor program, a single thread of
instructions is being executed. This single thread of control allows the process to perform
only one task at a time. The user cannot simultaneously type in characters and run the spell
checker within the same process
• for example. Most modern operating systems have extended the process concept to allow a
process to have multiple threads of execution and thus to perform more than one task at a
time
• Memory Management Information: 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 : This information includes the amount of CPU and real time
used, time limits, account numbers, job or process numbers, and so on
• I/O Status Information : This information includes the list of I/O devices allocated to the
process, a list of open files, and so on.

UNIT-2 PAGE-4
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 an available process for program
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

Scheduling Queues
• The Operating System maintains the following important process scheduling queues

• Job queue − This queue keeps all the processes in the system.
• Ready queue − This queue keeps a set of all processes residing in main memory, ready
and waiting to execute. A new process is always put in this queue.
• Device queues − The processes which are blocked due to unavailability of an I/O device
constitute this queue.

Fig. queuing Diagram Representation of Process Scheduling

• A common representation of process scheduling is a queueing diagram


• Each rectangular box represents a queue. Two types of queues are present: the ready
queue and a set of device queues. The circles represent the resources that serve the
queues, and the arrows indicate the flow of processes in the system.
• A new process is initially put in the ready queue. It waits there until it is selected for
execution, or dispatched
• Once the process is allocated the CPU and is executing, one of several events could occur

UNIT-2 PAGE-5
• The process could issue an I/O request and then be placed in an I/O queue.
• The process could create a new child process and wait for the child’s termination.
• The process could be removed forcibly from the CPU, as a result of an interrupt, and be
put back in the ready queue.
In the first two cases, the process eventually switches from the waiting state to the ready state and
is then put back in the ready queue. A process continues this cycle until it terminates, at which
time it is removed from all queues and has its PCB and resources deal located

Schedulers
• Schedulers are special system software which handle process scheduling in various ways.
Their main task is to select the jobs to be submitted into the system and to decide which
process to run. Schedulers are of three types
• Long-Term Scheduler
• Short-Term Scheduler
• Medium-Term Scheduler

Long Term Scheduler


• It is also called a job scheduler. A long-term scheduler determines which programs are
admitted to the system for processing.
• It 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.
• If the degree of multiprogramming is stable, then the average rate of process creation must
be equal to the average departure rate of processes leaving the system.

Short Term Scheduler


• It is also called as CPU scheduler. Its main objective is to increase system performance in
accordance with the chosen set of criteria. It is the change of ready state to running state of
the process.
• CPU scheduler selects a process among the processes that are ready to execute and
allocates CPU to one of them.
• Short-term schedulers, also known as dispatchers, make the decision of which process to
execute next. Short-term schedulers are faster than long-term schedulers.

Medium Term Scheduler


• Some operating systems, such as time-sharing systems, may introduce an additional,
intermediate level of scheduling called medium-term scheduler
• The key idea behind a medium-term scheduler is that sometimes it can be advantageous to
remove a process from memory and thus reduce the degree of multiprogramming. Later,
the process can be reintroduced into memory, and its execution can be continued where it
left off. This scheme is called swapping.
• The process is swapped out, and is later swapped in, by the medium-term scheduler

UNIT-2 PAGE-6
Fig. Addition of medium-term scheduling to the queueing diagram

Context Switch
• A context switch is the mechanism to store and restore the state or context of a CPU in
Process Control block so that a process execution can be resumed from the same point at a
later time.
• Context switching is an essential part of a multitasking operating system features.
• When the scheduler switches the CPU from executing one process to execute another, the
state from the current running process is stored into the process control block.
• After this, the state for the process to run next is loaded from its own PCB and used to set
the PC, registers, etc. At that point, the second process can start executing.

Operations on Process
Process Creation
• A process may create several new processes, via a create-process system call, during the
course of execution.
• The creating process is called a parent process, and the new processes are called the
children of that process. Each of these new processes may in turn create other processes,
forming a tree of processes.
• Most operating systems identify processes according to a unique process identifier (or
pid), which is typically an integer number.
The pid provides a unique value for each process in the system, and it can be used as an index to
access various attributes of a process within the kernel

Fig. A Tree of Processes on typical Linux System

UNIT-2 PAGE-7
• A typical process tree for the Linux operating system, showing the name of each process
and its pid.
• The init process serves as the root parent process for all user processes
• When a process creates a new process, two possibilities exist in terms of execution:
1. The parent continues to execute concurrently with its children.
2. The parent waits until some or all of its children have terminated
3. There are also two possibilities in terms of the address space of the new process:
4. 1. The child process is a duplicate of the parent process (it has the same program and data
as the parent).
5. 2. The child process has a new program loaded into it
6. A new process is created by the fork() system call. The new process consists of a copy of
the address space of the original process. This mechanism allows the parent process to
communicate easily with its child process

Inter-process Communication
• Processes executing concurrently in the operating system may be either independent
processes or cooperating processes.
• A process is independent if it cannot affect or be affected by the other processes
executing in the system
• Any process that does not share data with any other process is independent.
• A process is cooperating if it can affect or be affected by the other processes executing in
the system.
• There are several reasons for providing an environment that allows process cooperation:
• Information sharing. Since several users may be interested in the same piece of
information (for instance, a shared file), we must provide an environment to allow
concurrent access to such information
• Computation speedup. If we want a particular task to run faster, we must break it into
subtasks, each of which will be executing in parallel with the others. Notice that such a
speedup can be achieved only if the computer has multiple processing elements (such as
CPUs or I/O channels).
• Modularity. We may want to construct the system in a modular fashion, dividing the
system functions into separate processes or threads.
• Convenience. Even an individual user may work on many tasks at the same time. For
instance, a user may be editing, printing, and compiling in parallel.
• Cooperating processes require an interprocess communication (IPC) mechanism that will
allow them to exchange data and information.
• There are two fundamental models of inter-process communication:
• (1) shared memory and
• (2) message passing.
• In the shared-memory model, a region of memory that is shared by cooperating processes
is established.
UNIT-2 PAGE-8
• Processes can 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 than is shared memory for inter computer
communication.
• Shared memory allows maximum speed and convenience of communication, as it can be
done at memory speeds when within a computer.
• Shared memory is faster than message passing, as message-passing systems are typically
implemented using system calls and thus require the more time consuming task of kernel
intervention.

Fig. Communication Models : A. Message Passing B. Shared Memory

• Shared-Memory Systems Inter process 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. Other processes that wish to communicate using this shared-
memory segment must attach it to their address space.

• The operating system tries to prevent one process from accessing another process's
memory. Shared memory requires that two or more processes agree to remove this
restriction.

• They can then exchange information by reading and writing data in the shared areas

• Message-Passing Systems The scheme requires that these processes share a region of
memory and that the code for accessing and manipulating the shared memory be written
explicitly by the application programmer.

• Another way to achieve the same effect is for the operating system to provide the means
for cooperating processes to communicate with each other via a message-passing facility

• Message passing provides a mechanism to allow processes to communicate and to


synchronize their actions without sharing the same address space and is particularly

UNIT-2 PAGE-9
useful in a distributed environment, where the communicating processes may reside on
different computers connected by a network.

• A message-passing facility provides at least two operations:

• send(message) and receive(message).

• Messages sent by a process can be of either fixed or variable size.

• If only fixed sized messages can be sent, the system-level implementation is


straightforward. This restriction, however, makes the task of programming more difficult.

• Conversely, variable-sized messages require a more complex system-level


implementation, but the programming task becomes simpler. This is a common kind of
tradeoff seen throughout operating system design

Naming

• Processes that want to communicate must have a way to refer to each other.

• They can use either direct or indirect communication.

• Under direct communication, each process that wants to communicate must explicitly
name the recipient or sender of the communication.

• In this scheme, the send() and receive() primitives are defined as:

• send(P, message)—Send a message to process P

• receive (Q, message)—Receive a message from process Q

• A communication link in this scheme has the following properties:

• A link is established automatically between every pair of processes that want to


communicate. The processes need to know only each other's identity to communicate.

• A link is associated with exactly two processes.

• Between each pair of processes, there exists exactly one link

• The disadvantage in both of these schemes (symmetric and asymmetric) is the limited
modularity of the resulting process definitions. Changing the identifier of a process may
necessitate examining all other process definitions.

Synchronization

• Communication between processes takes place through calls to send() and receive ()
primitives. There are different design options for implementing each primitive.

• Message passing may be either blocking or nonblocking— also known as synchronous


and asynchronous.

• Blocking send- The sending process is blocked until the message is received by the
receiving process or by the mailbox.

• Nonblocking send- The sending process sends the message and resumes operation

• Blocking receive- The receiver blocks until a message is available.


UNIT-2 PAGE-10
• Nonblocking receive- The receiver retrieves either a valid message or a null.

• Buffering Whether communication is direct or indirect, messages exchanged by


communicating processes reside in a temporary queue. Basically, such queues can be
implemented in threeways:

1. Zero capacity- The queue has a maximum length of zero; thus, the link cannot have any
messages waiting in it. In this case, the sender must block until the recipient receives the
message.

2. Bounded capacity- The queue has finite length n; thus, at most n messages can reside in
it. If the queue is not full when a new message is sent, the message is placed in the queue
(either the message is copied or a pointer to the message is kept), and the sender can
continue execution without waiting. The links capacity is finite, however. If the link is full,
the sender must block until space is available in the queue.

3. Unbounded capacity- The queues length is potentially infinite; thus, any number of
messages can wait in it. The sender never blocks

Case Studies

Examples of IPC Systems :

1. POSIX Shared Memory

2. Windows XP

Communication in Client Server Systems

• Client/Server communication involves two components, namely a client and a server.


They are usually multiple clients in communication with a single server. The clients send
requests to the server and the server responds to the client requests.

• There are three main methods to client/server communication

1. Sockets

2. Remote Procedure Calls

3. PIPES

Sockets

• Sockets facilitate communication between two processes on the same machine or different
machines. They are used in a client/server framework and consist of the IP address and
port number.

• Many application protocols use sockets for data connection and data transfer between a
client and a server.

• Socket communication is quite low-level as sockets only transfer an unstructured byte


stream across processes. The structure on the byte stream is imposed by the client and
server applications.

UNIT-2 PAGE-11
Fig. Communication using sockets

• When a client process initiates a request for a connection, it is assigned a port by its host
computer. This port has some arbitrary number greater than 1024.

• For example, if a client on host X with IP address 146.86.5.20 wishes to establish a


connection with a web server (which is listening on port 80) at address 161.25.19.8, host X
may be assigned port 1625.

• The connection will consist of a pair of sockets: (146.86.5.20:1625) on host X and


(161.25.19.8:80) on the web server

Remote Procedure Calls

• These are interprocess communication techniques that are used for client-server based
applications. A remote procedure call is also known as a subroutine call or a function call.

• A client has a request that the RPC translates and sends to the server.

• This request may be a procedure or a function call to a remote server. When the server
receives the request, it sends the required response back to the client

Pipes

• These are interprocess communication methods that contain two end points. Data is
entered from one end of the pipe by a process and consumed from the other end by the
other process.

• The two different types of pipes are ordinary pipes and named pipes.

• Ordinary pipes allow two processes to communicate in standard producer– consumer


fashion: the producer writes to one end of the pipe (the write-end) and the consumer reads
from the other end (the read-end).

• Ordinary pipes only allow one way communication. For two way communication, two
pipes are required.

• Ordinary pipes have a parent child relationship between the processes as the pipes can
only be accessed by processes that created or inherited them.

UNIT-2 PAGE-12
Fig. File Descriptors for Ordinary PIPE

• Named pipes provide a much more powerful communication tool.

• Communication can be bidirectional, and no parent–child relationship is required. Once a


named pipe is established, several processes can use it for communication.

• These pipes exist even after the processes using them have terminated. They need to be
explicitly deleted when not required anymore.

PART-II

• Multithreaded Programming: Multithreading models, Thread libraries, Threading issues,


Examples

Threads

• A thread is a basic unit of CPU utilization; it comprises a thread ID, a program counter, a
register set, and a stack.

• It 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 (or heavyweight) 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

UNIT-2 PAGE-13
Overview

• 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

• In certain situations, a single application may be required to perform several similar tasks.
For example, a web server accepts client requests for web pages, images, sound, and so
forth.

• A busy web server may have several (perhaps thousands) of 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. The amount of time that a client might have to wait for its request
to be serviced could be enormous.

• 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.
Threads also play a vital role in remote procedure call (RPC) systems.

• Finally, many operating system kernels are now multithreaded; several threads operate in
the kernel, and each thread performs a specific task,such as managing devices or interrupt
handling.

Fig. Multi threaded server architecture

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 only share resources through techniques such as shared
memory and message passing. Such techniques must be explicitly arranged by the
programmer.

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

UNIT-2 PAGE-14
Fig: Concurrent execution on a single-core system

Scalability

The benefits of multithreading can be even greater in a multiprocessor architecture, where threads
may be running in parallel on different processing cores. A single-threaded process can run on
only one processor, regardless how many are available. We explore this issue further in the
following section

Multi core programming

• A more recent, similar trend in system design is to place multiple computing cores on a
single chip. Each core appears as a separate processor to the operating system

• Whether the cores appear across CPU chips or within CPU chips, we call these systems
multicore or multiprocessor systems

• Multithreaded programming provides a mechanism for more efficient use of these multiple
computing cores and improved concurrency

Fig. Parallel execution on a multicore system

Types of Parallelism

• There are two types of parallelism: data parallelism and task parallelism. Data parallelism
focuses on distributing subsets of the same data across multiple computing cores and
performing the same operation on each core.

• Task parallelism involves distributing not data but tasks (threads) across multiple
computing cores. Each thread is performing a unique operation. Different threads may be
operating on the same data, or they may be operating on different data

Multithreading Models

• Multi threading-It is a process of multiple threads executes at same time.

• Many operating systems support kernel thread and user thread in a combined way.

Many to One Model


UNIT-2 PAGE-15
• The many to one model maps many of the user threads to a single kernel thread. This
model is quite efficient as the user space manages the thread management.

• A disadvantage of the many to one model is that a thread blocking system call blocks
the entire process. Also, multiple threads cannot run in parallel as only one thread
can access the kernel at a time.

Many to one Model

One to One Model

• One to One Model: The one to one model maps each of the user threads to a kernel
thread. This means that many threads can run in parallel on multiprocessors and other
threads can run when one thread makes a blocking system call.

• A disadvantage of the one to one model is that the creation of a user thread requires a
corresponding kernel thread. Since a lot of kernel threads burden the system, there is
restriction on the number of threads in the system.

• Many to Many Model: he many to many model maps many of the user threads to a equal
number or lesser kernel threads. The number of kernel threads depends on the application
or 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.

UNIT-2 PAGE-16
• The second approach is to implement a kernel level library supported directly by the
operating system. In this case the code and data structures for the library exist in kernel
space.

• Invoking a function in the application program interface for the library typically results in
a system call to the kernel.

• The main thread libraries which are used are given below

• POSIX threads − Pthreads refers to the POSIX standard (IEEE 1003.1c) defining an API
for thread creation and synchronization. This is a specification for thread behavior, not an
implementation

• Pthreads, the threads extension of the POSIX standard, may be provided as either a user
level or a kernel level library.

• WIN 32 thread − The windows thread library is a kernel level library available on
windows systems.

• JAVA thread − The JAVA thread API allows threads to be created and managed directly
as JAVA programs.

Threading Issues

• Semantics of fork() and exec() system calls

• Thread cancellation

• Signal handling

• Thread pools

• The fork() and exec() system calls:

• The fork() is used to create a duplicate process. The meaning of the fork() and exec()
system calls change in a multithreaded program

• If one thread in a program which calls fork(), does the new process duplicate all threads, or
is the new process single-threaded?

• If we take, some UNIX systems have chosen to have two versions of fork(), one that
duplicates all threads and another that duplicates only the thread that invoked the fork()
system call.

• If a thread calls the exec() system call, the program specified in the parameter to exec()
will replace the entire process which includes all threads

• Signal Handling: Generally, signal is used in UNIX systems to notify a process that a
particular event has occurred. A signal received either synchronously or asynchronously,
based on the source of and the reason for the event being signalled.

• All signals, whether synchronous or asynchronous, follow the same pattern as given below

• A signal is generated by the occurrence of a particular event.

UNIT-2 PAGE-17
• The signal is delivered to a process.

• Once delivered, the signal must be handled.

• Cancellation:Thread cancellation is the task of terminating a thread before it has


completed.

• A target thread is a thread that is to be cancelled, cancellation of target thread may occur in
two different scenarios −

• Asynchronous cancellation − One thread immediately terminates the target thread.

• Deferred cancellation − The target thread periodically checks whether it should


terminate, allowing it an opportunity to terminate itself in an ordinary fashion.

• Thread Polls:Multithreading in a web server, whenever the server receives a request it


creates a separate thread to service the request

• Some of the problems that arise in creating a thread are as follows :

• The amount of time required to create the thread prior to serving the request together with
the fact that this thread will be discarded once it has completed its work.

• If all concurrent requests are allowed to be serviced in a new thread, there is no bound on
the number of threads concurrently active in the system

• Unlimited thread could exhaust system resources like CPU time or memory.

• A thread pool is to create a number of threads at process start-up and place them into a
pool, where they sit and wait for work.

Operating System Examples

1. Windows Threads: Windows implements the Windows API, which is the primary API
for the family of Microsoft operating systems

• A Windows application runs as a separate process, and each process may contain one or
more threads

• Windows uses the one-to-one mapping

• The general components of a thread include:A thread ID, A register set, A user stack and
A private storage area

• The register set, stacks, and private storage area are known as the context of the thread.

• The primary data structures of a thread include:

• ETHREAD—executive thread block

• KTHREAD—kernel thread block

• TEB—thread environment block

• The key components of the ETHREAD include a pointer to the process to which the thread
belongs and the address of the routine in which the thread starts control

• The KTHREAD includes scheduling and synchronization information for the thread
UNIT-2 PAGE-18
• the TEB contains the thread identifier, a user-mode stack, and an array for thread-local
storage

Fig. Data Structures of windows thread

1. Linux Threads: Linux provides the fork() system call with the traditional functionality of
duplicating a process

• Linux also provides the ability to create threads using the clone() system call

• Linux does not distinguish between processes and threads. In fact, Linux uses the term
task —rather than process or thread— when referring to a flow of control within a
program

• When clone() is invoked, it is passed a set of flags that determine how much sharing is to
take place between the parent and child tasks

• For example, suppose that clone() is passed the flags CLONE FS, CLONE VM, CLONE
SIGHAND, and CLONE FILES. The parent and child tasks will then share the same file-
system information

• Using clone() in this fashion is equivalent to creating a thread

• since the parent task shares most of its resources with its child task

UNIT-2 PAGE-19
• However, if none of these flags is set when clone() is invoked, no sharing takes place,
resulting in functionality similar to that provided by the fork() system call

Part-III

• Process Scheduling: Basic concepts, Scheduling criteria, Scheduling algorithms, Multiple


processor scheduling, Thread scheduling, Examples

• In a single-processor system, only one process can run at a time. Others must wait until the
CPU is free and can be resched

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


maximize CPU utilization.

• Several processes are kept in memory at one time when one process has to wait, the
operating system takes the CPU away from that process and gives the CPU to another
process. This pattern continues

CPU –I/O Burst Cycle

• process execution consists of a cycle of CPU execution and I/O wait. Processes alternate
between these two states.

• Process execution begins with a CPU burst. That is followed by an I/O burst, which is
followed by another CPU burst, then anotherI/O burst, and so on.

• Eventually, the final CPU burst ends with a system request to terminate execution.

• An I/O-bound program typically has many short CPU bursts. A CPU-bound program
might have a few long CPU bursts

Fig. Alternating sequence of CPU and I/O bursts.

UNIT-2 PAGE-20
CPU Scheduler

• Selects from among the processes in memory that are ready to execute, and allocates the
CPU to one of them

• CPU scheduling decisions may take place when a process:

 When a process switches from the running state to the waiting state

 When a process switches from the running state to the ready state

 When a process switches from the waiting state to the ready state

 When a process terminates

• When scheduling takes place only under circumstances 1 and 4, we say that the scheduling
scheme is nonpreemptive or cooperative. Otherwise, it is preemptive

• Preemptive Scheduling: The resources (mainly CPU cycles) are allocated to the process
for a limited amount of time and then taken away, and the process is again placed back in
the ready queue if that process still has CPU burst time remaining. That process stays in
the ready queue till it gets its next chance to execute.

• Non Preemptive Scheduling: In this scheduling, once the resources (CPU cycles) are
allocated to a process, the process holds the CPU till it gets terminated or reaches a waiting
state.

• In the case of non-preemptive scheduling does not interrupt a process running CPU in the
middle of the execution.

• Instead, it waits till the process completes its CPU burst time, and then it can allocate the
CPU to another process.

Scheduling Criteria

• CPU utilization. We want to keep the CPU as busy as possible. CPU utilization can range
from 0 to 100 percent. In a real system, it should range from 40 percent (for a lightly
loaded system) to 90 percent (for a heavily loaded system).

• Throughput: number of processes that are completed per time unit, called throughput

• Turnaround Time: The interval from the time of submission of a process to the time of
completion is the turnaround time. Turnaround time is the sum of the periods spent waiting
to get into memory, waiting in the ready queue, executing on the CPU, and doing I/O.

• Waiting Time: Waiting time is the sum of the periods spent waiting in the ready queue.

• Response Time:the time from the submission of a request until the first response is
produced. This measure, called response time, is the time it takes to start responding, not
the time it takes to output the response

Scheduling Algorithms

• First Come First Served Scheduling:

• Jobs are executed on first come, first serve basis.

UNIT-2 PAGE-21
• It is a non-preemptive scheduling algorithm.

• Easy to understand and implement.

• Its implementation is based on FIFO queue.

• Poor in performance as average wait time is high.

• Example: If the processes arrive in the order P1, P2, P3, and are served in FCFS order, we
get the result shown in the following Gantt chart, which is a bar chart that illustrates a
particular schedule, including the start and finish times of each of the participating
processes:

• The waiting time is 0 milliseconds for process P1, 24 milliseconds for process P2, and 27
milliseconds for process P3. Thus, the average waiting time is (0 + 24 + 27)/3 = 17
milliseconds. If the processes arrive in the order P2, P3, P1, however, the results will be as
shown in the following Gantt chart:

The average waiting time is now (6 + 0 + 3)/3 = 3 milliseconds

• There is a convoy effect as all the other processes wait for the one big process to get off
the CPU. This effect results in lower CPU and device utilization than might be possible if
the shorter processes were allowed to go first.

• The FCFS algorithm is thus particularly troublesome for time-sharing systems, where it is
important that each user get a share of the CPU at regular intervals

Shortest Job First Scheduling

• This is also known as shortest job first, or SJF

• This is a non-preemptive, pre-emptive scheduling algorithm.

• Best approach to minimize waiting time.

• Easy to implement in Batch systems where required CPU time is known in advance.

UNIT-2 PAGE-22
• Impossible to implement in interactive systems where required CPU time is not known.

• The processer should know in advance how much time process will take.

• The waiting time is 3 milliseconds for process P1, 16 milliseconds for process P2, 9
milliseconds for process P3, and 0 milliseconds for process P4. Thus, the average waiting
time is (3 + 16 + 9 + 0)/4 = 7 milliseconds.

• The SJF algorithm can be either preemptive or nonpreemptive. The choice arises when a
new process arrives at the ready queue while a previous process is still executing.

• The next CPU burst of the newly arrived process may be shorter than what is left of the
currently executing process.

• A preemptive SJF algorithm will preempt the currently executing process, whereas a
nonpreemptive SJF algorithm will allow the currently running process to finish its CPU
burst. Preemptive SJF scheduling is sometimes called shortest-remaining-time-first
scheduling

• As an example, consider the following four processes, with the length of the CPU burst
given in milliseconds:

If the processes arrive at the ready queue at the times shown and need the indicated burst
times, then the resulting preemptive SJF schedule is as depicted in the following Gantt chart:

UNIT-2 PAGE-23
Process P1 is started at time 0, since it is the only process in the queue. Process P2 arrives at time
1. The remaining time for process P1 (7 milliseconds) is larger than the time required by process
P2 (4 milliseconds), so process P1 is preempted, and process P2 is scheduled. The average waiting
time for this example is [(10 − 1) + (1 − 1) + (17 − 2) + (5 − 3)]/4 = 26/4 = 6.5 milliseconds.
Nonpreemptive SJF scheduling would result in an average waiting time of 7.75 milliseconds.

Priority Scheduling Algorithm

• The SJF algorithm is a special case of the general priority-scheduling algorithm. A priority
is associated with each process, and the CPU is allocated to the process with the highest
priority. Equal-priority processes are scheduled in FCFS order

• The larger the CPU burst, the lower the priority, and vice versa

• Priorities are generally indicated by some fixed range of numbers, such as 0 to 7 or 0 to


4,095.

• we assume that low numbers represent high priority.

As an example, consider the following set of processes, assumed to have arrived at time 0 in
the order P1, P2, ···, P5, with the length of the CPU burst given in milliseconds:

The average waiting time is 8.2 milliseconds

• Priority scheduling can be either preemptive or nonpreemptive. When a process arrives at


the ready queue, its priority is compared with the priority of the currently running process.

• A preemptive priority scheduling algorithm will preempt the CPU if the priority of the
newly arrived process is higher than the priority of the currently running process. A
nonpreemptive priority scheduling algorithm will simply put the new process at the head
of the ready queue.

• A major problem with priority scheduling algorithms is indefinite blocking, or


starvation. A process that is ready to run but waiting for the CPU can be considered
blocked

• A solution to the problem of indefinite blockage of low-priority processes is aging.

• Aging involves gradually increasing the priority of processes that wait in the system for a
long time

UNIT-2 PAGE-24
• For example, if priorities range from 127 (low) to 0 (high), we could increase the priority
of a waiting process by 1 every 15 minutes. Eventually, even a process with an initial
priority of 127 would have the highest priority in the system and would be executed. In
fact, it would take no more than 32 hours for a priority-127 process to age to a priority-0
process.

Round-Robin Scheduling

• The round-robin (RR) scheduling algorithm is designed especially for time-sharing


systems.

• It is similar to FCFS scheduling, but preemption is added to enable the system to switch
between processes. A small unit of time, called a time quantum or time slice, is defined

• Once a process is executed for a given time period, it is preempted and other process
executes for a given time period.

• Context switching is used to save states of preempted processes

If we use a time quantum of 4 milliseconds, then process P1 gets the first 4 milliseconds. Since it
requires another 20 milliseconds, it is preempted after the first time quantum, and the CPU is
given to the next process in the queue, process P2. Process P2 does not need 4 milliseconds, so it
quits before its time quantum expires. The CPU is then given to the next process, process P3.
Once each process has received 1 time quantum, the CPU is returned to process P1 for an
additional time quantum.

P1 waits for 6 milliseconds (10 - 4), P2 waits for 4 milliseconds, and P3 waits for 7 milliseconds.
Thus, the average waiting time is 17/3 = 5.66 milliseconds.

• In the RR scheduling algorithm, no process is allocated the CPU for more than 1 time
quantum in a row (unless it is the only runnable process).

• if the time quantum is extremely large, the RR policy is the same as the FCFS policy. In
contrast, if the time quantum is extremely small (say, 1 millisecond), the RR approach can
result in a large number of context switches

UNIT-2 PAGE-25
Fig. smaller time quantum increases context switches

Multilevel Queue Scheduling

• Another class of scheduling algorithms has been created for situations in which processes
are easily classified into different groups.

• common division is made between foreground (interactive) processes and background


(batch) processes.

• These two classes have different scheduling needs. For this kind of situation Multilevel
Queue Scheduling is used.

• A multilevel queue scheduling algorithm partitions the ready queue into several separate
queues

• The processes are permanently assigned to one queue, generally based on some property
of the process, such as memory size, process priority, or process type

• Each queue has its own scheduling algorithm.

• For example, separate queues might be used for foreground and background processes.
The foreground queue might be scheduled by an RR algorithm, while the background
queue is scheduled by an FCFS algorithm

• example of a multilevel queue scheduling algorithm with five queues, listed below in order
of priority

UNIT-2 PAGE-26
Fig. Multilevel queue scheduling

• Each queue has absolute priority over lower-priority queues. No process in the batch
queue, for example, could run unless the queues for system processes, interactive
processes, and interactive editing processes were all empty.

• If an interactive editing process entered the ready queue while a batch process was
running, the batch process would be preempted

• Another possibility is to time-slice among the queues. Here, each queue gets a certain
portion of the CPU time, which it can then schedule among its various processes.

Multilevel Feedback Queue Scheduling

• The multilevel feedback queue scheduling algorithm allows a process to move between
queues.

• The idea is to separate processes according to the characteristics of their CPU bursts. If a
process uses too much CPU time, it will be moved to a lower-priority queue

• This scheme leaves I/O-bound and interactive processes in the higher-priority queues.

In addition, a process that waits too long in a lower-priority queue may be moved to a higher-
priority queue. This form of aging prevents starvation

• For example, consider a multilevel feedback queue scheduler with three queues, numbered
from 0 to 2

• The scheduler first executes all processes in queue 0 Only when queue 0 is empty will it
execute processes in queue 1. Similarly, processes in queue 2 will be executed only if
queues 0 and 1 are empty.

• A process that arrives for queue 1 will preempt a process in queue 2. A process in queue 1
will in turn be preempted by a process arriving for queue 0.

UNIT-2 PAGE-27
Fig. Multilevel Feedback Queues

• A multilevel feedback queue scheduler is defined by the following parameters:

• The number of queues

• The scheduling algorithm for each queue

• The method used to determine when to upgrade a process to a higher priority queue

• The method used to determine when to demote a process to a lower priority queue

• The method used to determine which queue a process will enter when that process needs
service

Thread Scheduling

• Contention ScopThe word contention here refers to the competition or fight among the
User level threads to access the kernel resources.

• Thus, this control defines the extent to which contention takes place. It is defined by the
application developer using the thread library. Depending upon the extent of contention it
is classified as Process Contention Scope and System Contention Scope. e:

• Process Contention Scope (PCS):The contention takes place among threads within a same
process. The thread library schedules the high-prioritized PCS thread to access the
resources via available LWPs

• System Contention Scope (SCS):The contention takes place among all threads in the
system. In this case, every SCS thread is associated to each LWP by the thread library and
are scheduled by the system scheduler to access the kernel resources

• Pthread Scheduling: Pthreads identifies the following contention scope values

• PTHREAD_SCOPE_PROCESS schedules threads using PCS scheduling.

• PTHREAD_SCOPE_SYSTEM schedules threads using SCS scheduling

• The Pthread IPC provides two functions for getting—and setting—the contention scope
policy

• pthread attr setscope(pthread attr t *attr, int scope)


UNIT-2 PAGE-28
• pthread attr getscope(pthread attr t *attr, int *scope)

Multiprocessor Scheduling

• In multiple-processor scheduling multiple CPU’s are available and hence Load


Sharing becomes possible. However multiple processor scheduling is more complex as
compared to single processor scheduling

• Approaches to Multiple-Processor Scheduling

One approach is when all the scheduling decisions and I/O processing are handled by a single
processor which is called the Master Server and the other processors executes only the user
code. This is simple and reduces the need of data sharing. This entire scenario is
called Asymmetric Multiprocessing

• A second approach uses Symmetric Multiprocessing where each processor is self


scheduling.

• All processes may be in a common ready queue or each processor may have its own
private queue for ready processes.

The scheduling proceeds further by having the scheduler for each processor examine the ready
queue and select a process to execute

Process Affinity

• When a process runs on a specific processor there are certain effects on the cache memory.
The data most recently accessed by the process populate the cache for the processor and as
a result successive memory access by the process are often satisfied in the cache memory.

• Now if the process migrates to another processor, the contents of the cache memory must
be invalidated for the first processor and the cache for the second processor must be
repopulated

• Because of the high cost of invalidating and repopulating caches, most of the
SMP(symmetric multiprocessing) systems try to avoid migration of processes from one
processor to another and try to keep a process running on the same processor. This is
known as PROCESSOR AFFINITY.

• There are two types of process affinity

1. Soft Affinity

2. Hard Affinity

• Soft Affinity – When an operating system has a policy of attempting to keep a process
running on the same processor but not guaranteeing it will do so, this situation is called
soft affinity.

• Hard Affinity – Hard Affinity allows a process to specify a subset of processors on which
it may run. Some systems such as Linux implements soft affinity but also provide some
system calls like sched_setaffinity() that supports hard affinity.

UNIT-2 PAGE-29
Load Balancing

• On SMP systems, it is important to keep the workload balanced among all processors to
fully utilize the benefits of having more than one processor.

• Otherwise, one or more processors may sit idle while other processors have high
workloads, along with lists of processes awaiting the CPU.

• Load balancing attempts to keep the workload evenly distributed across all processors in
an SMP system. It is important to note that load balancing is typically necessary only on
systems where each processor has its own private queue of eligible processes to execute

• On systems with a common run queue, load balancing is often unnecessary, because once
a processor becomes idle, it immediately extracts a runnable process from the common run
queue.

• There are two general approaches to load balancing: push migration and pull migration

• Push Migration – In push migration a task routinely checks the load on each processor and
if it finds an imbalance then it evenly distributes load on each processors by moving the
processes from overloaded to idle or less busy processors.

• Pull Migration – Pull Migration occurs when an idle processor pulls a waiting task from a
busy processor for its execution.

Fig. Non Uniform Memory Access (NUMA) and CPU Scheduling

Part – IV

• Inter-process Communication: Race conditions, Critical Regions, Mutual exclusion with


busy waiting, Sleep and wakeup, Semaphores, Mutexes, Monitors, Message passing,
Barriers, Classical IPC Problems - Dining philosophers problem, Readers and writers
problem.

Race Condition

• The situation where several processes access and manipulate the same data concurrently
and the outcome of the execution depends on the particular order in which the access takes
place, is called a race condition

• We can easily synchronize the processes to prevent the race condition. To prevent the race
condition, we need to ensure that only one process can access the shared data at a time

UNIT-2 PAGE-30
The Critical Section Problem

• Consider a system consisting of n processes {P0, P1, ..., Pn−1}. Each process has a
segment of code, called a critical section, in which the process may be changing common
variables, updating a table, writing a file, and so on.

• The important feature of the system is that, when one process is executing in its critical
section, no other process is allowed to execute in its critical section

• That is, no two processes are executing in their critical sections at the same time

• The critical-section problem is to design a protocol that the processes can use to cooperate.
Each process must request permission to enter its critical section.

• The section of code implementing this request is the entry section.

• The critical section may be followed by an exit section. The remaining code is the
remainder section

• The entry section and exit section are enclosed in boxes to highlight these important
segments of code.

Fig. General Structure of a Typical Process Pi

Solution to Critical Section Problem

• A solution to the critical-section problem must satisfy the following three requirements:

• Mutual exclusion. If process Pi is executing in its critical section, then no other processes
can be executing in their critical sections

• Progress. If no process is executing in its critical section and some processes wish to enter
their critical sections, then only those processes that are not executing in their remainder
sections can participate in deciding which will enter its critical section next, and this
selection cannot be postponed indefinitely

• Bounded waiting. There exists a bound, or limit, on the number of times that other
processes are allowed to enter their critical sections after a process has made a request to
enter its critical section and before that request is granted

Critical Region

• Use critical regions to provide mutual exclusion and help fix race conditions

• Four conditions to provide mutual exclusion


UNIT-2 PAGE-31
1. No two processes simultaneously in critical region

2. No assumptions made about speeds or numbers of CPUs

3. No process running outside its critical region may block another process

4. No process must wait forever to enter its critical region

UNIT-2 PAGE-32

You might also like