How the interrupt mechanism works

Thus far it is possible that we’ve made the assumption that a process usually runs to completion and then the OS runs other processes. As indicated in our process management note/discussion we now know this to be false.

It is possible for an OS to be implemented as a timing mechanism, switching between each process after say 100 instructions being executed. In theory, this is a good first approach if all instructions are executed in  a very short and equal timeframe.

Below, we  represent how this can be accomplished by observing what the cpu processes assuming each instruction takes about 1 second for 2 processes, P1 and P2:

Order of executionProcess/operation categoryInstructions executedTime taken (s)
1I/OI/O for OS and P12
2P1100 lines from P1100
3I/OI/O for OS and P22
4P2100 lines from P2100
. . . . .. . . . .. . . . .. . . . .
501I/OI/O for OS and P12
502P1100 lines from P1100
503I/OI/O for OS and P22
504P2100 lines from P2100
. . . . .. . . . .. . . . .. . . . .

In practice though, a single instruction from a running process can  be waiting or very long, such as when the instruction requires data be read from a secondary storage medium.  Data access on secondary storage is very slow.  Let us assume that P1 has  a few instructions that require some data access. Our table now becomes:

Order of executionProcess/operation categoryInstructions executedTime taken (s)
1I/OI/O for OS and P12
2P1100 lines from P11500
3I/OI/O for OS and P22
4P2100 lines from P2100
. . . . .. . . . .. . . . .. . . . .
501I/OI/O for OS and P12
502P1100 lines from P11200
503I/OI/O for OS and P22
504P2100 lines from P2100
. . . . .. . . . .. . . . .. . . . .

We observe in this analogy that p1 runs for a total of 1502 seconds P2 for 102, P1 for 1202 seconds, P2 for 102 seconds.

To the end user, It appears as if BOTH processes are running slowly, in the long run, p2 can appear to be running in slow motion!

To solve this problem, we could use a system of interrupts, i.e, interrupt the CPU whenever we anticipate a waiting period (for whatever reason I/O, system errors, device errors etc.)

In our analogy, every time P1 needs to wait on data from secondary access, we could put the rest of P1 in a waiting/blocked state and start processing P2.  When the hardware is finished gathering the data for P1, it could interrupt the execution of P2 and return control to P1.

Omitting IO from the OS, An illustration of this example is shown below:

(Assume that instructions for p1 are executed in 1 second intervals until an instruction needing secondary data access is reached)

P1’s instruction include:

49 short instructions then,

1 long access instruction then,

24 short instructions then,

1 long access instruction, then

25 short instructions

Order of executionProcess/operation categoryInstructions executedTime taken (s)
1P150 lines from P150
2OS(P1 Blocked),
3P2100 lines from P2100
4P2100 lines from P2100
5P2100 lines from P2100
6P250 lines from P2, interruption occurs50
7P125 lines from P125
8OS(P1 Blocked),
9P250 lines from P250
10P2100 lines from P2100
11P2100 lines from P2100
12OSInterruption occurs to return accessed data to P1
13P125 lines from P125
14P2100 lines from P2100
. . . . .. . . . .. . . . .. . . . .

We now observe both processes running efficiently with no excessive wait times.

Exercise
  1. What is the total wait time for P1 and P2 Respectively?
  2. Which process had the more instructions executed?

© 2022  Vedesh Kungebeharry. All rights reserved. 


  •  


Process Management

Multiple programs are usually executed concurrently  as processes (running programs).  A bit of code from each process is executed at a time  on the processor so that all processes are continually executed until system shutdown or they have completed their task and exit.

The operating system  (OS) is programmed to start the execution of programs and manage their execution by putting them into different states.  Note that the programs can be application programs intended for the end user or system programs used for self management (e.g memory management)

Discussion:

  1. Virtual memory / page files used to simulate ram capacities larger than physical ram on the main system. (Falls under memory management).
  2. Blackboard  as Ram chiselled tablet as Secondary storage – How processes react when they must wait


Process States.

Running – The process is currently being executed

Ready  – A previously interrupted process that can be expected to resume.

