0% found this document useful (0 votes)
25 views78 pages

Os Notes 543322

The document provides a comprehensive overview of Operating Systems, detailing their definitions, types, and core functionalities including process management, memory management, and I/O management. It covers various operating system types such as batch, multiprogrammed, time-sharing, real-time, and distributed systems, along with their advantages and disadvantages. Additionally, it discusses key concepts like inter-process communication, deadlocks, and the structure of the Linux operating system.

Uploaded by

zjf2a6hm
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)
25 views78 pages

Os Notes 543322

The document provides a comprehensive overview of Operating Systems, detailing their definitions, types, and core functionalities including process management, memory management, and I/O management. It covers various operating system types such as batch, multiprogrammed, time-sharing, real-time, and distributed systems, along with their advantages and disadvantages. Additionally, it discusses key concepts like inter-process communication, deadlocks, and the structure of the Linux operating system.

Uploaded by

zjf2a6hm
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/ 78

OPERATING

SYSTEM BSc I
SEMESTER
Operating System
UNIT I:
Introduction: Basics of Operating Systems: Definition, types of Operating
Systems, OS Service, System Calls, OS structure: Layered, Monolithic,
Microkernel Operating Systems – Concept of Virtual Machine.

UNIT II:
Process Management Process Definition , Process Relationship , Process
states , Process State transitions , Process Control Block , Context switching ,
Threads, Concept of multi threads , Benefits of threads, Types of threads.
Process Scheduling: Definition, Scheduling objectives, Types of Schedulers,
CPU scheduling algorithms, performance evaluation of the scheduling.

UNIT III:
Inter-process Communication, Race Conditions, Critical Section, Mutual
Exclusion, Hardware Solution, Strict Alternation, Peterson’s Solution, The
Producer Consumer Problem, Semaphores, Event Counters, Monitors,
Message Passing, and Classical IPC Problems.
Deadlocks: Definition, Deadlock characteristics, Deadlock Prevention,
Deadlock Avoidance (concepts only).

UNIT IV:
Memory Management: Logical and Physical address map, Memory
allocation, Internal and External fragmentation and Compaction, Paging.
Virtual Memory: Demand paging, Page Replacement policies.

UNIT V:
I/O Management Principles of I/O Hardware: Disk structure, Disk
scheduling algorithm
File Management: Access methods, File types, File operation, Directory
structure, File System structure, Allocation methods, Free-space
management, and directory implementation. Structure of Linux Operating
System, Exploring the Directory Structure, Naming Files and Directories,
Concept of shell, Types of shell, Editors for shell programming (e.g. vi), basics
of Shell programming.
UNIT I
Introduction: Basics of Operating Systems: Definition, types of
Operating Systems, OS Service, System Calls, OS structure:
Layered, Monolithic, Microkernel Operating Systems – Concept of
Virtual Machine.

Introduction to Operating System:


⮚ An operating system is software that manages the basic
resources of a computer i.e Processor, Memory, Files, Peripheral
devices, disk storage units etc…
⮚ It provides an environment in which user can execute
programs in a convenient and efficient manner.
⮚ Purpose of OS is to transfer control automatically from one job
to next job.
⮚ OS performs the job of allocating resources for various tasks
such as program execution, file creation, command execution etc.
and reclaims the allocated resources after their use.
⮚ OS allocates Main Memory (MM) for the running program to
the CPU, Schedules them and manage the data transfer to and
from peripheral (I/O) devices to MM and Secondary storage
(disks) units.

User Views:-
The user view of the computer depends on the interface used. ∙ Some users
may use PC’s. In this the system is designed so that only one user can
utilize the resources and mostly for ease of use where the attention is
mainly on performances and not on the resource utilization.
∙ Some users may use a terminal connected to a mainframe or
minicomputers.
∙ Other users may access the same computer through other terminals.
These users may share resources and exchange information. In this case
the OS is designed to maximize resource utilization-so that all available
CPU time, memory & I/O are used efficiently.
∙ Other users may sit at workstations, connected to the networks of other
workstations and servers. In this case OS is designed to compromise
between individual visibility & resource utilization.

System Views:
∙ We can view a system as resource allocator i.e. a computer system has
many resources that may be used to solve a problem. The OS acts as a
manager of these resources. The OS must decide how to allocate these
resources to programs and the users so that it can operate the Computer
system efficiently and fairly.
∙ A different view of an OS is that it need to control various I/O devices &
user programs i.e. an OS is a control program used to manage the
execution of user program to prevent errors and improper use of the
computer.
∙ Resources can be either CPU Time, memory space, file storage space, I/O
devices and so on. The OS must support the following tasks

Types of Operating Systems

1. Batch Operating System.


2. Multiprogrammed Operating System
3. Time-Sharing Operating Systems
4. Parallel Systems
5. Distributed Operating System
6. Real-Time Operating System
7. Network Operating System

1. Batch Operating System:


This type of operating system does not interact with the computer directly.
There is an operator which takes similar jobs having same requirement
and group them into batches. It is the responsibility of operator to sort the
jobs with similar needs.

Human operators would organize jobs into batches

⮚ Early computers where physically large machines.


⮚ The common I/P devices are card readers & tape drives.
⮚ The common O/P devices are line printers, tape drives & card
punches.
⮚ The job was mainly in the form punched cards.
⮚ At later time the O/P appeared and it consisted of result along with dump
of memory and register content for debugging.
⮚ The OS of these computers was very simple.
⮚ Its major task was to transfer control from one job to the next.
⮚ The OS was always resident in the memory.
⮚ The processing of job was very slow. To improve the processing speed
operators batched together the jobs with similar needs and processed it
through the computers. This is called Batch Systems.

Drawback of First OS
The CPU may be idle for some time because the speed of the mechanical
devices is slower compared to the electronic devices.
In order to eliminate this drawback, a batch OS was used to perform the task
of batching jobs.
Advantages:
⮚ Simple, Sequential job Scheduling.
⮚ Human interventions minimized.
⮚ Increased performance & System throughput due to batching of jobs.
Disadvantages:
⮚ Turn around time can be large from user point of view due to
batching.
⮚ Difficult to debug the program.
⮚ A job can enter into infinite loop.
⮚ A job could corrupt the monitor.
⮚ Due to lack of protection scheme, one job may affect the pending
jobs.
Example: Payroll System, Bank Statements etc

Note: Turn around time means time elapsed between the time of submission of a
process or job by a user and the time of completion of that process or job.

Spooling:
⮚ SPOOL(Simultaneous Peripheral Operation On-Line)
⮚ Spooling is a process in which data is temporarily held to be used and
executed by a device, program or the system.
⮚ Data is sent to and stored in memory of other volatile (Temporary
memory) storage until the program or computer requests it for
execution.

2. Multiprogramming Operating Systems


⮚ Multiprogramming concept increases CPU utilization by organization
jobs so that the CPU always has one job to execute.
⮚ The operating system keeps several jobs in memory simultaneously as
shown in below figure.

Memory Layout for Multiprogrammed System


⮚ This set of job is subset of the jobs kept in the job pool. The operating
system picks and begins to execute one of the jobs in the memory.
⮚ When a job needs to wait the CPU is simply switched to another job
and so on.
Functions:
⮚ The multiprogramming operating system is sophisticated because the
operating system makes decisions for the user. This is known as Job
scheduling.
⮚ If several jobs are ready to run at the same time the system choose one
among them. This is known as CPU scheduling.
⮚ Other functions are Memory management, Device and File
Management.

Advantages of Multiprogramming System:


⮚ Effective Resource Utilization (CPU, Memory, Peripheral
devices). ⮚ Elimination or Minimizing of the waste CPU idle time.
⮚ Increased Throughput (The number of jobs executed against the
number of jobs submitted for execution in a given interval of time). The
Disadvantages of Multiprogramming System:
⮚ It does not provide user interaction with the computer system during
the program execution.
⮚ The introduction of disk technology solved these problems rather than
reading the cards from card reader into disk. This form of processing is
known as spooling.
⮚ Complex and quite expensive.

3. Time-Sharing Operating Systems


⮚ The time sharing system is also known as multi user systems. ⮚ The
CPU executes multiple jobs by switching among them but the switches
occurs so frequently that the user can interact with each program while it
is running.
⮚ An interactive computer system provides direct communication
between a user and system.
⮚ The user gives instruction to the operating systems or to a program
directly using keyboard or mouse and wait for immediate results. So the
response time will be short.
⮚ The time sharing system allows many users to share the computer
simultaneously. Since each action in this system is short, only a little CPU
time is needed for each user.
⮚ The system switches rapidly from one user to the next so each user feels
as if the entire computer system is dedicated to his use, even though it is
being shared by many users.

⮚ In above figure the user 5 is active but user 1, user 2, user 3, and user 4
are in waiting state whereas user 6 is in ready status.
⮚ As soon as the time slice of user 5 is completed, the control moves on to
the next ready user i.e. user 6. In this state user 2, user 3, user 4, and user
5 are in waiting state and user 1 is in ready state. The process continues
in the same way and so on.

Advantages of time sharing system:


⮚ Effective sharing and utilization of computer resources.
⮚ Quick response time to many users.
⮚ CPU idle time is eliminated completely.
⮚ Suitable for on-line Data processing and user conversations.

Disadvantages of time sharing system:


⮚ It is more complex than multiprogrammed operating system. ⮚
The system must have memory management & protection, since
several jobs are kept in memory at the same time.
⮚ Time sharing system must also provide a file system, so disk
management is required.
⮚ It provides mechanism for concurrent execution which requires
complex CPU scheduling schemes.
Example: Multics, UNIX etc.

4. Multiprocessor/Parallel/Tightly coupled Systems ⮚ These


Systems have more than one processor in close communications which
share the computer bus, clock, memory & peripheral devices
. Ex: UNIX, LINUX.
⮚ Multiprocessor Systems have 3 main advantages.
o Increased throughput: Number of processes completed per unit
time. By increasing the number of processors, move work can be
done in less time. The speed up ratio with N processors is not N, but
it is less than N. Because a certain amount of overhead is incurred
in keeping all the parts working correctly.
o Increased Reliability: If functions can be properly distributed
among several processors, then the failure of one processor will
not halt the system, but slow it down. This ability to continue to
operate in spite of failure makes the system fault tolerant.
o Economic scale: Multiprocessor systems can save money as they
can share peripherals, storage & power supplies.

The various types of multiprocessing systems are:


∙ Symmetric Multiprocessing (SMP): Each processor runs an identical
copy of the operating system & these copies communicate with one
another as required. Ex: Encore’s version of UNIX for multi max
computer. Virtually, all modern operating system including Windows
NT, Solaris, Digital UNIX, OS/2 & LINUX now provide support for SMP.

