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

OS - Lect 6

The document discusses interrupts and system calls. Interrupts are signals sent to the operating system that cause it to stop its current task and service the interrupt. System calls provide an interface between programs and the operating system and allow programs to request services from the operating system. Common system call types include process control, file management, device management, and information maintenance.

Uploaded by

aehab1912
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 views58 pages

OS - Lect 6

The document discusses interrupts and system calls. Interrupts are signals sent to the operating system that cause it to stop its current task and service the interrupt. System calls provide an interface between programs and the operating system and allow programs to request services from the operating system. Common system call types include process control, file management, device management, and information maintenance.

Uploaded by

aehab1912
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/ 58

Operating Systems

Lecture 6
Interrupts
What is an Interrupt?

An interrupt is a signal which is sent from a device or from


software to the operating system.

The interrupt signal causes the operating system to temporarily


stop what it is doing and ‘service’ the interrupt.
What is an Interrupt?
• Definition: It is referred to as an input signal that has the
highest priority for hardware or software events that requires
immediate processing of an event.
• A new mechanism was introduced to overcome this
complicated process. In this mechanism, hardware or
software will send the signal to a processor, rather than a
processor checking for any signal from hardware or software
(pooling).
• The signal alerts the processor with the highest priority and
suspends the current activities by saving its present state and
function, and processes the interrupt immediately, this is
known as ISR. As it doesn’t last long, the processor restarts
normal activities as soon as it is processed.
Difference between Interrupt and Polling

In the case of an interrupt, the system informs the CPU that it needs attention
While in polling, the CPU constantly inspects the status of the system to find
whether it needs attention.

In Polling
you pick up the phone every few seconds to check whether you are getting a
phone

In interrupt:
do whatever you should do and pick up the phone every when it is rings.
Difference between Interrupt and Polling
In Polling Polling is an affair that informs the CPU that a system
int main()
{
needs its attention. It is a constant action to find out whether
while(1) the system is working efficiently and properly.
{
int count = 1;
while(count < 500000)
count++;
toggle(PIN_5);
}
return 0;
}

interrupt is an affair that indicates the CPU to take prompt


In interrupt: action. In other words, we can say that the device notifies the
int main()
{ CPU that it needs its attention. When an interrupt happens, the
WFI(); CPU usually holds its current task and starts executing the
return 0;
}
corresponding interrupt handler. After completing this task it
resumes the old task which was paused.
Difference between Interrupt and Polling

S.No
Interrupt Polling
1. When it comes to an interrupt, the device When it comes to polling, the CPU keeps on checking if the
informs the CPU that it needs its attention. device needs attention.

2. It is a hardware mechanism, not a protocol. It is a protocol and not a hardware mechanism.

3. It can occur at any time and moment. Here the CPU polls the system at some particular intervals.

4. On the opposite hand, in polling, processor waste countless


In interrupts, processor is simply disturbed
processor cycles by repeatedly checking the command-ready
once any device interrupts it.
little bit of each device.
Types of Interrupt (Hardware / Software Interrupts)

1- Hardware Interrupts
An electronic signal sent from an external device or hardware to
communicate with the processor indicating that it requires
immediate attention. For example, strokes from a keyboard or
an action from a mouse invoke hardware interrupts causing the
CPU to read and process it. So it arrives asynchronously and
during any point of time while executing an instruction.
• For example (Key press by the user, e.g. CTRL ALT DEL)

Hardware interrupts are classified into two types


• Maskable Interrupts
• Non-maskable Interrupts (NMI)
Hardware Interrupts (Maskable and Non-Maskable)

1. Maskable Interrupt :
An Interrupt that can be disabled or ignored by the instructions of CPU are
called as Maskable Interrupt.

1. Non-Maskable Interrupt :
An interrupt that cannot be disabled or ignored by the instructions of CPU
are called as Non-Maskable Interrupt. A Non-maskable interrupt is often
used when response time is critical or when an interrupt should never be
disable during normal system operation. Simply is the highest priority
activities that need to be processed immediately and under any situation.
Hardware Interrupts (Maskable and Non-Maskable)

SR.N
O. Maskable Interrupt Non Maskable Interrupt
A non-maskable interrupt is a hardware interrupt
Maskable interrupt is a hardware Interrupt that can
1 that cannot be disabled or ignored by the
be disabled or ignored by the instructions of CPU.
instructions of CPU.
When non-maskable interrupts occur, the current
When maskable interrupt occur, it can be handled
2 instructions and status are stored to handle the
after executing the current instruction.
interrupt.
Maskable interrupts used to interface with Non maskable interrupt used for emergency
3
peripheral device. purpose e.g power failure, smoke detector etc .

4 In maskable interrupts, response time is high. In non maskable interrupts, response time is low.

5 Operation can be masked or made pending. Operation Cannot be masked or made pending.
Types of Interrupt (Hardware / Software Interrupts)