Waiting/blocked  – a process that has been put to wait by the cpu or is waiting on a slower operation (I/O or request for data)

Process state

Attributions to media used in post

MrDrBob, CC BY-SA 3.0, via Wikimedia Commons

© 2022  Vedesh Kungebeharry. All rights reserved. 

Bootstrap  Process

This is the process of loading and executing the basic instructions to load the operating system to bring the system to a fully functional state  to be used for its intended purpose (initiated either by the user powering on the system or an autonomous system system e.g a network device , typically a router. See wake on lan)  .  For modern day systems, these instructions are usually hardcoded in the EEPROM.

Updates to this post

2023-09-15 – Added wake on LAN, and detailed to the initiation of the device.

© 2022  Vedesh Kungebeharry. All rights reserved. 

Adding 2 numbers  – real/floating point numbers

0004

Exercise 1 : Modify your code from the previous example to use floating point numbers 5.1 and 6.1

  1. Teacher modifies variable declarations, but leaves prinf to output integer values.
  2. The erroneous result is observed and explained: the binary data stored at the variables are calculated and interpreted to show an integer result.  
  3. Teacher modifies the code of printf() to use %d and now the correct result is shown.


Exercise 2:

Observe what is output when %d is changed to each of the following, one at a time:

%d for int

%f for float

%lf for double

%c for char

%s for string

Video Demonstration

https://youtu.be/Be_QtiweYTY

You can download the files here: https://drive.google.com/file/d/1nVXCOXTJkPYNSs2jDwxGGbz9mvuKooDK/view?usp=sharing

Code

#include <stdio.h>
#include <stdlib.h>

int main()
{
   //declare and identify variables
   float a,b;
   float c;

   //Initialize the variables
   a=5.1;
   b=6.1;

   //Perform processing c <-- a+b
   c=a+b;

   printf("The sum of %.1f and %.1f is %.f \n", a,b,c);


}

© 2021  Vedesh Kungebeharry. All rights reserved. 

Adding 2 numbers – Outputting the result with printf()

0003-OutputResults

Exercise: Modify your previous code to output results using printf()

Video Demonstration

https://youtu.be/YE0V_XLuOx8

You can download the file used in the video here: https://drive.google.com/file/d/1A4oCHsdXW0Ab7ox-T_zXi3eVmrQkXoic/view?usp=sharing

Code

#include <stdio.h>
#include <stdlib.h>

int main()
{
   //declare and identify variables
   int a,b;
   int c;

   //Initialize the variables
   a=5;
   b=6;

   //Perform processing c <-- a+b
   c=a+b;

   printf("The sum of %d and %d is %d \n", a,b,c);


}

© 2021  Vedesh Kungebeharry. All rights reserved. 

Adding 2 numbers – (Declaring integer variables, Watching Variables)

0002-ADDING 2 integers , 5 and 6 no output

Exercise 1 

  1. Write code which declares 2 integer variables A and B .  Initialize A to 5 and B to 6.  Use a third variable , C to store the result of A+B.  

  2. Verify addition has occurred by using the variable watch window.

Demonstration Video

https://youtu.be/nfGwWUIZDaY

Download the files here: https://drive.google.com/file/d/15PfdndUqvnd_fLYPzctsgZMG4I9oSYtx/view?usp=sharing

Code

#include <stdio.h>
#include <stdlib.h>

int main()
{
   //declare and identify variables
   int a,b;
   int c;

   //Initialize the variables
   a=5;
   b=6;

   //Perform processing c <-- a+b
   c=a+b;


}

© 2021  Vedesh Kungebeharry. All rights reserved. 

Your First Program Using Codeblocks IDE

0001 Your first program

*Using Codeblocks 17.12

1. Create a new Project

2.Select “Console Application” and click on go.

3.Select “C” and click next.