Asymmetric Multiprocessing (Master – Slave Processors): Each


processor is designed for a specific task. A master processor controls
the system & schedules & allocates the work to the slave processors.
Ex- Sun’s Operating system SUNOS version 4 provides asymmetric
multiprocessing

5. Distributed/Loosely Coupled System


⮚ In contrast to tightly coupled systems, the processors do not share
memory or a clock. Instead, each processor has its own local memory. ⮚
The processors communicate with each other by various
communication lines such as high speed buses or telephone lines. ⮚
Distributed systems depend on networking for their functionalities. By
being able to communicate distributed systems are able to share
computational tasks and provide a rich set of features to the users. ⮚
Networks vary by the protocols used, the distances between the nodes
and transport media.
⮚ TCP/IP is the most common network protocol.
⮚ The processor in a distributed system varies in size and function. It may
be microprocessors, work stations, minicomputer, and large general
purpose computers.
⮚ Network types are based on the distance between the nodes such as LAN
(within a room, floor or building) and WAN (between buildings, cities or
countries).

The advantages of distributed system are:


⮚ Resource sharing.
⮚ Computations speed up.
⮚ Load sharing/Load Balancing.
⮚ Reliability.
⮚ Communication.

Disadvantages of Distributed Operating System:


⮚ Failure of the main network will stop the entire communication. ⮚ To
establish distributed systems the language which are used are not well
defined yet.
⮚ These types of systems are not readily available as they are very
expensive. Not only that the underlying software is highly complex and
not understood well yet
Examples- LOCUS etc

6. Real-Time Operating System


⮚ Real time system is used when there are rigid time requirements on the
operation of a processor or flow of data. Sensors bring data to the
computers. The computer analyzes data and adjusts controls to modify
the sensor's inputs.
⮚ System that controls scientific experiments, medical imaging systems
and some display systems are real time systems.

The disadvantages of real time system are:


⮚ A real time system is considered to function correctly only if it returns
the correct result within the time constraints.
⮚ Secondary storage is limited or missing instead data is usually stored in
short term memory or ROM.
⮚ Advanced OS features are absent.

Real time system is of two types such as:


1. Hard real time systems:
∙ It guarantees that the critical task has been completed on time. ∙
The sudden task is takes place at a sudden instant of time. 2. Soft
real time systems:
∙ It is a less restrictive type of real time system where a critical task gets
priority over other tasks and retains that priority until it computes.
∙ These have more limited utility than hard real time systems. ∙
Missing an occasional deadline is acceptable

Example: QNX, VX works. Digital audio or multimedia is included in this


category.
⮚ It is a special purpose OS in which there are rigid time requirements on
the operation of a processor.
⮚ A real time OS has well defined fixed time constraints.
⮚ Processing must be done within the time constraint or the system will
fail.
⮚ A real time system is said to function correctly only if it returns the
correct result within the time constraint.
⮚ These systems are characterized by having time as a key
parameter.
Advantages of RTOS:
∙ Maximum Consumption: Maximum utilization of devices and system,
thus more output from all the resources
∙ Task Shifting: Time assigned for shifting tasks in these systems are very
less. For example in older systems it takes about 10 micro seconds in
shifting one task to another and in latest systems it takes 3 micro seconds.
∙ Focus on Application: Focus on running applications and less
importance to applications which are in queue.
∙ Real time operating system in embedded system: Since sizes of
programs are small, RTOS can also be used in embedded systems like in
transport and others.
∙ Error Free: These types of systems are error free.
∙ Memory Allocation: Memory allocation is best managed in these type of
systems.

Disadvantages of RTOS:
∙ Limited Tasks: Very few tasks run at the same time and their
concentration is very less on few applications to avoid errors. ∙ Use heavy
system resources: Sometimes the system resources are not so good and
they are expensive as well.
∙ Complex Algorithms: The algorithms are very complex and difficult for
the designer to write on.
∙ Device driver and interrupt signals: It needs specific device drivers and
interrupts signals to response earliest to interrupts.
∙ Thread Priority: It is not good to set thread priority as these systems are
very less pron to switching tasks.

Examples of Real-Time Operating Systems are: Scientific experiments,


medical imaging systems, industrial control systems, weapon systems, robots,
air traffic control systems, etc.
7. Network Operating System
⮚ These systems run on a server and provide the capability to manage
data, users, groups, security, applications, and other networking
functions.
⮚ This type of operating systems allows shared access of files, printers,
security, applications, and other networking functions over a small
private network.
⮚ One more important aspect of Network Operating Systems is that all the
users are well aware of the underlying configuration, of all other users
within the network, their individual connections etc. and that’s why
these computers are popularly known as tightly coupled systems.

Advantages of Network Operating System:


∙ Highly stable centralized servers
∙ Security concerns are handled through servers
∙ New technologies and hardware up-gradation are easily integrated to the
system
∙ Server access are possible remotely from different locations and types of
systems

Disadvantages of Network Operating System:


∙ Servers are costly
∙ User has to depend on central location for most operations ∙
Maintenance and updates are required regularly

Examples of Network Operating System are: Microsoft Windows Server


2003, Microsoft Windows Server 2008, UNIX, Linux, Mac OS X, Novell
NetWare, and BSD etc.
❖ Operating System Services
⮚ An operating system provides an environment for the execution of the
program. It provides some services to the programs.
⮚ The services provided by one OS may be different from other OS.
⮚ OS makes the programming task easier.

The various services provided by an operating system are as follows: ∙


Program Execution:
∙ The system must be able to load a program into main memory
partitions and to run that program.
∙ The program must be able to terminate this execution normally for
successful execution or abnormally for displaying errors.

∙ I/O Operation:
∙ Accomplish the task of Device allocation and control I/O devices. ∙
Provide for notifying device errors, device status, etc.

∙ File System Manipulation:


∙ Accomplish the task of Opening a File, Closing a File.
∙ The programs need to create and delete files by name
∙ Allows file manipulations such as Reading a file, Writing a File,
Appending a File.

∙ Communication:
∙ Accomplish the task of inter-process communication either on the
same computer system or between different computer systems on a
computer network.
∙ Provide for Message passing and shared memory access in safe mode.
∙ Error detection:
∙ The operating system should take the appropriate actions for the
occurrences of any type like arithmetic overflow; divide by zero
error, access to the illegal memory location and too large user CPU
time.
∙ Accomplish the task of error detection and recovery if any. Ex: Paper
jam or out of paper in a printer.
∙ Keep track status of CPU, Memory, I/O devices, Storage devices, File
system, networking, etc.
∙ Abort execution in case of fatal errors such as RAM parity errors,
power fluctuations, if any.

∙ Resource Allocation:
∙ Accomplish the task of resource allocation to multiple jobs. ∙ Reclaim
the allocated resources after their use or as and when the job
terminates.
∙ When multiple users are logged on to the system the resources must
be allocated to each of them.
∙ For current distribution of the resource among the various processes
the operating system uses the CPU scheduling run times which
determine which process will be allocated with the resource.

∙ Accounting:
∙ The operating system keep track of which users use how many and
which kind of computer resources.
∙ Maintain logs of system activities for performance analysis and error
recovery.

∙ Protection:
∙ Accomplish the task of protecting the system resources against
malicious use.
∙ Provide for safe computing by employing security scheme against
unauthorized access/users.
∙ Authenticate legitimate users with login passwords and registrations.
∙ The operating system is responsible for both hardware as well as
software protection.
∙ The operating system protects the information stored in a multiuser
computer system.
❖System Calls
⮚ System calls provide the interface between a process & the OS. These
are usually available in the form of assembly language instruction. ⮚
Some systems allow system calls to be made directly from a high level
language program like C, BCPL and PERL etc.
⮚ System calls occur in different ways depending on the computer in
use. System calls can be roughly grouped into 5 major categories.

1. Process Management and Control:


∙ End, abort: A running program needs to be able to has its execution
either normally (end) or abnormally (abort).
∙ Load, execute: A process or job executing one program may want to
load and executes another program.
∙ Create Process, terminate process: There is a system call specifying for
the purpose of creating a new process or job (create process or submit
job). We may want to terminate a job or process that we created
(terminates process, if we find that it is incorrect or no longer
needed).
∙ Get process attributes, set process attributes: If we create a new job or
process we should able to control its execution. This control requires the
ability to determine & reset the attributes of a job or processes (get
process attributes, set process attributes).
∙ Wait time: After creating new jobs or processes, we may need to wait
for them to finish their execution (wait time).
∙ Wait event, signal event: We may wait for a specific event to occur (wait
event). The jobs or processes then signal when that event has occurred
(signal event).
∙ Allocate, Release Memory.

2. File Management/File Manipulation/File Handling:


∙ Create file, delete file: We first need to be able to create & delete files.
Both the system calls require the name of the file & some of its attributes.
∙ Open file, close file: Once the file is created, we need to open it & use it.
We close the file when we are no longer using it.
∙ Read, write, reposition file: After opening, we may also read, write or
reposition the file (rewind or skip to the end of the file).
∙ Get file attributes, set file attributes: For either files or directories, we
need to be able to determine the values of various attributes & reset
them if necessary. Two system calls get file attribute & set file
attributes are required for their purpose.

3. Device Management:
∙ Request device, release device: If there are multiple users of the system,
we first request the device. After we finished with the device, we must
release it.
∙ Read, write, reposition: Once the device has been requested &
allocated to us, we can read, write & reposition the device.

4. Information Maintenance/Management:
∙ Get time or date, set time or date: Most systems have a system call to
return the current date & time or set the current date & time. ∙ Get system
data, set system data: Other system calls may return information about
the system like number of current users, version number of OS, amount of
free memory etc.
∙ Get process attributes, set process attributes: The OS keeps
information about all its processes & there are system calls to access this
information.

5. Communication Management:
∙ Create, Delete Connection.
∙ Send, Receive Message.
∙ Attach, Detach remote device(Mounting/Remote Login) ∙
Transfer status information(byte)

There are two modes of communication such as:


∙ Message passing model:
▪ Information is exchanged through an inter process communication
facility provided by operating system.
▪ Each computer in a network has a name by which it is known. ▪
Similarly, each process has a process name which is translated to an
equivalent identifier by which the OS can refer to it.
▪ The get host id and get processed systems calls to do this translation. ▪
These identifiers are then passed to the general purpose open & close
calls provided by the file system or to specific open connection system
call.
▪ The recipient process must give its permission for communication to
take place with an accept connection call.
▪ The source of the communication known as client & receiver
known as server exchange messages by read message & write
message system calls.
▪ The close connection call terminates the connection.

∙ Shared memory model:


▪ Processes use map memory system calls to access regions of
memory owned by other processes.
▪ They exchange information by reading & writing data in the
shared areas.
▪ The processes ensure that they are not writing to the same
location simultaneously.

