Software interrupts

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Software interrupts

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A program tries to calculate 10 / 0. What type of interrupt is generated?
A Software interrupt
B Hardware interrupt
2. Which one is a software interrupt?
A Two processes try to access the same memory location
B A user presses a key on the keyboard
3. Who or what generates a software interrupt?
A A peripheral device such as a mouse
B A program or the processor itself
4. A program contains an invalid instruction that the CPU cannot decode. What happens?
A A software interrupt is generated so the error can be handled
B Nothing happens and the CPU skips it silently
5. A program needs the operating system to save a file for it. How does it ask?
A It sends a hardware interrupt through the file's storage device
B It generates a software interrupt to request an OS service

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1result = 10 / 0 AA key press is a hardware interrupt, because it comes from a hardware device. A software interrupt comes from a program or the processor.
2open("notes.txt") BProcess A writes to address 5000 while Process B writes to address 5000
3Two processes trying to use the same memory location at once cause a software interrupt CCPU fetches the instruction 11111111 (not in the instruction set)
4The processor raises a software interrupt when it meets an instruction it cannot run DA running program divides by zero, so a software interrupt is generated
5A software interrupt is sent by a keyboard when a key is pressed EA program asks the operating system for a service, which causes a software interrupt
6A software interrupt means the program keeps running and the error is ignored FA software interrupt makes the CPU pause the process so the error or request can be handled first.

Definitions

software interrupt

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
result ← 10 / _ // CPU sends a software interrupt
2.
IF divisor = 0 THEN // CPU sends a ________ interrupt ENDIF
3.
CALL PrintFile(report) // Program requests a service from the __
4.
// Process A writes to address 500 // Process B writes to address 500 // Same ______ location causes an interrupt
5.
instruction ← "ZZZ" // The CPU cannot run an _______ instruction

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A INPUT divisor
B total ← 100
C IF divisor > 0 THEN
D SEND interrupt TO CPU
E ENDIF
Question 2
A FUNCTION safeDivide(a, b)
B IF b = 1 THEN
C RAISE DivideByZero
D ENDIF
E RETURN a / b
F ENDFUNCTION
Question 3
A PROCESS A writes address 500
B PROCESS B writes address 900
C CPU detects memory conflict
D CPU generates software interrupt
Question 4
A instruction ← FETCH(PC)
B opcode ← DECODE(instruction)
C IF opcode NOT valid THEN
D GENERATE hardware interrupt
E ENDIF
Question 5
A program asks OS to save a file
B CPU pauses the program
C ISR deletes the program
D program resumes afterwards

Answer Key

Software interrupts
Section A: Quick Questions
Q1
A — Software interrupt
Q2
A — Two processes try to access the same memory location
Q3
B — A program or the processor itself
Q4
A — A software interrupt is generated so the error can be handled
Q5
B — It generates a software interrupt to request an OS service
Section B: Match the Pairs
Q1
result = 10 / 0 → A running program divides by zero, so a software interrupt is generated
Q2
open("notes.txt") → A program asks the operating system for a service, which causes a software interrupt
Q3
Two processes trying to use the same memory location at once cause a software interrupt → Process A writes to address 5000 while Process B writes to address 5000
Q4
The processor raises a software interrupt when it meets an instruction it cannot run → CPU fetches the instruction 11111111 (not in the instruction set)
Q5
A software interrupt is sent by a keyboard when a key is pressed → A key press is a hardware interrupt, because it comes from a hardware device. A software interrupt comes from a program or the processor.
Q6
A software interrupt means the program keeps running and the error is ignored → A software interrupt makes the CPU pause the process so the error or request can be handled first.
Section C: Definitions
Q1
software interrupt — A signal sent to the CPU by a program or the processor itself when a process hits an error (e.g. divide by zero, invalid instruction) or needs an OS service, so the CPU pauses it.
Section D: Fill in the Blanks
Q1
result ← 10 / 0 // CPU sends a software interrupt
Q2
IF divisor = 0 THEN // CPU sends a software interrupt ENDIF
Q3
CALL PrintFile(report) // Program requests a service from the OS
Q4
// Process A writes to address 500 // Process B writes to address 500 // Same memory location causes an interrupt
Q5
instruction ← "ZZZ" // The CPU cannot run an invalid instruction
Section E: Spot the Error
Q1
C — A divide by zero error happens when the divisor equals 0, so the check must be IF divisor = 0 THEN. Testing for a value greater than 0 would raise the software interrupt for valid divisors.
Q2
B — Dividing by 1 is fine. The error (and the software interrupt) happens when the divisor is 0, so the line should be IF b = 0 THEN.
Q3
B — The software interrupt is caused by two processes trying to use the same memory location. If B writes to address 900 there is no conflict. It should be address 500.
Q4
D — An invalid instruction is spotted by the processor itself while running a program, so it generates a software interrupt, not a hardware interrupt from a device.
Q5
C — When a program requests an OS service with a software interrupt, the ISR must carry out that service (saving the file). It should not delete the program.