Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
1  
Very good question! –  Stéphane Gimenez Aug 2 '11 at 18:27

3 Answers 3

up vote 7 down vote accepted
  • 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.

  • 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.

  • 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.

share|improve this answer
    
Thanks! (1) In 1, do you mean the two ways of getting attention from CPUs are software interrupt (the program with system call example) and hardware interrupt (the reset key example)? (2) In 2, do you mean that system call and message passing are two ways of software interrupt? (3) Is it correct that the purpose of software interrupt is for process to request kernel services and resources, and the purpose of hardware interrupt is not? If yes, what is the purpose of hardware interrupt? –  Tim Aug 2 '11 at 22:35
    
(1) Correct. (2) Well, system calls and message passing are two different techniques that can be used for achieving Interprocess Communication. But it's true that both are software-based, thus to get attention from the CPU you execute a trap (software interrupt). (3) In a sense, yes. An example of asking for a kernel service is executing a library procedure (which will be translated into 1 or more system calls) like creat for creating a new file. An example of an interrupt is the printer sending a signal to the CPU to inform it that printing is completed. –  faif Aug 2 '11 at 23:13
    
Thanks! Regarding (3), do you mean in a sense no? What are the general purposes of hardware interrupt and of software interrupt? –  Tim Aug 2 '11 at 23:24
    
A system reset is not an interrupt; it doesn't save state to resume after. –  psusi Aug 3 '11 at 2:50
    
@psusi I think that reset is an interrupt - non-maskable, but you're right that it doesn't need to save the state. –  faif Aug 3 '11 at 9:22

System calls, messaging passing (as described in the Wikipedia article), and interrupts are all things that cause a context switch or a switch from user to kernel mode. As you likely know:

  • kernel mode: programs have a flat or real view of memory, and programs can read/write freely to all memory and all hardware devices directly without restriction.

  • user mode: programs have a virtualized view of memory, programs cannot read/write freely to all memory and cannot read/write hardware devices directly. To obtain more memory or access hardware devices, the user mode program must call the kernel. System calls and message passing are two methods to do this.

System calls involve executing a specific CPU instruction or set of instructions, which make the CPU jump (first saving return address on the stack) to a predefined address (not writeable to user mode) and move the CPU from user mode to kernel mode (ring 3 to ring 0 in Intel architecture).

Hardware interrupts do much the same thing, they make the CPU jump (first saving return address on the stack) to a predefined address and move the CPU from user mode to kernel mode. So in many CPUs the same mechanism can be invoked by software (called a "software interrupt") and can be used for CPU calls.

Message passing implies (to me at least) that the kernel is a "running process" that will receive a stream of messages and that there exists a user-mode accessible function that will send such messages. Or it could be that the "send" function just shoves values on a stack and the next time the kernel has control (either if a process blocks or an interrupt occurrs) it pops messages off the stack and dispatches to internal routines accordingly.

In a microkernel architecture where the actual "kernel" is very minimal and most of the functions that a kernel provides are moved to "server" processes, all of which may be running concurrently on a multi-CPU system, something like this might be more useful than the plain old system call approach. Interpreting and routing "messages" to the appropriate kernel "server" would be one of the microkernel's few jobs.

share|improve this answer
1  
Kernel mode uses the same page tables and presents the same virtual memory view as user mode. The difference is that it has access to the pages marked as being restricted to kernel mode. –  psusi Aug 3 '11 at 2:55

Message passing is a higher level concept of one process sending a message to another. It is implemented by a system ( kernel ) call, asking the kernel to pass the message to the other process. System calls ask the kernel to perform various services for the process. They are implemented by a software interrupt / system trap, which causes the cpu to save some state on the stack so it can return later, then switch to kernel mode and jump to the kernel handler.

Both hardware and software interrupts cause the cpu to save state, switch to kernel mode, and jump to a defined handler for that interrupt. The difference is that hardware interrupts are generated by external hardware when they need some attention, such as a keyboard indicating that a key has been pressed. The keyboard handler then can go read the keyboard IO port to see what key was pressed, and take appropriate action, and then return to the program that was interrupted.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.