❖OPERATING SYSTEM STRUCTURES


The design of operating system architecture traditionally follows the
separation of concerns principle. This principle suggests structuring the
operating system into relatively independent parts that provide simple
individual features, thus keeping the complexity of the design manageable.

1. Simple Structure

In MS-DOS, applications may bypass the operating system.

⮚ There are several commercial system that don’t have a well- defined
structure such operating systems begins as small, simple & limited
systems and then grow beyond their original scope.
⮚ MS-DOS is an example of such system. It was not divided into modules
carefully.
⮚ Another example of limited structuring is the UNIX operating system. ⮚
There was no CPU Execution Mode (user and kernel), so errors in
applications could cause the whole system to crash.

2. Monolithic Approach
⮚ A monolithic design of the operating system architecture makes no
special accommodation for the special nature of the operating system.
Although the design follows the separation of concerns, no attempt is
made to restrict the privileges granted to the individual parts of the
operating system.
⮚ The entire operating system executes with maximum privileges.
⮚ The communication overhead inside the monolithic operating system is
the same as the communication overhead inside any other software,
considered relatively low.
CP/M and DOS are simple examples of monolithic operating systems. Both
CP/M and DOS are operating systems that share a single address space with
the applications. In CP/M, the 16 bit address space starts with system
variables and the application area and ends with three parts of the
operating system, namely CCP (Console Command Processor), BDOS (Basic
Disk Operating System) and BIOS (Basic Input/Output System). In DOS, the
20 bit address space starts with the array of interrupt vectors and the
system variables, followed by the resident part of DOS and the application
area and ending with a memory block used by the video card and BIOS.

Simple Monolithic Operating Systems Example

3. Layered approach:
⮚ In the layered approach, the OS is broken into a number of layers
(levels) each built on top of lower layers. The bottom layer (layer 0) is the
hardware & top most layer (layer N) is the user interface. ⮚ The main
advantage of the layered approach is modularity.

▪ The layers are selected such that each users functions (or operations) &
services of only lower layer.
▪ This approach simplifies debugging & system verification, i.e. the first
layer can be debugged without concerning the rest of the system. Once
the first layer is debugged, its correct functioning is assumed while the
2nd layer is debugged & so on.
▪ If an error is found during the debugging of a particular layer, the error
must be on that layer because the layers below it are already debugged.
Thus the design & implementation of the system are simplified when the
system is broken down into layers.
▪ Each layer is implemented using only operations provided by lower
layers. A layer doesn’t need to know how these operations are
implemented; it only needs to know what these operations do.
▪ The layer approach was first used in the operating system. It was
defined in six layers.

The main disadvantage of the layered approach is:


▪ The main difficulty with this approach involves the careful definition of
the layers, because a layer can use only those layers below it. For
example, the device driver for the disk space used by virtual memory
algorithm must be at a level lower than that of the memory
management routines, because memory management requires
the ability to use the disk space.
▪ It is less efficient than a non layered system (Each layer adds
overhead to the system call & the net result is a system call that
take longer time than on a non layered system).

4. Microkernel approach:
This structures the operating system by removing all nonessential
portions of the kernel and implementing them as system and
user level programs.
∙ Generally they provide minimal process and memory
management, and a communications facility.
∙ Communication between components of the OS is provided by
message passing.
The benefits of the microkernel are as follows:
∙ Extending the operating system becomes much easier.
∙ Any changes to the kernel tend to be fewer, since the kernel is
smaller.
∙ The microkernel also provides more security and reliability.

Main disadvantage is poor performance due to increased system


overhead from message passing.

❖Virtual Machines
⮚ A virtual machine takes the layered approach to its logical conclusion.
⮚ It treats hardware and the operating system kernel as though they
were all hardware.
⮚ A virtual machine provides an interface identical to the underlying
bare hardware.
⮚ The operating system creates the illusion of multiple processes, each
executing on its own processor with its own (virtual) memory. ⮚ The
resources of the physical computer are shared to create the virtual
machines.
⮚ CPU scheduling can create the appearance that users have their own
processor.
⮚ Spooling and a file system can provide virtual card readers and virtual
line printers.
⮚ A normal user time-sharing terminal serves as the virtual machine
operator’s console.

Advantages and Disadvantages of Virtual Machines:


∙ The virtual-machine concept provides complete protection of system
resources since each virtual machine is isolated from all other virtual
machines. This isolation, however, permits no direct sharing of
resources.
∙ A virtual-machine system is a perfect vehicle for operating-systems
research and development. System development is done on the virtual
machine, instead of on a physical machine and so does not disrupt
normal system operation.
∙ The virtual machine concept is difficult to implement due to the effort
required to provide an exact duplicate to the underlying machine.

Drawback:
∙ Virtual Machine includes the increased system overhead which is due
to simulation of virtual machine operation heavily.
∙ The efficiency of VM OS depends upon the number of operations that
must be simulated by VM monitor.
UNIT II
Process Management Process Definition , Process Relationship , Process
states , Process State transitions , Process Control Block , Context
switching , Threads, Concept of multithreads , Benefits of threads, Types
of threads.
Process Scheduling: Definition, Scheduling objectives, Types of
Schedulers, CPU scheduling algorithms, performance evaluation of the
scheduling.

Process Management:
⮚ A program does nothing unless their instructions are executed by a
CPU.A process is a program in execution. A time shared user program
such as a complier is a process. A word processing program being run by
an individual user on a pc is a process.
⮚ A system task such as sending output to a printer is also a process. A
process needs certain resources including CPU time, memory files & I/O
devices to accomplish its task.
⮚ These resources are either given to the process when it is created or
allocated to it while it is running.
⮚ The OS is responsible for the following activities of process
management.
o Creating & deleting both user & system processes.
o Suspending & resuming processes.
o Providing mechanism for process synchronization.
o Providing mechanism for process communication.
o Providing mechanism for deadlock handling.

Process:
⮚ A process or task is an instance of a program in execution. ⮚ The
execution of a process must programs in a sequential manner. ⮚ At
any time at most one instruction is executed.
⮚ The process includes the current activity as represented by the value of
the program counter and the content of the processors registers. Also it
includes the process stack which contain temporary data (such as
method parameters return address and local variables) & a data section
which contain global variables. A process may also include a heap, which
is memory that is dynamically allocated during process run time.
Difference between process & program:
⮚ A program by itself is not a process.
⮚ A program in execution is known as a process.
⮚ A program is a passive entity, such as the contents of a file stored on
disk whereas process is an active entity with a program counter
specifying the next instruction to execute and a set of associated
resources may be shared among several process with some scheduling
algorithm being used to determinate when the stop work on one process
and service a different one.

Process Relationship:

Process States
As a process executes, it changes state. The state of a process is defined by the
correct activity of that process. Each process may be in one of the following
states.
∙ New: The process is being created.
∙ Ready: The process is waiting to be assigned to a processor. ∙
Running: Instructions are being executed.
∙ Waiting: The process is waiting for some event to occur. ∙
Terminated: The process has finished execution.

Process State transitions


Many processes may be in ready and waiting state at the same time.
But only one process can be running on any processor at any instant.
Process Control Block
⮚ Each process is represented in the OS by a process control block. It is
also by a process control block.
⮚ It is also known as task control block.
⮚ A process control block contains many pieces of information associated
with a specific process.
⮚ It includes the following information’s.
o Process state: The state may be new, ready, running, waiting or
terminated state.
o Program counter: It indicates the address of the next instruction
to be executed for this purpose.
o CPU registers: The registers vary in number & type depending on
the computer architecture. It includes accumulators, index
registers, stack pointer & general purpose registers, plus any
condition- code information must be saved when an interrupt
occurs to allow the process to be continued correctly after- ward.
o CPU scheduling information: This information includes process
priority pointers to scheduling queues & any other scheduling
parameters.
o Memory management information: This information may
include such information as the value of the bar & limit registers,
the page tables or the segment tables, depending upon the memory
system used by the operating system.
o Accounting information: This information includes the amount
of CPU and real time used, time limits, account number, job or
process numbers and so on.
o I/O Status Information: This information includes the list of
I/O devices allocated to this process, a list of open files and so on.
The PCB simply serves as the repository for any information that
may vary from process to process.

Context Switching
1. 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. This is
known as context switch.
2. Context-switch time is overhead; the system does no useful work while
switching.
3. Switching speed varies from machine to machine, depending on the
memory speed, the number of registers that must be copied, and the
existence of special instructions. A typical speed is a few milliseconds. 4.
Context switch times are highly dependent on hardware support.

Threads
⮚ A thread is a flow of execution through the process code, with its own
program counter, system registers and stack.
⮚ Threads are a popular way to improve application performance
through parallelism.
⮚ A thread is sometimes called a light weight process.
⮚ Threads represent a software approach to improving performance of
operating system by reducing the over head thread is equivalent to a
classical process.
⮚ Each thread belongs to exactly one process and no thread can exist
outside a process.
⮚ Each thread represents a separate flow of control.

Threads have been successfully used in implementing network servers. They


also provide a suitable foundation for parallel execution of applications on
shared memory multiprocessors.
Concept of Multithreads
⮚ Some operating systems provide a combined user level thread and
Kernel level thread facility.
⮚ Solaris is a good example of this combined approach. 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:


1. Many to many relationship.
2. Many to one relationship.
3. One to one relationship.

1. Many to Many Model


∙ In this model, many user level threads multiplexes to the Kernel thread
of smaller or equal numbers.
∙ The number of Kernel threads may be specific to either a particular
application or a particular machine.

In this model, developers can create as many user threads as necessary and
the corresponding Kernel threads can run in parallels on a multiprocessor.

2. 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. When thread makes a
blocking system call, the entire process will be blocks.
∙ Only one thread can access the Kernel at a time, so multiple threads are
unable to run in parallel on multiprocessors.
If the user level thread libraries are implemented in the operating system,
that system does not support Kernel threads use the many to one
relationship modes.

3. 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 allows another thread to run when a thread makes a blocking system call.
It supports multiple threads to execute in parallel on microprocessors.

Disadvantage of this model is that creating user thread requires the


corresponding Kernel thread. OS/2, Windows NT and windows 2000 use
one to one relationship model.
Benefits of Threads
The benefits of multithreaded programming can be broken down into four
major categories:
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. This quality
is especially useful in designing user interfaces. For instance, consider what
happens when a user clicks a button that results in the performance of a time-
consuming operation. A single-threaded application would be unresponsive
to the user until the operation had completed. In contrast, if the time-
consuming operation is performed in a separate thread, the application
remains responsive 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. However, threads share the memory
and the resources of the process to which they belong by default.
The benefit of sharing code and data is that it allows an application to have
several different threads of activity within the same address space.

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. Empirically gauging
the difference in overhead can be difficult, but in general it is significantly
more time consuming to create and manage processes than threads. In
Solaris, for example, creating a process is about thirty time’s slower than is
creating a thread, and context switching is about five times slower.

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. A single-threaded process can run on only one
processor, regardless how many are available. We explore this issue further
in the following section.
Types of Threads
Thread is implemented in two ways:
1. User Level
2. Kernel Level

