Dr. Mark Humphrys

School of Computing. Dublin City University.

Home      Blog      Teaching      Research      Contact

My big idea: Ancient Brain


CA114      CA170

CA668      CA669      Projects

OS structure

"Process" = A program running. Could be multiple copies of the same program running. (Multiple processes.)

Device speed

See History of OS.
What do we mean "I/O devices are slower than CPU" ?

Device controllers and Interrupts

CPU is kept free (responsive to user, able to run other code) while device I/O is going on.

Modern OS driven by interrupts.

Interrupts are to do with the massive asymmetry between CPU speeds and device speeds. They are how the OS program runs on hardware with slow devices.

Interrupts (IRQs) on Windows.
My Computer - Properties - Device Manager - View - Resources by Type - Interrupt Request (IRQ)

Examples of Interrupts

Does an infinite loop cause an interrupt?

Does an infinite loop quickly (or eventually) cause an interrupt?

The Halting Problem (Turing, 1936).

Might just be a long loop. We have no way of knowing.
But still, even if long loop, control must switch occasionally - time-slicing.
It is a timer interrupt that switches control. But loop runs forever, time-sliced.

Unsolved problems in mathematics.
Note that if you could detect an infinite loop in general, then could solve all problems of the form:

Does there exist a solution to f(n)
for n > t?
by asking the OS if the following:
 n := n+1
 test solution
until solution
is an infinite loop, or just a long loop? Then our OS could solve many of the world's great mathematical problems.
Many mathematical problems can be phrased as infinite-loop problems.

Note that many "infinite loops" actually terminate with a crash, because they are using up some resource each time round the loop. e.g. This program:

f ( int x )

will eventually crash with a stack overflow.
This program however:
while true { }
will run forever, time-sliced.

Interrupts - Keeping the OS in charge

Interrupt idea is a way of periodically keeping the OS in charge so nothing runs forever without reference to the OS. e.g. In time-slicing, periodic timer interrupt to give OS a chance to do something else with the CPU.

Remember, the CPU is just looking for a stream of instructions to process, whether they come from the "OS" or from "user programs". When the OS "runs" a program, it points the CPU towards a stream of instructions in the user program and basically hands over control. How do we know program cannot now control CPU for ever?

Note: Interrupt does not necessarily mean that OS immediately attends to the process that sent it (in the sense of giving it CPU time). It just means the OS is now aware that that process wants attention. The OS will note that its "state" has changed.

CPU modes

CPU modes

There must be a concept of an "OS-type program" (which can do anything, including scheduling ordinary programs) and an "ordinary program" (which is restricted in what it can do on the machine).

The restrictions on the "ordinary program" are normally not a problem - they are basically just: "Other programs have to be able to run at the same time as you".

This obviously makes sense in a multi-user system, but in fact it makes sense on a PC as well. When you're writing a program and you make an error, you don't want your crashed program to be able to crash the whole OS. You still want to be able to turn to the OS (which should still be responsive) and say "terminate that program".

Code is executed either in kernel/system/monitor mode or in user mode.

boot in system mode, load OS
when run program, switch to user mode

when interrupt, switch to system mode 
 and jump to OS code

when resume, switch back to user mode
 and return to next instruction in user code
Privileged instructions can only be executed in system mode.
e.g. Perhaps any I/O at all - user can't do I/O, user has to ask OS to do I/O for it (OS will co-ordinate it with the I/O of other processes).

The kernel is the part of OS that is not scheduled

Consider the different types of code, OS or applications:

  1. Parts of OS that are not scheduled themselves. Are always in memory. Operate in kernel/system mode.
    This part of the OS is called the kernel.
    • memory management
    • process scheduling
    • device I/O

  2. Parts of OS that can be scheduled. Come in and out of memory. Operate in user mode.
    • command-line
    • GUI
    • OS utilities

    Parts of OS that could be in either of the above:

    • file system
    • network interface

  3. Applications. All scheduled. Come in and out of memory. Operate in user mode.

Windows has both kernel-mode drivers and user-mode drivers.
From Microsoft driver developer manual.

Virtual Machine (VM)

Virtual machine

Running Linux inside Windows with VMware.
I use this kind of thing in DCU labs to teach system administration of an imaginary multi-machine network all running on one machine.
Image from here.

Java Virtual Machine

Java is a language designed for a machine that does not exist, but that can be emulated on top of almost any machine. Java runs on a virtual piece of "hardware", the Java virtual machine.


  1. Portability. Write an application once, run everywhere.
  2. Run code from Internet on client-side. Even though from a site with different hardware, they will run on your hardware.

Java is a HLL. It is "compiled" to a "virtual" instruction set called Java bytecodes. These run on Java VM, where they are actually mapped to native hardware instructions.

What language is OS written in?

OS'es are written mainly in HLL, with some Assembly in Kernel.
Assembly fast but hardware-specific.
HLL much more portable, much easier to write/debug/change/maintain.

Languages used in Android.
From here.

Is Assembly needed?

Assembly is faster. But not needed everywhere.

Another reason to stick with HLL:
Modern compilers have smart optimisers:
Compiler often has a -optimise switch to analyse the HLL code to generate faster Assembly code.

Part of the Linux OS source. Written in C.
This is part of init/main.c in Linus Torvalds' Linux source.

ancientbrain.com      w2mind.org      humphrysfamilytree.com

On the Internet since 1987.

Wikipedia: Sometimes I link to Wikipedia. I have written something In defence of Wikipedia. It is often a useful starting point but you cannot trust it. Linking to it is like linking to a Google search. A starting point, not a destination. I automatically highlight in red all links to Wikipedia and Google search and other possibly-unreliable user-generated content.