2- Software Interrupts
• The processor itself requests a software interrupt after executing certain
instructions or if particular conditions are met.

• These can be a specific instruction that triggers an interrupt such as


subroutine calls and can be triggered unexpectedly because of program
execution errors, known as exceptions or traps.
A trap is a software generated interrupt.
An operating system is interrupt driven.

Examples
• division by zero or invalid memory access
• A cout (or) cin statement in C++ would generate a software interrupt
because it would make a system call to print something
Interrupt Handling

1. CPU is interrupted
2. CPU stops current process
3. CPU transfers execution to a fixed location
4. CPU executes interrupt service routine
5. CPU resumes process

• Notes:
• Interrupts must be handled quickly
• Interrupted process information must be stored
Interrupt Handling Mechanisms

The interrupt handler is the part of the operating system


which is responsible for dealing with interrupt signals.

The interrupt handler prioritizes interruptions as they are


received, placing them into a queue as necessary.

For every interruption, the current task needs to be


stopped, with it’s status saved (so it can resume later).
System Calls
System Calls

• System calls provide the interface between a


running program and the operating system.

• Generally available as assembly-language


instructions.
• Languages defined to replace assembly
language for systems programming allow system
calls to be made directly (e.g., C, C++)
• Mostly accessed by programs via a high-level
Application Programming Interface (API) rather
than direct system call
Example of Standard API
API – System Call – OS Relationship
API – System Call – (Example)
• C program invoking printf() library call, which
calls write() system call

printf() is one of the APIs.

printf() is a function that convert your


data into a formatted sequence of bytes
and that calls system call ”write()” to
write those bytes onto the output.

C++ use “cout”


And Java use “System.out.println”
Copy Program Example

• Variable initialization
• open ( file1 )
• create ( file2 )
• read ( file1 )
• write ( file2 )
• close ( file1 )
• close ( file2 )
Example of System Calls
• System call sequence to copy the contents of one file to
another file
Types of System Calls

• Process control
• File management
• Device management
• Information maintenance
• Communications
Types of System Calls
• Process control
• create process, terminate process
• end, abort
• load, execute
• get process attributes, set process attributes
• wait for time
• wait event, signal event
• allocate and free memory
• Dump memory if error
• Debugger for determining bugs, single step execution
• Locks for managing access to shared data between
processes
Types of System Calls
• File management
• create file, delete file
• open, close file
• read, write, reposition
• get and set file attributes

• Device management
• request device, release device
• read, write, reposition
• get device attributes, set device attributes
• logically attach or detach devices
Types of System Calls (Cont.)
• Information maintenance
• get time or date, set time or date
• get system data, set system data
• get and set process, file, or device attributes

• Communications
• create, delete communication connection
• send, receive messages if message passing model to host
name or process name
• From client to server
• Shared-memory model create and gain access to memory
regions
• transfer status information
• attach and detach remote devices
System Calls Types

Process File Device Information Communications


Control Management Management Maintenance IPC

load, execute create file request get time create channel


end, abort delete file release set time delete channel

create open attach get system data send message


terminate close detach set system data receive message

get attributes read / write read get attributes


transfer status
set attributes reposition write set attributes

wait time, get attributes get attributes attach / detach


event, signal set attributes set attributes remote device

allocate memory
free memory
Examples of Windows and Unix System Calls

Ref: https://www.guru99.com/system-call-operating-system.html
Ref: https://www.javatpoint.com/system-calls-in-operating-system
Ref: https://www.geeksforgeeks.org/introduction-of-system-call/
Deadlocks
Table of Content

• Introduction
• Deadlock Characterization
• Methods for Handling Deadlocks
• Deadlock Prevention
• Recovery from Deadlock
introduction
The Deadlock Problem

• A set of blocked processes each holding a resource and


waiting to acquire a resource held by another process in
the set.
• Example
• System has 2 tape drives.
• P1 and P2 each hold one tape drive and each needs another one.
• semaphores A and B, initialized to 1

P1 P2
wait (A); wait(B)
wait (B); wait(A)
Is this Deadlock?
Yes, How to prevent it?
Bridge Crossing Example

• Traffic only in one direction.


• Each section of a bridge can be viewed as a resource.
• If a deadlock occurs, it can be resolved if one car backs
up (preempt resources and rollback).
• Several cars may have to be backed up if a deadlock
occurs.
• Starvation is possible
Deadlock Characterization
Deadlock Characterization

• Deadlock can arise if four conditions hold simultaneously.


1. Mutual exclusion: only one process at a time can use a
resource.
2. Hold and wait: a process holding at least one resource is
waiting to acquire additional resources held by other
processes.
3. No preemption: a resource can be released only voluntarily by
the process holding it, after that process has completed its
task.
4. Circular wait: there exists a set {P0, P1, …, P0} of waiting
processes such that P0 is waiting for a resource that is held by
P1, P1 is waiting for a resource that is held by P2, …, Pn–1 is
waiting for a resource that is held by Pn, and Pn is waiting for a
resource that is held by P0
Resource-Allocation Graph