1. User Level Thread


∙ In a user thread, all of the work of thread management is done by the
application and the kernel is not aware of the existence of threads. ∙ The
thread library contains code for creating and destroying threads, for
passing message and data between threads, for scheduling thread
execution and for saving and restoring thread contexts.
∙ The application begins with a single thread and begins running in that
thread.
∙ User level threads are generally fast to create and manage.

Advantage of user level thread over Kernel level thread: ▪


Thread switching does not require Kernel mode privileges. ▪
User level thread can run on any operating system.
▪ Scheduling can be application specific.
▪ User level threads are fast to create and manage.

Disadvantages of user level thread:


▪ In a typical operating system, most system calls are blocking. ▪
Multithreaded application cannot take advantage of multiprocessing.

2. Kernel Level Threads


∙ In Kernel level thread, thread management done by the Kernel. ∙ There is
no thread management code in the application area. ∙ Kernel threads are
supported directly by the operating system. ∙ Any application can be
programmed to be multithreaded. All of the
threads within an application are supported within a single process. ∙
The Kernel maintains context information for the process as a whole and
for individual threads within the process.
∙ Scheduling by the Kernel is done on a thread basis.
∙ The Kernel performs thread creation, scheduling and management in
Kernel space.
∙ Kernel threads are generally slower to create and manage than the user
threads.
Advantages of Kernel level thread:
▪ Kernel can simultaneously schedule multiple threads from the same
process on multiple processes.
▪ If one thread in a process is blocked, the Kernel can schedule another
thread of the same process.
▪ Kernel routines themselves can multithreaded.

Disadvantages:
▪ Kernel threads are generally slower to create and manage than the user
threads.
▪ Transfer of control from one thread to another within same process
requires a mode switch to the Kernel.

Benefits/Advantages of Thread
⮚ Thread minimizes context switching time.
⮚ Use of threads provides concurrency within a process.
⮚ Efficient communication.
⮚ Economy- It is more economical to create and context switch
threads. ⮚ Utilization of multiprocessor architectures –
The benefits of multithreading can be greatly increased in a multiprocessor
architecture.

Difference between User Level & Kernel Level Thread


1 User level threads are faster Kernel level threads are slower
to create and manage to create and manage.

2 Implemented by a thread library Operating system support


at the user level directly to Kernel threads
3 User level thread can run on Kernel level threads are specific
any operating system to the operating system
4 Support provided at the user Support may be provided by
level called user level thread kernel is called Kernel level
threads
5 Multithread application cannot Kernel routines themselves can
take advantage of be multithreaded
multiprocessing

Difference between Process and Thread


1 Process is called heavy Thread is called light weight
weight process Process
2 Process switching needs Thread switching does not need to
interface with operating system call a operating system and cause
an interrupt to the Kernel
3 In multiple process All threads can share same set
implementation each process of open files, child processes
executes the same code but has
its own memory and file
resources
4 If one server process is blocked While one server thread is
no other server process can blocked and waiting, second
execute until the first process thread in the same task could
unblocked run
5 Multiple redundant process Multiple threaded process uses
uses more resources than fewer resources than multiple
multiple threaded redundant process

6 In multiple process each One thread can read, write or


process operates even completely wipe out
independently of the others another threads stack

Process Scheduling:
Scheduling is a fundamental function of OS. When a computer is
Multiprogrammed, it has multiple processes competing for the
CPU at the same time. If only one CPU is available, then a choice
has to be made regarding which process to execute next. This
decision making process is known as scheduling and the part of
the OS that makes this choice is called a scheduler. The
algorithm it uses in making this choice is called scheduling
algorithm.

Scheduling queues: As processes enter the system, they are put


into a job queue. This queue consists of all process in the system.
The process that are residing in main memory and are ready &
waiting to execute or kept on a list called ready queue.
This queue is generally stored as a linked list. A ready queue header contains
pointers to the first & final PCB in the list. The PCB includes a pointer field that
points to the next PCB in the ready queue. The lists of processes waiting for a
particular I/O device are kept on a list called device queue. Each device has
its own device queue. A new process is initially put in the ready queue. It waits
in the ready queue until it is selected for execution & is given the CPU.
SCHEDULERS:
⮚ A process migrates between the various scheduling queues throughout
its life-time purposes. The OS must select for scheduling processes from
these queues in some fashion. This selection process is carried out by
the appropriate scheduler.
⮚ In a batch system, more processes are submitted and then executed
immediately. So these processes are spooled to a mass storage device
like disk, where they are kept for later execution.
Types of schedulers:
There are 3 types of schedulers mainly used:
1. Long term scheduler:
∙ Long term scheduler selects process from the disk & loads them into
memory for execution.
∙ It controls the degree of multi-programming i.e. no. of processes in
memory.
∙ It executes less frequently than other schedulers.
∙ If the degree of multiprogramming is stable than the average rate of
process creation is equal to the average departure rate of processes
leaving the system.
∙ So, the long term scheduler is needed to be invoked only when a process
leaves the system.
∙ Due to longer intervals between executions it can afford to take more
time to decide which process should be selected for execution. ∙ Most
processes in the CPU are either I/O bound or CPU bound. ∙ An I/O bound
process (an interactive ‘C’ program) is one that spends most of its time in
I/O operation than it spends in doing I/O operation. ∙ A CPU bound
process is one that spends more of its time in doing computations than
I/O operations (complex sorting program). ∙ It is important that the long
term scheduler should select a good mix of I/O bound & CPU bound
processes.

2. Short - term scheduler:


∙ The short term scheduler selects among the process that are ready to
execute & allocates the CPU to one of them.
∙ The primary distinction between these two schedulers is the frequency
of their execution.
∙ The short-term scheduler must select a new process for the CPU quite
frequently.
∙ It must execute at least one in 100ms. Due to the short duration of time
between executions, it must be very fast.

3. Medium - term scheduler:


∙ Some operating systems introduce an additional intermediate level of
scheduling known as medium - term scheduler.
∙ The main idea behind this scheduler is that sometimes it is
advantageous to remove processes from memory & thus reduce the
degree of multiprogramming.
∙ At some later time, the process can be reintroduced into memory & its
execution can be continued from where it had left off. This is called as
swapping.
∙ The process is swapped out & swapped in later by medium term
scheduler.
∙ Swapping is necessary to improve the process miss or due to some change
in memory requirements, the available memory limit is exceeded which
requires some memory to be freed up.
Scheduling objectives:
1. CPU utilization – keep the CPU as busy as possible
2. Throughput – Number of processes that complete their execution per
time unit
3. Turnaround time – amount of time to execute a particular process 4.
Waiting time – amount of time a process has been waiting in the ready
queue
5. Response time – amount of time it takes from when a request was
submitted until the first response is produced, not output (for time
sharing environment)

General Goals
Fairness
Fairness is important under all circumstances. A scheduler makes sure
that each process gets its fair share of the CPU and no process can suffer
indefinite postponement.
Note that giving equivalent or equal time is not fair. Think of safety
control and payroll at a nuclear plant.
Policy Enforcement
The scheduler has to make sure that system's policy is enforced. For
example, if the local policy is safety then the safety control processes must
be able to run whenever they want to, even if it means delay in payroll
processes.

Efficiency
Scheduler should keep the system (or in particular CPU) busy cent
percent of the time when possible. If the CPU and all the Input/Output
devices can be kept running all the time, more work gets done per
second than if some components are idle.
Response Time
A scheduler should minimize the response time for interactive user.

Turnaround
A scheduler should minimize the time batch users must wait for an
output.

Throughput
A scheduler should maximize the number of jobs processed per unit
time.
A little thought will show that some of these goals are contradictory. It
can be shown that any scheduling algorithm that favors some class of
jobs hurts another class of jobs. The amount of CPU time available is
finite, after all.

Preemptive Vs Non-preemptive Scheduling


The Scheduling algorithms can be divided into two categories with respect
to how they deal with clock interrupts.

Non-preemptive Scheduling
A scheduling discipline is non-preemptive if, once a process has been given
the CPU, the CPU cannot be taken away from that process.

Following are some characteristics of non-preemptive scheduling ∙ In non-


preemptive system, short jobs are made to wait by longer jobs but the
overall treatment of all processes is fair.
∙ In non-preemptive system, response times are more predictable because
incoming high priority jobs cannot displace waiting jobs.
∙ In non-preemptive scheduling, a scheduler executes jobs in the following
two situations.
o When a process switches from running state to the waiting
state. o When a process terminates.

Preemptive Scheduling
∙ A scheduling discipline is preemptive if, once a process has been given
the CPU can taken away.
∙ The strategy of allowing processes that are logically runnable to be
temporarily suspended is called Preemptive Scheduling and it is
contrast to the "run to completion" method.

CPU scheduling algorithms:


CPU Scheduling deals with the problem of deciding which of the processes in
the ready queue is to be allocated the CPU.
Following are some scheduling algorithms we will study.
1. FCFS Scheduling.
2. Round Robin Scheduling.
3. SJF Scheduling.
4. SRT Scheduling.
5. Priority Scheduling.
6. Multilevel Queue Scheduling.
7. Multilevel Feedback Queue Scheduling.

1. First Come, First Served Scheduling (FCFS) Algorithm: ∙ This is the


simplest CPU scheduling algorithm. In this scheme, the process which
requests the CPU first, that is allocated to the CPU first. ∙ The
implementation of the FCFS algorithm is easily managed with a FIFO
queue.
∙ When a process enters the ready queue its PCB is linked onto the rear of
the queue. The average waiting time under FCFS policy is quiet long.

Consider the following example:


Process CPU time
P1 3
P2 5
P3 2
P4 4
Using FCFS algorithm find the average waiting time and average turnaround
time if the order is P1, P2, P3, P4.
Solution: If the process arrived in the order P1, P2, P3, P4 then according to
the FCFS the Gantt chart will be:
P1 P2 P3 P4

0 3 8 10 14

Waiting time for process P1 = 0, P2 = 3, P3 = 8, P4 = 10


Turnaround time for process P1 = 0 + 3 = 3, P2 = 3 + 5 = 8, P3
= 8 + 2 = 10, P4 = 10 + 4 =14.
Average waiting time = (0 + 3 + 8 + 10)/4
= 21/4
= 5.25
Average Turnaround time = (3 + 8 + 10 + 14)/4
= 35/4
= 8.75
The FCFS algorithm is non preemptive means once the CPU has been allocated
to a process then the process keeps the CPU until the release the CPU either
by terminating or requesting I/O.

