20
votes

In an operating system, what is the difference between a system call and an interrupt? Are all system calls interrupts? Are all interrupts system calls?

2
Minimal interrupt example and how some Linux use it for system calls: stackoverflow.com/questions/1817577/…Ciro Santilli 新疆再教育营六四事件法轮功郝海东

2 Answers

29
votes

Short Answer: They are different things.

  • A system call is call by software running on the OS to services provided by the OS.
  • An interrupt is usually external hardware component notifying the CPU/Microprocessor about an event that needs handling in software (usually a driver).

I say usually external, because some interrupts can be raised by software (soft interrupt)

Are all system calls interrupts? Depends

Are all interrupts system calls? No

Long answer: The OS manages CPU time and other hardware connected to the CPU (Memory (RAM), HDD, keyboard, to name a few). It exposes services that allow user programs to access the underlying hardware and these are system calls. Usually these deal with allocating memory, reading/writing files, printing a document and so on.

When the OS interacts with other hardware it usually does so through a driver layer which sets-up the task for the hardware to perform and interrupt once the job is done, so the printer may interrupt once the document is printed or it runs out of pages. It is therefore often the case that a system call leads to generation of interrupts.

Are all system calls interrupts - Depends as they may be implemented as soft interrupts. So when a user program makes a system call, it causes a soft interrupt that results in the OS suspending the calling process, and handle the request itself, then resume the process. But, and I quote from Wikipedia,

"For many RISC processors this (interrupt) is the only technique provided, but CISC architectures such as x86 support additional techniques. One example is SYSCALL/SYSRET, SYSENTER/SYSEXIT (the two mechanisms were independently created by AMD and Intel, respectively, but in essence do the same thing). These are "fast" control transfer instructions that are designed to quickly transfer control to the OS for a system call without the overhead of an interrupt"

3
votes

The answer to your question depends upon the underlying hardware (and sometimes operating system implementation). I will return to that in a bit.

The purpose of an interrupt handler and a system call (and an fault handler) is largely the same: to switch the processor into kernel mode while providing protection from inadvertent or malicious access to kernel structures.

An interrupt is triggered by an asynchronous external event. A system call (or fault or trap) is triggered synchronously by executing code.

This is then the answer to your first question.

The answer to your second question is that system calls are not interrupts because they are not triggered asynchronously by the hardware. A process continues to execute its code stream in a system call, but not in an interrupt.

That being said, Intel's documentation often conflates interrupt, system calls, traps, and faults, as "interrupt."

Some processors treat system calls traps, fault and interrupts largely the same way. Others (notably Intel) provide different methods for implementing system calls.

In processors that handle all of the above in the same way, each type of interrupt, trap, and fault has a unique number. The processor expects the operating system to set up a vector (array) of pointers to handlers. In addition, there are one or more handlers available for an operating system to implement system calls

Depending upon the number of available handlers, the OS may have a separate handler for each system call or use a register value to determine what specific system function to execute.

In such a system, one can execute an interrupt handler synchronously the same way one invokes a system call.

For example, on the VAX the

CHMK #4

instruction, invokes the 4th kernel mode handler. In intel land their is an

INT

instruction that does roughly the same.

Intel processors have supported the SYSCALL mechanism that provides a different way to implement system calls.