The relationship between system calls, message passing, and interrupts

Architectureinterruptsystem-calls

I am reading the Wikipedia article for process management. My focus is on Linux. I cannot figure out the relation and differences between system call, message passing and interrupt, in their concepts and purposes. Are they all for processes to make requests to kernel for resources and services?

Some quotes from the article and some other:

  1. There are two possible ways for an OS to regain control of the
    processor during a program’s execution in order for the OS to
    perform
    de-allocation or allocation:

    1. The process issues a system call (sometimes called a
      software
      interrupt); for example, an I/O request occurs requesting to
      access a
      file on hard disk.
    2. A hardware interrupt occurs; for example, a key was pressed
      on
      the keyboard, or a timer runs out (used in pre-emptive
      multitasking).
  2. There are two techniques by which a program executing in user mode
    can
    request the kernel's services:

    * System call
    * Message passing
    
  3. an interrupt is an asynchronous signal indicating the need for
    attention or a synchronous event in software indicating the need
    for a
    change in execution.

    A hardware interrupt causes the processor to save its state of
    execution and begin execution of an interrupt handler. Software
    interrupts are usually implemented as instructions in the
    instruction
    set, which cause a context switch to an interrupt handler similar
    to a
    hardware interrupt.

Best Answer

  1. All modern operating systems support multitasking. This means that the system is able to execute multiple processes at the same time; either in pseudo-parallel (when only one CPU is available) or nowadays with multi-core CPUs being common in parallel (one task/core).

    Let's take the simpler case of only one CPU being available. This means that if you execute at the same time two different processes (let's say a web browser and a music player) the system is not really able to execute them at the same time. What happens is that the CPU is switching from one process to the other all the time; but this is happening extremely fast, thus you never notice it.

    Now let's assume that while those two processes are executing, you press the reset button (bad boy). The CPU will immediately stop whatever is doing and reboot the system. Congratulations: you generated an interrupt.

    The case is similar when you are programming and want to ask for a service from the CPU. The difference is that in this case you execute software code -- usually library procedures that are executing system calls (for example fopen for opening a file).

    Thus, 1 describes two different ways of getting attention from the CPU.

  2. Most modern operating systems support two execution modes: user mode and kernel mode. By default an operating system runs in user mode. User mode is very limited. For example, all I/O is forbidden; thus, you are not allowed to open a file from your hard disk. Of course this never happens in real, because when you open a file the operating system switches from user to kernel mode transparently. In kernel mode you have total control of the hardware.

    If you are wondering why those two modes exist, the simplest answer is for protection. Microkernel-based operating systems (for example MINIX 3) have most of their services running in user mode, which makes them less harmful. Monolithic kernels (like Linux) have almost all their services running in kernel mode. Thus a driver that crashes in MINIX 3 is unlikely to bring down the whole system, while this is not unusual in Linux.

    System calls are the primitive used in monolithic kernels (shared data model) for switching from user to kernel mode. Message passing is the primitive used in microkernels (client/server model). To be more precise, in a message passing system programmers also use system calls to get attention from the CPU. Message passing is visible only to the operating system developers. Monolithic kernels using system calls are faster but less reliable, while microkernels using message passing are slower but have better fault isolation.

    Thus, 2 mentions two different ways of switching from user to kernel mode.

  3. To revise, the most common way of creating a software interrupt, aka trap, is by executing a system call. Interrupts on the other hand are generated purely by hardware.

    When we interrupt the CPU (either by software or by hardware) it needs to save somewhere its current state -- the process that it executes and at which point it did stop -- otherwise it will not be able to resume the process when switching back. That is called a context switch and it makes sense: Before you switch off your computer to do something else, you first need to make sure that you saved all your programs/documents, etc so that you can resume from the point where you stopped the next time you'll turn it on :)

    Thus, 3 explains what needs to be done after executing a trap or an interrupt and how similar the two cases are.

Related Question