2. Shortest Job First Scheduling (SJF) Algorithm:


∙ Other name of this algorithm is Shortest-Process-Next (SPN). ∙ This
algorithm associates with each process if the CPU is available. ∙ This
scheduling is also known as shortest next CPU burst, because the
scheduling is done by examining the length of the next CPU burst of the
process rather than its total length.

Consider the following example:


Process CPU time
P1 3
P2 5
P3 2
P4 4
Solution: According to the SJF the Gantt chart will be
P3 P1 P2 P4

0 2 5 9 14

Waiting time for process P1 = 0, P2 = 2, P3 = 5, P4 = 9


Turnaround time for process P3 = 0 + 2 = 2, P1 = 2 + 3 = 5,
P4 = 5 + 4 = 9, P2 = 9 + 5 =14.
Average waiting time = (0 + 2 + 5 + 9)/4 = 16/4 = 4 Average
Turnaround time = (2 + 5 + 9 + 14)/4 = 30/4 = 7.5

The SJF algorithm may be either preemptive or non preemptive algorithm.

The preemptive SJF is also known as shortest remaining time


first. Consider the following example.
Process Arrival Time CPU Time
P1 0 8
P2 1 4
P3 2 9
P4 3 5
In this case the Gantt chart will be
P1 P2 P4 P1 P3

0 1 5 10 17 26

The Waiting time for process


P1 = 10 - 1 = 9
P2 = 1 – 1 = 0
P3 = 17 – 2 = 15
P4 = 5 – 3 = 2
The Average Waiting time = (9 + 0 + 15 + 2)/4 = 26/4 = 6.5

3. Priority Scheduling Algorithm:


∙ In this scheduling a priority number (integer) is associated with each
process and the CPU is allocated to the process with the highest
priority (smallest integer, highest priority).
o Preemptive
o Non-preemptive
∙ Equal priority processes are scheduled in FCFS manner. ∙ SJF is a
priority scheduling where priority is the predicted next CPU burst
time.
∙ Problem {Starvation – low priority processes may never execute. ∙
Solution { Aging – as time progresses increase the priority of the
process
∙ Priority can be defined either internally or externally.

Examples of Internal priorities are


▪ Time limits.
▪ Memory requirements.
▪ File requirements, for example, number of open files.
▪ CPU Vs I/O requirements.
Externally defined priorities are set by criteria that are external to operating
system such as
▪ The importance of process.
▪ Type or amount of funds being paid for computer use.
▪ The department sponsoring the work.
▪ Politics.
Consider the following example:
Process Arrival Time CPU Time P1 10 3
P2 1 1
P3 2 3
P4 1 4
P5 5 2

According to the priority scheduling the Gantt chart will be


P2 P5 P1 P3 P4

0 1 6 16 18 19 The Waiting time for process


P1 = 6
P2 = 0
P3 = 16
P4 = 18
P5 = 1
The Average Waiting time = (0 + 1 + 6 + 16 + 18)/5 = 41/5 = 8.2

4. Round Robin Scheduling Algorithm:


∙ This type of algorithm is designed only for the time sharing system. ∙
It is similar to FCFS scheduling with preemption condition to switch
between processes.
∙ A small unit of time called quantum time or time slice is used to switch
between the processes.
∙ The average waiting time under the round robin policy is quiet long.
Consider the following example:
Process CPU time
P1 3
P2 5
P3 2
P4 4
Time Slice = 1 millisecond.
P1 P2 P3 P4 P1 P2 P3 P4 P1 P2 P4 P2 P4 P2

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

The Waiting Time for process


P1 = 0 + (4 – 1) + (8 – 5)
=0+3+3
=6
P2 = 1 + (5 – 2) + (9 – 6) + (11 – 10) + (13 – 12)
=1+3+3+1+1
=9
P3 = 2 + (6 – 3)
=2+3
=5
P4 = 3 + (7 – 4) + (10 – 8) + (12 – 11)
=3+3+2+1
=9

The Average Waiting time = (6 + 9 + 5 + 9)/4


= 7.2

5. Shortest-Remaining-Time (SRT) Scheduling


∙ The SRT is the preemptive counterpart of SJF and useful in time
sharing environment.
∙ In SRT scheduling, the process with the smallest estimated run-time to
completion is run next, including new arrivals.
∙ In SJF scheme, once a job begins executing, it run to completion. ∙ In
SJF scheme, a running process may be preempted by a new arrival
process with shortest estimated run-time.
∙ The algorithm SRT has higher overhead than its counterpart SJF. ∙
The SRT must keep track of the elapsed time of the running process
and must handle occasional preemptions.
∙ In this scheme, arrival of small processes will run almost immediately.
However, longer jobs have even longer mean waiting time.
6. Multilevel Queue Scheduling
∙ A multilevel queue scheduling algorithm partitions the ready queue in
several separate queues, for instance, in a multilevel queue scheduling
processes are permanently assigned to one queues.
∙ The processes are permanently assigned to one another, based on some
property of the process, such as Memory size, Process priority, Process
type.
∙ Algorithm choose the process from the occupied queue that has the
highest priority, and run that process either
7. Multilevel Feedback Queue Scheduling.
∙ Multilevel feedback queue-scheduling algorithm allows a process to
move between queues.
∙ It uses many ready queues and associates a different priority with each
queue.
∙ The Algorithm chooses to process with highest priority from the
occupied queue and run that process either preemptively or non
preemptively.
∙ If the process uses too much CPU time it will moved to a lower-priority
queue.
∙ Similarly, a process that wait too long in the lower-priority queue may be
moved to a higher-priority queue may be moved to a highest priority
queue.
Note that this form of aging prevents starvation.
Example:
▪ A process entering the ready queue is placed in queue 0. ▪ If it does
not finish within 8 milliseconds time, it is moved to the tail of queue 1.
▪ If it does not complete, it is preempted and placed into queue 2. ▪
Processes in queue 2 run on a FCFS basis, only when 2 run on a FCFS
basis queue, only when queue 0 and queue 1 are empty.
Three queues:
Q0 – RR with time quantum 8 milliseconds
Q1 – RR time quantum 16 milliseconds
Q2 – FCFS

In general, 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
Performance evaluation of the scheduling:
UNIT III
Inter-process Communication, Race Conditions, Critical Section, Mutual
Exclusion, Hardware Solution, Strict Alternation, Peterson’s Solution, The
Producer Consumer Problem, Semaphores, Event Counters, Monitors,
Message Passing, and Classical IPC Problems.
Deadlocks: Definition, Deadlock characteristics, Deadlock Prevention,
Deadlock Avoidance (concepts only).

Inter-process Communication:
∙ Inter process communication (IPC) is a mechanism which allows
processes to communicate each other and synchronize their actions. The
communication between these processes can be seen as a method of co-
operation between them.
∙ 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. Clearly, any process that shares data
with other processes is a cooperating process.
∙ Reasons for co-operating processes:
o 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.

o 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 cores.

o Modularity: We may want to construct the system in a modular


fashion, dividing the system functions into separate processes or
threads.

o Convenience: Even an individual user may work on many tasks at


the same time. For instance, a user may be editing, listening to
music and compiling in parallel.
∙ There are two fundamental models of inter-process communication:
o Shared Memory –
▪ In the shared-memory model, a region of memory that is
shared by cooperating processes is established.
▪ Processes can then exchange information by reading and
writing data to the shared region.
▪ 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.
o Message Passing –
▪ 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 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.
▪ Recall that, normally, 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.
▪ The form of the data and the location are determined by these processes
and are not under the operating system’s control.
▪ The processes are also responsible for ensuring that they are not writing to
the same location simultaneously.

Race Conditions:
▪ A situation like this, 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.

⮚ Suppose that two processes, P1 and P2, share the global variable a. At
some point in its execution, P1 updates a to the value 1, and at some
point in its execution, P2 updates a to the value 2. Thus, the two tasks
are in a race to write variable a.
⮚ In this example the "loser" of the race (the process that updates last)
determines the final value of a.

Therefore Operating System Concerns of following things 1. The


operating system must be able to keep track of the various processes.
2. The operating system must allocate and deallocate various
resources for each active process.
3. The operating system must protect the data and physical resources
of each process against unintended interference by other processes. 4.
The functioning of a process, and the output it produces, must be
independent of the speed at which its execution is carried out relative
to the speed of other concurrent processes.

❖ Process Interaction can be defined as


• Processes unaware of each other
• Processes indirectly aware of each other
• Processes directly aware of each other

❖ Concurrent processes come into conflict with each other when


they are
∙ Competing for the use of the same resource.
∙ Two or more processes need to access a resource during the course of
their execution.
∙ Each process is unaware of the existence of the other processes. ∙
There is no exchange of information between the competing processes.

Critical Section Problem:

Consider a system consisting of n processes (P0, P1, ……… ,Pn-1) each process
has a segment of code which is known as critical section in which the process
may be changing common variable, updating a table, writing a file and so on.
∙ The important feature of the system is that when the process is executing
in its critical section no other process is to be allowed to execute in its
critical section.
∙ The execution of critical sections by the processes is a mutually
exclusive.
∙ The critical section problem is to design a protocol that the process can
use to co-operate each process must request permission to enter its
critical section.
∙ The section of code implementing this request is the entry section. The
critical section is followed on exit section. The remaining code is the
remainder section.

Example:
While (1)
{
Entry Section;
Critical Section;
Exit Section;
Remainder Section;
}
A solution to the critical section problem must satisfy the following three
conditions.
1. Mutual Exclusion: If process Pi is executing in its critical section
then no any other process can be executing in their critical section.

2. Progress: If no process is executing in its critical section and some


process wish to enter their critical sections then only those process
that are not executing in their remainder section can enter its critical
section next.

3. Bounded waiting: There exists a bound on the number of times that


other processes are allowed to enter their critical sections after a
process has made a request.

Mutual Exclusion:
Requirements for Mutual Exclusion:
1. Mutual exclusion must be enforced: Only one process at a time is
allowed into its critical section, among all processes that have critical
sections for the same resource or shared object.
2. A process that halts in its non critical section must do so without
interfering with other processes.
3. It must not be possible for a process requiring access to a critical
section to be delayed indefinitely: no deadlock or starvation. 4. When no
process is in a critical section, any process that requests entry to its
critical section must be permitted to enter without delay. 5. No
assumptions are made about relative process speeds or number of
processors.
6. A process remains inside its critical section for a finite time only.

Hardware Solution:
Hardware approaches to mutual exclusion.

∙ Interrupt Disabling:
In a uniprocessor machine, concurrent processes cannot be overlapped; they
can only be interleaved. Furthermore, a process will continue to run until it
invokes an operating system service or until it is interrupted. Therefore, to
guarantee mutual exclusion, it is sufficient to prevent a process from being
interrupted. This capability can be provided in the form of primitives
defined by the system kernel for disabling and enabling interrupts.
Solution to Critical-section Problem Using Locks
do
{
acquire lock
critical section;
release lock
remainder section;
} while (TRUE);

Because the critical section cannot be interrupted, mutual exclusion is


guaranteed.

Disadvantages
∙ It works only in a single processor environment.
∙ Interrupts can be lost if not serviced promptly.
∙ A process waiting to enter its critical section could suffer from
starvation.

⮚ Test and Set Instruction


∙ It is special machine instruction used to avoid mutual exclusion. ∙
The test and set instruction can be defined as follows:

Definition:
boolean TestAndSet (boolean *target)
{
boolean rv = *target;
*target = TRUE;
return rv:
}

The above function is carried out automatically.

Solution using TestAndSet


Shared boolean variable lock initialized to false.

Solution:
do {
while ( TestAndSet (&lock ))
; // do nothing
// critical section
lock = FALSE;
// remainder section
} while (TRUE);

Advantages
1. It is simple and easy to verify.
2. It is applicable to any number of processes. 3. It
can b used to support multiple critical section.

Disadvantages
1. Busy waiting is possible.
2. Starvation is also possible.
3. There may be deadlock.

Swap Instruction:
Definition:
void Swap (boolean *a, boolean *b)
{
boolean temp = *a;
*a = *b;
*b = temp:
}

Solution using Swap


Shared Boolean variable lock initialized to FALSE;
Each process has a local Boolean variable key

Solution:
do {
key = TRUE;
while ( key == TRUE)
Swap (&lock, &key);

// critical section
lock = FALSE;
// remainder section
} while (TRUE);
Bounded-waiting Mutual Exclusion with TestandSet()
do {
waiting[i] = TRUE;
key = TRUE;
while (waiting[i] && key)
key = TestAndSet(&lock);
waiting[i] = FALSE;
// critical section
j = (i + 1) % n;
while ((j != i) && !waiting[j])
j = (j + 1) % n;
if (j == i)
lock = FALSE;
else
waiting[j] = FALSE;
// remainder section
} while (TRUE);

Peterson’s Solution:

The mutual exclusion problem is to devise a pre-protocol (or entry protocol)


and a post-protocol (or exist protocol) to keep two or more threads from
being in their critical sections at the same time. Tanenbaum examine
proposals for critical-section problem or mutual exclusion problem.

Problem
When one process is updating shared modifiable data in its critical section,
no other process should allowed to enter in its critical section.

❖ Proposal 1 -Disabling Interrupts (Hardware Solution)


Each process disables all interrupts just after entering in its critical section
and re-enable all interrupts just before leaving critical section. With
interrupts turned off the CPU could not be switched to other process. Hence,
no other process will enter its critical and mutual exclusion achieved.

Conclusion
Disabling interrupts is sometimes a useful interrupts is sometimes a useful
technique within the kernel of an operating system, but it is not appropriate
as a general mutual exclusion mechanism for user process. The reason is
that it is unwise to give user process the power to turn off interrupts.

❖ Proposal 2 -Lock Variable (Software Solution)

In this solution, we consider a single, shared, (lock) variable, initially 0. When


a process wants to enter in its critical section, it first test the lock. If lock is 0,
the process first sets it to 1 and then enters the critical section. If the lock is
already 1, the process just waits until (lock) variable becomes 0. Thus, a 0
means that no process in its critical section, and 1 means hold your horses -
some process is in its critical section.

Conclusion
The flaw in this proposal can be best explained by example. Suppose process
A sees that the lock is 0. Before it can set the lock to 1 another process B is
scheduled, runs, and sets the lock to 1. When the process A runs again, it will
also set the lock to 1, and two processes will be in their critical section
simultaneously.

❖Proposal 1 -Strict Alternation

In this proposed solution, the integer variable 'turn' keeps track of whose turn
is to enter the critical section. Initially, process A inspects turn, finds it to be
0, and enters in its critical section. Process B also finds it to be 0 and sits in a
loop continually testing 'turn' to see when it becomes 1.
Continuously testing a variable waiting for some value to appear is called the
Busy-Waiting.

Conclusion:
Taking turns is not a good idea when one of the processes is much slower than
the other. Suppose process 0 finishes its critical section quickly, so both
processes are now in their noncritical section. This situation violates above
mentioned condition 3.
The Producer Consumer Problem:
∙ A producer process produces information that is consumed by a
consumer process.
∙ For example, a compiler may produce assembly code that is consumed by
an assembler. The assembler, in turn, may produce object modules that
are consumed by the loader.
∙ The producer–consumer problem also provides a useful metaphor for
the client–server paradigm.
∙ One solution to the producer–consumer problem uses shared memory ∙
To allow producer and consumer processes to run concurrently, we must
have available a buffer of items that can be filled by the producer and
emptied by the consumer.
∙ This buffer will reside in a region of memory that is shared by the
producer and consumer processes. A producer can produce one item
while the consumer is consuming another item.
∙ The producer and consumer must be synchronized, so that the consumer
does not try to consume an item that has not yet been produced.
∙ Two types of buffers can be used.
o Unbounded buffer
o Bounded buffer
∙ The unbounded buffer places no practical limit on the size of the buffer.
The consumer may have to wait for new items, but the producer can
always produce new items.
∙ The bounded buffer assumes a fixed buffer size. In this case, the consumer
must wait if the buffer is empty, and the producer must wait if the buffer
is full.
∙ Let’s look more closely at how the bounded buffer illustrates inter
process communication using shared memory. The following variables
reside in a region of memory shared by the producer and consumer
processes.
#define BUFFER SIZE 10
typedef struct {
...
}item;
item buffer[BUFFER SIZE];
int in = 0;
int out = 0;
∙ The shared buffer is implemented as a circular array with two logical
pointers: in and out.
∙ The variable in points to the next free position in the buffer; ∙
The variable out points to the first full position in the buffer.
∙ The buffer is empty when in ==out;
∙ The buffer is full when ((in + 1) % BUFFER SIZE) == out. ∙ The
producer process has a local variable next produced in which the new
item to be produced is stored.
∙ The consumer process has a local variable next consumed in which the
item to be consumed is stored.
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;
}
The producer process using shared memory.

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 */
}
The consumer process using shared memory.