• Process

• Resource Type with 2 instances

Pi Rj
• Pi requests instance of Rj

Pi Rj
• Pi is holding an instance of Rj
Example of a Resource Allocation Graph
Resource Allocation Graph With A Deadlock
Resource Allocation Graph With A Cycle But No Deadlock
Basic Facts

• If graph contains no cycles no deadlock.


• If graph contains a cycle
• if only one instance per resource type, then deadlock.
• if several instances per resource type, possibility of deadlock.
Methods for Handling Deadlocks
Methods for Handling Deadlocks

• Ensure that the system will never enter a deadlock state.


• Allow the system to enter a deadlock state and then
recover.
• Ignore the problem and pretend that deadlocks never
occur in the system; used by most operating systems.
Deadlock Characterization
There are four methods of handling deadlocks

• deadlock avoidance

• deadlock prevention

• deadlock detection and recovery

• deadlock ignorance
Deadlock avoidance
deadlock avoidance

• In deadlock avoidance, the operating system checks


whether the system is in safe state or in unsafe state at
every step.
• The process continues until the system is in safe state.
Once the system moves to unsafe state, the request is got
granted.
• Therefore, it requires additional information such as how
many resources of each type is required by a process. If
the system enters into an unsafe state, it has to take a step
back to avoid deadlock.
Deadlock Prevention
Deadlock Prevention
• The strategy of deadlock prevention is to design the system in
such a way that the possibility of deadlock is excluded.
• The idea behind the approach is very simple that we have to fail
one of the four conditions (mutual exclusion, no pre-emption and
hold and wait and the circular wait) but there can be a big
argument on its physical implementation in the system.

The disadvantages of this method are:


• long waiting time required
• in efficient use of allocated resource
• A process may not know all the required resources in advance.
Deadlock Prevention Cont’d

• Mutual Exclusion
• Not required for sharable resources; must hold for non-sharable
resources.

• Hold and Wait


• must guarantee that whenever a process requests a resource, it
does not hold any other resources.
• Require process to request and be allocated all its resources before it begins
execution, or allow process to request resources only when the process has
none.
• Low resource utilization; starvation possible.
Deadlock Prevention Cont’d
• No Preemption
• If a process that is holding some resources requests another
resource that cannot be immediately allocated to it, then all
resources currently being held are released.
• Preempted resources are added to the list of resources for which
the process is waiting.
• Process will be restarted only when it can regain its old resources, as
well as the new ones that it is requesting.

• Circular Wait
• Impose a total ordering of all resource types, and require that each
process requests resources in an increasing order of enumeration.
Detection and Recovery from
Deadlock
Detection and Recovery from Deadlock
• This approach let the processes fall in deadlock and then
periodically check whether deadlock occur in the system or not.

• If it occurs then it applies some of the recovery methods to the


system to recover from deadlock.

• Deadlock detection is used by employing an algorithm that


tracks the circular waiting and killing one or more processes so
that deadlock is removed.

• The system state is examined periodically to determine if a set of


processes is deadlocked or not.
Detection and Recovery from Deadlock cont.

• Abort all deadlocked processes.

• Abort one process at a time until the deadlock cycle is


eliminated.

• In which order should we choose to abort?


• Priority of the process.
• How long process has computed, and how much longer to completion.
• Resources the process has used.
• Resources process needs to complete.
• How many processes will need to be terminated.
• Is process interactive or batch?
Deadlock Ignorance

In the method, the system assumes that deadlock never


occurs. Since the problem of deadlock situation is not
frequent, some systems simply ignore it.

Operating systems such as UNIX and Windows follow this


approach. However, if a deadlock occurs we can reboot our
system and the deadlock is resolved automatically.
Direct memory
access (DMA)
• Direct memory access (DMA) is a method that allows an
input/output (I/O) device to send or receive data directly to
or from the main memory, bypassing the CPU to speed up
memory operations.

• The process is managed by a chip known as a DMA controller


(DMAC).

• DMA Controller is a hardware device that allows I/O devices


to directly access memory with less participation of the
processor.
Direct Memory Access Structure

• A DMA channel enables a device to transfer data without


exposing the CPU to a work overload. Without the DMA channels,
the CPU copies every piece of data using a peripheral bus from
the I/O device.
• Using a peripheral bus occupies the CPU during the read/write
process and does not allow other work to be performed until the
operation is completed.

• With DMA, the CPU can process other tasks while data transfer is
being performed. The transfer of data is first initiated by the CPU.
The data block can be transferred to and from memory by the
DMAC in three ways.
Direct Memory Access Structure

• Used for high-speed I/O devices able to transmit


information at close to memory speeds.
• Device controller transfers blocks of data from buffer
storage directly to main memory without CPU intervention.
• Only one interrupt is generated per block, rather than the
one interrupt per byte.
Thank You

You might also like