4. Give your project a title and optionally select a folder where you want the project to be created. In this case we use “Upper camel case”` : “0001-MyFirstProgram”

5.Leave all settings as default and choose finish:

6. Open main.c from the tree on the left. [1] Expand sources, [2]then double click on “main.c”.

7. Click on Build, and choose build and run.

8. Your code is now built to an executable file, and the program is executed in the console window:

Exercises

  1. What happens when “Hello world!\n” is changed to “Hello world!” and the program is run?
  2. Modify the printed message to say “Goodbye”.

You can download the project here:

https://drive.google.com/drive/folders/1-oDom6OVimkNZeBACsJs9FZ0byhsNXHv?usp=sharing

The code is shown below:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    printf("Goodbye world!");
    return 0;
}

© 2021  Vedesh Kungebeharry. All rights reserved. 

Multiplexers

A multiplexer is a combinational circuit having many inputs which allows for the outputting a single data input at a time by using selection input lines. A multiplexer has 2n data input lines , n select input lines and a single output.

Discussion by analogy : A Television remote control used to access 4 channels

Teacher describes a remote control with 2 buttons that can be toggled on and off to switch between 4 channels

A 4 to 1 multiplexer

4-to-1 multiplexer

The truth table is shown below:

s1s2Output at F
00x­1
01x2
10x­3
11x­4

Note that the data from any input line can be either a 0 or 1 instantaneously depending what the input at a the time. For example, let us consider the situation where over the next 4 cycles of execution on a cpu that

  • dataline x­1 produces 0011 on each cycle respectively,
  • dataline x­2 produces 1100 on each cycle respectively; and
  • during cycles 1 and 2, s1 and s2 are 0 and 0 respectively
  • during cycles 3 and 4, s1 and s2 are 0 and 1 respectively

The resulting output at f over the four cycles are 0000.

That is, two bits of data from x­1, then two bits of data from x­2.

In sequence , we selected x1 for 2 consecutive cycles, then x2 for another 2 consecutive cycles.

Special Note on Spelling: MultiplexEr or MultiplexOr ?

Using the Oxford English Dictionary, the original spelling is multiplexer, as evidenced by it’s widespread use. However it’s use in quotation is as early as 1961, the spelling as multiplexor appeared in quotation in 1957.

Despite this, It’s generally accepted that the original spelling is multiplexer, and multiplexor should be considered a variation on the.

See:

MultiplexEr: https://www.oed.com/search/advanced/Quotations?textTermText0=multiplexer&textTermOpt0=QuotText&quotDateFirstUse=all&page=1&sortOption=DateOldFirst

MultiplexOr: https://www.oed.com/search/advanced/Quotations?textTermText0=multiplexor&textTermOpt0=QuotText&quotDateFirstUse=all&page=1&sortOption=DateOldFirst

Attribution to media used in this post

User:Mdd4696, Public domain, via Wikimedia Commons

Updates to this post

2023/11/25-

Added Special Note on Spelling: MultiplexEr or MultiplexOr ?

Changed Main spellings from multiplexor to multiplexer in note and URL

© 2021  Vedesh Kungebeharry. All rights reserved. 

The SR Flip-Flop – Practical Introduction

This post is used for discussion in class.

In class demonstration

Teacher draws a SR latch using NOR gates  with top gate as g1 and bottom gate as g2.

R-S

Teacher draws a truth table for quick reference:

ABA NOR B
001
010
100
110

  • Scenario, Circuit Startup S=0, R=0 , Q=0  (Q’ is irrelevant and can be proven in a later scenario)
    • Input G2 becomes 0,
    • Q’ becomes 1
    •  Input G1 becomes 1
    • Q stays at 0 , circuit is stable in a consistent state.

  • Scenario: circuit in previous state, S becomes 1, r stays at 0, Q was 0 and Q’ is 1



  • Scenario: Same as previous state,  however S returns to 0.
    (Memory is Achieved)

  • Same as previous State, but R is set to 1


  • Same as previous state but R returns to 0

  • CHAOS!!! S and R set to 1.

Media Attrbution

No machine-readable author provided. Arturo Urquizo assumed (based on copyright claims)., CC BY 3.0 <https://creativecommons.org/licenses/by/3.0>, via Wikimedia Commons

© 2021  Vedesh Kungebeharry. All rights reserved.