⮚ To write a C program to implement the Producer & consumer


Problem (Semaphore)
ALGORITHM:

Step 1: The Semaphore mutex, full & empty are initialized.


Step 2: In the case of producer process
i) Produce an item in to temporary variable.
ii) If there is empty space in the buffer check the mutex value for
enter into the critical section.
iii)If the mutex value is 0, allow the producer to add value in the
temporary variable to the buffer.
Step 3: In the case of consumer process
i) It should wait if the buffer is empty
ii) If there is any item in the buffer check for mutex value, if the
mutex==0, remove item from buffer
iii)Signal the mutex value and reduce the empty value by
1. iv)Consume the item.
Step 4: Print the result

PROGRAM CODING
#include<stdio.h>
int mutex=1,full=0,empty=3,x=0; main()
{
int n;
void producer();
void consumer();
int wait(int);
int signal(int);
printf("\n 1.producer\n2.consumer\n3.exit\n");
while(1) {
printf(" \nenter ur choice");
scanf("%d",&n);
switch(n)
{
case 1: if((mutex==1)&&(empty!=0))
producer();
else
printf("buffer is full\n");
break;

case 2: if((mutex==1)&&(full!=0))
consumer();
else
printf("buffer is empty");
break;
case 3: exit(0);
break;
}
}
}
int wait(int s)
{
return(--s);
}
int signal(int s)
{
return (++s);
}

void producer()
{
mutex=wait(mutex);
full=signal(full);
empty=wait(empty);
x++;
printf("\n producer produces the items %d",x);
mutex=signal(mutex);
}

void consumer()
{
mutex=wait(mutex);
full=wait(full);
empty=signal(empty);
printf("\n consumer consumes the item %d",x);
x--;
mutex=signal(mutex);
}

Semaphores:

⮚ A semaphore S is an integer variable that, apart from initialization, is


accessed only through two standard atomic operations: wait() and
signal().
⮚ The wait() operation was originally termed P (from the Dutch proberen,
“to test”); signal() was originally called V (from verhogen, “to
increment”).
⮚ The definition of wait() is as follows:
wait(S) {
while (S <= 0)
; // busy wait
S--;
}

⮚ The definition of signal() is as follows:


signal(S) {
S++;
}

The integer value of the semaphore in the wait and signal operations must be
executed indivisibly. That is, when one process modifies the semaphore value,
no other process can simultaneously modify that same semaphore value.
In addition, in the case of the wait(S), the testing of the integer value of S (S
0) and its possible modification (S := S – 1), must also be executed without
interruption.

Semaphore as General Synchronization Tool


⮚ Counting semaphore – integer value can range over an unrestricted
domain
⮚ Binary semaphore – integer value can range only between 0 and
1; ⮚ Can be simpler to implement
⮚ Also known as mutex locks Can implement a counting semaphore S as a
binary semaphore
⮚ Provides mutual exclusion Semaphore mutex;
// initialized to
do {
wait (mutex);
// Critical Section
signal (mutex);
} while (TRUE);

Semaphore Implementation
⮚ Must guarantee that no two processes can execute wait () and signal ()
on the same semaphore at the same time
⮚ Thus, implementation becomes the critical section problem where the
wait and signal code are placed in the critical section.
⮚ Could now have busy waiting in critical section implementation But
implementation code is short Little busy waiting if critical section rarely
occupied
⮚ Note that applications may spend lots of time in critical sections and
therefore this is not a good solution.

Semaphore Implementation with no Busy waiting:


∙ With each semaphore there is an associated waiting queue. ∙
Each entry in a waiting queue has two data items:
o value (of type integer)
o pointer to next record in the list
∙ Two operations:
o block – place the process invoking the operation on the
appropriate waiting queue.
o wakeup – remove one of processes in the waiting queue and place
it in the ready queue.

Implementation of wait:
wait(semaphore *S) {
S->value--;
if (S->value < 0) {
add this process to S->list;
block();
}
}

Implementation of signal:
signal(semaphore *S) {
S->value++;
if (S->value <= 0) {
remove a process P from S->list;
wakeup(P);
}
}

Semaphores are not provided by hardware. But they have several attractive
properties:
1. Semaphores are machine independent.
2. Semaphores are simple to implement.
3. Correctness is easy to determine.
4. Can have many different critical sections with different semaphores.
5. Semaphore acquires many resources simultaneously.

