System call
From Free net encyclopedia
In computing, a system call is the mechanism used by an application program to request service from the operating system, or more specifically, the operating system kernel.
Contents |
Background
Modern processors can typically execute instructions in several differently privileged states. In systems with two levels, they are usually called user mode and supervisor mode. Different privilege levels are provided so that operating systems can restrict the operations that programs running under them can perform, for reasons of security and stability. Such operations include accessing hardware devices, enabling and disabling interrupts, changing privileged processor state, and accessing memory management units. The operating system kernel would run in supervisor mode, and user applications in user mode.
With the development of separate operating modes with varying levels of privilege, a mechanism was needed for transferring control safely from lesser privileged modes to higher privileged modes. Less privileged code could not simply transfer control to more privileged code at any arbitrary point and with any arbitrary processor state; to allow it to do so could allow it to break security. For instance, the less privileged code could cause the higher privileged code to execute in the wrong order, or provide it with a bad stack.
Mechanism
System calls often use a special CPU instruction which causes the processor to transfer control to more privileged code, as previously specified by the more privileged code. This allows the more privileged code to specify where it will be entered as well as important processor state at the time of entry.
When the system call is invoked, the program which invoked it is interrupted, and information needed to continue its execution later is saved. The processor then begins executing the higher privileged code, which, by examining processor state set by the less privileged code and/or its stack, determines what is being requested. When it is finished, it returns to the program, restoring the saved state, and the program continues executing.
Note that in many cases, the actual return to the program may not be immediate. If the system call performs any kind of lengthy I/O operation, for instance disk or network access, the program may be suspended (“blocked”) and taken off the “ready” queue until the operation is complete, at which point the operating system will again make it a candidate for execution.
The library as an intermediary
Generally, operating systems provide a library that sits between normal programs and the rest of the operating system, usually the C library (libc), such as glibc and MS LibC. This library handles the low-level details of passing information to the kernel and switching to supervisor mode, as well as any data processing and preparation which does not need to be done in privileged mode. Ideally, this reduces the coupling between the operating system and the application, and increases portability.
On exokernel based systems, the library is especially important as an intermediary. On exokernels LibOSes shield user applications from the very low level kernel API, and provide abstractions and resource management.
Examples
On POSIX and similar systems, popular system calls are close, execve, fork, kill, open, read, wait, and write. Many of today's operating systems have hundreds of system calls. For example, Linux almost has 300 different system calls. FreeBSD has about the same (almost 330).
External links
- Linux system calls - system calls for Linux kernel 2.2, with IA32 calling conventions
- How System Calls Work on Linux/i86
- This article was originally based on material from the Free On-line Dictionary of Computing, which is licensed under the GFDL.
de:Systemaufruf he:קריאת מערכת pl:Wywołanie systemowe it:System call