Drawback of Semaphore
1. They are essentially shared global variables.
2. Access to semaphores can come from anywhere in a program.
3. There is no control or guarantee of proper usage.
4. There is no linguistic connection between the semaphore and the data
to which the semaphore controls access.
5. They serve two purposes, mutual exclusion and scheduling constraints.

Monitors:
Although semaphores provide a convenient and effective mechanism for
process synchronization, using them incorrectly can result in timing errors
that are difficult to detect, since these errors happen only if particular
execution sequences take place and these sequences do not always occur.

⮚ The monitor is a programming-language construct that provides


equivalent functionality to that of semaphores and that is easier to
control.
⮚ The monitor construct has been implemented in a number of
programming languages, including Concurrent Pascal, Pascal-Plus,
Modula-2, Modula-3, and Java.
⮚ An abstract data type—or ADT—encapsulates data with a set of
functions to operate on that data that are independent of any specific
implementation of the ADT.
⮚ A monitor type is an ADT that includes a set of programmer defined
operations that are provided with mutual exclusion within the monitor. ⮚
The monitor type also declares the variables whose values define the
state of an instance of that type, along with the bodies of functions that
operate on those variables.
⮚ It has also been implemented as a program library.
⮚ This allows programmers to put monitor locks on any object.

monitor monitor_name
{
/* shared variable declarations */
function P1 ( . . . ) {
...
}
function P2 ( . . . ) {
...
}
.
.
.
function Pn ( . . . ) {
...
}
initialization code ( . . . ) {
...
}
}
Figure: Syntax of a monitor.

∙ Thus, a function defined within a monitor can access only those variables
declared locally within the monitor and its formal parameters.
∙ Similarly, the local variables of a monitor can be accessed by only the
local functions.

∙ The monitor construct ensures that only one process at a time is active
within the monitor
∙ However, the monitor construct, as defined so far, is not sufficiently
powerful for modeling some synchronization schemes.
∙ For this purpose, we need to define additional synchronization
mechanisms.
condition x, y;
∙ The only operations that can be invoked on a condition variable are
wait() and signal().
x.wait();
∙ The operation means that the process invoking this operation is
suspended until another process invokes
x.signal();
∙ The x.signal() operation resumes exactly one suspended process. ∙ If no
process is suspended, then the signal() operation has no effect;

∙ Now suppose that, when the x.signal() operation is invoked by a process


P, there exists a suspended process Q associated with condition x.
∙ Clearly, if the suspended process Q is allowed to resume its execution,
the signaling process P must wait.
∙ Otherwise, both P and Q would be active simultaneously within the
monitor. Note, however, that conceptually both processes can continue
with their execution.

Two possibilities exist:


1. Signal and wait. P either waits until Q leaves the monitor or waits for
another condition.
2. Signal and continue. Q either waits until P leaves the monitor or waits
for another condition.

∙ On the one hand, since P was already executing in the monitor, the
signal and continue method seems more reasonable. On the other, if
we allow thread P to continue, then by the time Q is resumed, the
logical condition for which Q was waiting may no longer hold. When
thread P executes the signal operation, it immediately leaves the
monitor. Hence, Q is immediately resumed.

Message Passing:
∙ Message passing provides a mechanism to allow processes to
communicate and to synchronize their actions without sharing the same
address space.
∙ It is particularly 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:
o send(message)
o receive(message)
∙ If P and Q wish to communicate, they need to establish a communication
link between them exchange messages via send/receive
Implementation of communication link physical (e.g., shared memory,
hardware bus) logical (e.g., logical properties)

Direct Communication
∙ Processes must name each other explicitly:
o send (P, message) – send a message to process P
o 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

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

Operations
∙ create a new mailbox
∙ send and receive messages through mailbox
∙ destroy a mailbox
∙ Primitives are defined as:
o send(A, message) – send a message to mailbox A
o receive(A, message) – receive a message from mailbox A
∙ Mailbox sharing

Problem: Now suppose that processes P1, P2, and P3 all share mailbox A.
Process P1 sends a message to A, while both P2 and P3 execute a receive()
from A. Which process will receive the message sent by P1?

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.

Synchronization
∙ Message passing may be either blocking or non-blocking ∙
Blocking is considered synchronous
∙ Blocking send has the sender block until the message is received ∙
Blocking receive has the receiver block until a message is available ∙
Non-blocking is considered asynchronous
∙ Non-blocking send has the sender send the message and continue ∙
Non-blocking receive has the receiver receive a valid message or null

Buffering
Queue of messages attached to the link; implemented in one of three ways
1. Zero capacity – 0 messages 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
Classical IPC Problems:
These problems are used for testing nearly every newly proposed
synchronization scheme. In our solutions to the problems, we use
semaphores for synchronization, since that is the traditional way to present
such solutions. However, actual implementations of these solutions could use
mutex locks in place of binary semaphores.
1. The Bounded-Buffer Problem
2. The Readers–Writers Problem
3. The Dining-Philosophers Problem

1. The Bounded-Buffer Problem:


∙ It is commonly used to illustrate the power of synchronization
primitives
∙ In our problem, the producer and consumer processes share the
following data structures:
int n;
semaphore mutex = 1;
semaphore empty = n;
semaphore full = 0
∙ We assume that the pool consists of n buffers, each capable of holding
one item.
∙ The mutex semaphore provides mutual exclusion for accesses to the
buffer pool and is initialized to the value 1.
∙ The empty and full semaphores count the number of empty and full
buffers.
∙ The semaphore empty is initialized to the value n; the semaphore full is
initialized to the value 0.
do {
...
/* produce an item in next produced */
...
wait(empty);
wait(mutex);
...
/* add next produced to the buffer */
...
signal(mutex);
signal(full);
} while (true);
Fig: The structure of the producer process.
do {
wait(full);
wait(mutex);
...
/* remove an item from buffer to next consumed */
...
signal(mutex);
signal(empty);
...
/* consume the item in next consumed */
...
} while (true);
Fig: The structure of the consumer process.

2. The Readers–Writers Problem:


∙ Suppose that a database is to be shared among several concurrent
processes. Some of these processes may want only to read the database,
whereas others may want to update (that is, to read and write) the
database.
∙ If two readers access the shared data simultaneously, no adverse effects
will result.
∙ If a writer and some other process (either a reader or a writer) access
the database simultaneously, problem may arise.
∙ No reader should wait for other readers to finish simply because a
writer is waiting.
∙ Once a writer is ready, that writer performs its write as soon as possible.
∙ A solution to either problem may result in starvation. In the first case,
writers may starve; in the second case, readers may starve. ∙ In the
solution to the first readers–writers problem, the reader processes share
the following data structures:
semaphore rw mutex = 1;
semaphore mutex = 1;
int read count = 0;
∙ The semaphore rw_mutex is common to both reader and writer
processes. The mutex semaphore is used to ensure mutual exclusion
when the variable read count is updated.
∙ The read_count variable keeps track of how many processes are
currently reading the object.
∙ The semaphore rw_mutex functions as a mutual exclusion semaphore
for the writers
∙ The code for a writer process:
do {
wait(rw mutex);
...
/* writing is performed */
...
signal(rw mutex);
} while (true);
∙ The code for a reader process:
do {
wait(mutex);
read count++;
if (read count == 1)
wait(rw mutex);
signal(mutex);
...
/* reading is performed */
...
wait(mutex);
read count--;
if (read count == 0)
signal(rw mutex);
signal(mutex);
} while (true);
∙ If a writer is in the critical section and n readers are waiting, then one
reader is queued on rw_mutex, and n − 1 readers are queued on mutex.
∙ When a writer executes signal (rw_mutex), we may resume the execution
of either the waiting readers or a single waiting writer. The selection is
made by the scheduler.

3. The Dining-Philosophers Problem:


∙ The dining-philosophers problem is considered a classic
synchronization problem.
∙ Consider five philosophers who spend their lives thinking and eating. The
philosophers share a circular table surrounded by five chairs, each
belonging to one philosopher. In the center of the table is a bowl of rice,
and the table is laid with five single chopsticks
∙ When a philosopher thinks, she does not interact with her colleagues.
From time to time, a philosopher gets hungry and tries to pick up the two
chopsticks that are closest to her (the chopsticks that are between her
and her left and right neighbors).
∙ A philosopher may pick up only one chopstick at a time. ∙ She cannot
pick up a chopstick that is already in the hand of a neighbor. ∙ When a
hungry philosopher has both her chopsticks at the same time, she eats
without releasing the chopsticks.
∙ When she is finished eating, she puts down both chopsticks and starts
thinking again.
∙ A philosopher tries to grab a chopstick by executing a wait() operation
on that semaphore.
∙ She releases her chopsticks by executing the signal() operation on the
appropriate semaphores.
∙ Thus, the shared data are where all the elements of chopstick are
initialized to 1.
semaphore chopstick[5];
do {
wait(chopstick[i]);
wait(chopstick[(i+1) % 5]);
...
/* eat for awhile */
...
signal(chopstick[i]);
signal(chopstick[(i+1) % 5]);
...
/* think for awhile */
...
} while (true);
Fig: The structure of philosopher i.
∙ Although this solution guarantees that no two neighbors are eating
simultaneously, it nevertheless must be rejected because it could
create a deadlock.
∙ Suppose that all five philosophers become hungry at the same time and
each grabs her left chopstick.
∙ All the elements of chopstick will now be equal to 0.
∙ When each philosopher tries to grab her right chopstick, she will be
delayed forever.
∙ Several possible remedies to the deadlock problem are replaced by: o
Allow at most four philosophers to be sitting simultaneously at the
table.
o Allow a philosopher to pick up her chopsticks only if both
chopsticks are available (to do this, she must pick them up in a
critical section).
o Use an asymmetric solution—that is, an odd-numbered
philosopher picks up first her left chopstick and then her right
chopstick, whereas an even numbered philosopher picks up her
right chopstick and then her left chopstick.
Deadlocks
Definition: In a multiprogramming environment, several processes may
compete for a finite number of resources. A process requests resources; if the
resources are not available at that time, the process enters a waiting state.
Sometimes, a waiting process is never again able to change state, because the
resources it has requested are held by other waiting processes. This situation
is called a deadlock.

SYSTEM MODEL
• A system may consist of finite number of resources and is distributed among
number of processes. There resources are partitioned into several instances
each with identical instances.
• A process must request a resource before using it and it must release the
resource after using it. It can request any number of resources to carry out a
designated task. The amount of resource requested may not exceed the total
number of resources available.

A process may utilize the resources in only the following sequences: 1.


Request:- If the request is not granted immediately then the requesting
process must wait it can acquire the resources. 2. Use:- The process can
operate on the resource.
3. Release:- The process releases the resource after using it.

Deadlock may involve different types of resources.


Example:-
Consider a system with one printer and one tape drive. If a process Pi
currently holds a printer and a process Pj holds the tape drive. If process Pi
request a tape drive and process Pj request a printer then a deadlock occurs.
Multithread programs are good candidates for deadlock because they
compete for shared resources.

Deadlock characteristics:
Necessary Conditions: A deadlock situation can occur if the following 4
conditions occur simultaneously in a system:-
1. Mutual Exclusion: Only one process must hold the resource at a time. If
any other process requests for the resource, the requesting process
must be delayed until the resource has been released.
2. Hold and Wait:- A process must be holding at least one resource and
waiting to acquire additional resources that are currently being held by
the other process.
3. No Preemption:- Resources can’t be preempted i.e., only the process
holding the resources must release it after the process has completed its
task.
4. Circular Wait:- A set {P0,P1……..Pn} of waiting process must exist such
that P0 is waiting for a resource i.e., held by P1, P1 is waiting for a
resource i.e., held by P2. Pn-1 is waiting for resource held by process Pn
and Pn is waiting for the resource i.e., held by P1. All the four conditions
must hold for a deadlock to occur.

Resource Allocation Graph:


1. Deadlocks are described by using a directed graph called system
resource allocation graph. The graph consists of set of vertices (v) and
set of edges (e).
2. The set of vertices (v) can be described into two different types of
nodes P={P1,P2……..Pn} i.e., set consisting of all active processes and
R={R1,R2……….Rn} i.e., set consisting of all resource types in the
system.
3. A directed edge from process Pi to resource type Rj denoted by Pi->Ri
indicates that Pi requested an instance of resource Rj and is waiting. This
edge is called Request edge.
4. A directed edge Ri-> Pj signifies that resource Rj is held by process Pi.
This is called Assignment edge.
5. If the graph contain no cycle, then no process in the system is deadlock.
If the graph contains a cycle then a deadlock may exist.
6. If each resource type has exactly one instance than a cycle implies that a
deadlock has occurred. If each resource has several instances then a
cycle do not necessarily implies that a deadlock has occurred.
P3}
∙ R = {R1, R2, R3, R4}

The sets P, R, and


E: ∙ P = {P1, P2,

∙ E = {P1 → R1, P2 → R3, R1 → P2, R2 → P2, R2 → P1, R3 → P3}


Resource instances:
∙ One instance of resource type R1
∙ Two instances of resource type R2
∙ One instance of resource type R3
∙ Three instances of resource type R4
Process states:
∙ Process P1 is holding an instance of resource type R2 and is waiting for
an instance of resource type R1.
∙ Process P2 is holding an instance of R1 and an instance of R2 and is
waiting for an instance of R3.
∙ Process P3 is holding an instance of R3.

Methods for Handling Deadlocks:


There are three ways to deal with deadlock problem
⮚ We can use a protocol to prevent deadlocks ensuring that the system
will never enter into the deadlock state.
⮚ We allow a system to enter into deadlock state, detect it and recover
from it.
⮚ We ignore the problem and pretend that the deadlock never occur in
the system. This is used by most OS including UNIX.
∙ To ensure that the deadlock never occur the system can use either
deadlock avoidance or a deadlock prevention.
∙ Deadlock prevention is a set of method for ensuring that at least one of
the necessary conditions does not occur.
∙ Deadlock avoidance requires the OS is given advance information about
which resource a process will request and use during its lifetime. ∙ If a
system does not use either deadlock avoidance or deadlock prevention
then a deadlock situation may occur. During this it can provide an
algorithm that examines the state of the system to determine whether a
deadlock has occurred and algorithm to recover from deadlock.
∙ Undetected deadlock will result in deterioration of the system
performance.

Deadlock Prevention:
For a deadlock to occur each of the four necessary conditions must hold. If at
least one of the conditions does not hold then we can prevent occurrence of
deadlock.

1. Mutual Exclusion: This holds for non-sharable resources.


Eg:-A printer can be used by only one process at a time.

Mutual exclusion is not possible in sharable resources and thus they


cannot be involved in deadlock. Read-only files are good examples for
sharable resources. A process never waits for accessing a sharable resource.
So we cannot prevent deadlock by denying the mutual exclusion condition in
non-sharable resources.

2. Hold and Wait: This condition can be eliminated by forcing a process to


release all its resources held by it when it request a resource i.e., not
available.
∙ One protocol can be used is that each process is allocated with all of
its resources before its start execution.
∙ Eg:-consider a process that copies the data from a tape drive to the disk,
sorts the file and then prints the results to a printer. If all the
resources are allocated at the beginning then the tape drive, disk files
and printer are assigned to the process. The main problem with this
is it leads to low resource utilization because it requires printer at the
last and is allocated with it from the beginning so that no other
process can use it.
∙ Another protocol that can be used is to allow a process to request a
resource when the process has none. i.e., the process is allocated with
tape drive and disk file. It performs the required operation and
releases both. Then the process once again request for disk file and
the printer and the problem and with this is starvation is possible.

3. No Preemption: To ensure that this condition never occurs the resources


must be preempted. The following protocol can be used.
∙ If a process is holding some resource and request another resource that
cannot be immediately allocated to it, then all the resources currently
held by the requesting process are preempted and added to the list of
resources for which other processes may be waiting. The process will be
restarted only when it regains the old resources and the new resources
that it is requesting.
∙ When a process request resources, we check whether they are available
or not. If they are available we allocate them else we check that whether
they are allocated to some other waiting process. If so we preempt the
resources from the waiting process and allocate them to
the requesting process. The requesting process must wait.

4. Circular Wait: The fourth and the final condition for deadlock is the
circular wait condition. One way to ensure that this condition never, is to
impose ordering on all resource types and each process requests resource
in an increasing order.

Let R={R1,R2,………Rn} be the set of resource types. We assign each


resource type with a unique integer value. This will allows us to compare two
resources and determine whether one precedes the other in ordering. Eg:-we
can define a one to one function

F:R->N as follows :-F(disk drive)=5 F(printer)=12 F(tape drive)=1


Deadlock can be prevented by using the following protocol: ∙ Each
process can request the resource in increasing order. A process can request
any number of instances of resource type say Ri and it can request instances
of resource type Rj only F(Rj) > F(Ri).
∙ Alternatively when a process requests an instance of resource type Rj, it
has released any resource Ri such that F(Ri) >= F(Rj). If these two
protocols are used then the circular wait can’t hold.

Deadlock Avoidance (concepts only):


∙ Deadlock prevention algorithm may lead to low device utilization and
reduces system throughput.
∙ Avoiding deadlocks requires additional information about how resources
are to be requested. With the knowledge of the complete sequences of
requests and releases we can decide for each requests whether or not
the process should wait.
∙ For each requests it requires to check the resources currently available,
resources that are currently allocated to each processes future requests
and release of each process to decide whether the current requests can
be satisfied or must wait to avoid future possible deadlock.
∙ A deadlock avoidance algorithm dynamically examines the resources
allocation state to ensure that a circular wait condition never exists.
The resource allocation state is defined by the number of available and
allocated resources and the maximum demand of each process.

Safe State:
∙ A state is a safe state in which there exists at least one order in which all
the process will run completely without resulting in a deadlock. ∙ A system
is in safe state if there exists a safe sequence.
∙ A sequence of processes <P1,P2,………..Pn> is a safe sequence for the
current allocation state if for each Pi the resources that Pi can request
can be satisfied by the currently available resources.
∙ If the resources that Pi requests are not currently available then Pi can
obtain all of its needed resource to complete its designated task. ∙ A safe
state is not a deadlock state.
∙ Whenever a process request a resource i.e., currently available, the
system must decide whether resources can be allocated immediately or
whether the process must wait. The request is granted only if the
allocation leaves the system in safe state.
∙ In this, if a process requests a resource i.e., currently available it must still
have to wait. Thus resource utilization may be lower than it would be
without a deadlock avoidance algorithm.

Resource Allocation Graph Algorithm:


This algorithm is used only if we have one instance of a resource type. In
addition to the request edge and the assignment edge a new edge called claim
edge is used.
Ex :- A claim edge Pi->Rj indicates that process Pi may request Rj in future.
The claim edge is represented by a dotted line.
∙ When a process Pi requests the resource Rj, the claim edge is converted
to a request edge.
∙ When resource Rj is released by process Pi, the assignment edge Rj->Pi
is replaced by the claim edge Pi->Rj.

When a process Pi requests Rj the request is granted only if converting the


request edge Pi->Rj to as assignment edge Rj->Pi do not result in a cycle. Cycle
detection algorithm is used to detect the cycle. If there are no cycles then the
allocation of the resource to process leave the system in safe state

Banker’s Algorithm:
∙ This algorithm is applicable to the system with multiple instances of each
resource types, but this is less efficient then the resource allocation
graph algorithm.
∙ When a new process enters the system it must declare the maximum
number of resources that it may need. This number may not exceed the
total number of resources in the system. The system must determine that
whether the allocation of the resources will leave the system in a safe
state or not. If it is so resources are allocated else it should wait until the
process release enough resources.
∙ Several data structures are used to implement the banker’s algorithm. Let
‘n’ be the number of processes in the system and ‘m’ be the number of
resources types.

We need the following data structures:


∙ Available:-A vector of length m indicates the number of available
resources.
If Available[i]=k, then k instances of resource type Rj is available.

∙ Max:-An n*m matrix defines the maximum demand of each process if


Max[i,j]=k, then Pi may request at most k instances of resource type Rj.

∙ Allocation:-An n*m matrix defines the number of resources of each type


currently allocated to each process.
If Allocation[i,j]=k, then Pi is currently k instances of resource type Rj.

∙ Need:-An n*m matrix indicates the remaining resources need of each


process.
If Need[i,j]=k, then Pi may need k more instances of resource type Rj to
compute its task.
So Need[i,j]=Max[i,j]-Allocation[i]

Safety Algorithm:-
This algorithm is used to find out whether or not a system is in safe state or
not.
Step 1: Let Work and Finish be vectors of length m and n, respectively
Initialize:
Work = Available
For i = 1,2, …, n,
if Allocationi¹ 0, then
Finish[i] = false;otherwise, Finish[i] = true.
Step 2: Find an index i such that both:
Finish[i] == false
Requesti <= Work
If no such i exists, go to step 4.

Step 3: Work = Work + Allocation


Finish [i] = true
Go to step 2

Step 4: If Finish [i] = false, for some i, 1<=i<= n, then the system is in a
deadlock state.
Moreover, if Finish[i] = false, then process Pi is

deadlocked. Resource Allocation Algorithm:

You might also like