
Introduction
A system call serves as a means for a user program to interact with an operating system. The OS responds to a program's request by invoking several system calls. Assembly language or high-level languages such as C or Pascal can implement a system call. When a high-level language is used, the operating system may directly invoke predefined functions.
Also see: Multiprogramming vs Multitasking, Open Source Operating System.
What is a system call?
When a user uses an API(Application programming interface) or writes a program, the code is typically written in user mode; however, if the user wishes to access the operating system's functionality, it is written in kernel mode. Therefore, we use a system call method to switch between kernel mode and user mode.
In user mode, accessing the resources is restricted. Consequently, the CPU can access only a limited set of instructions and memory. This mode is used by utility applications like word processors, music players, etc.
The kernel mode has complete access to memory, I/O, and other resources. Here, the CPU is free to execute any instruction and access all the hardware. Kernel-mode always executes the core functionality of the operating system.
Operating systems have too many system calls, but some do not support direct system calls because they require APIs or local libraries like printf(), scanf(), etc.
How do system calls work?
Whenever a user program calls a system call, the processor executes a system call instruction, causing the kernel protection domain to execute the system call handler.
Steps involved in making a system call are:
- User applications first set up the arguments to make a system call.
- The program then executes the "system call" instruction once all arguments have been set up.
- A processor exception is generated when this instruction causes it to jump to a new address and start executing the code.
- By saving your program's state at the new address, finding the system call you want, calling the kernel function that translates that call, and restoring your program's state, the instructions at the new address allow you to move control back to the user program.
The operating system provides system calls via the system call interface. As a result, it invokes the intended kernel call and returns the result of the call and any return values. System calls do not need the caller to understand how they are implemented or how they behave during execution.
Why are system calls necessary?
As a result of implementing user mode and kernel mode in operating systems, Individual processes have their data, with access permissions specific to them. Each of these processes requires a fair allocation of resources to function correctly.
There are several reasons why kernel mode is the more important control system here. All system-internal services and processes run there, and all user-mode applications block system-critical actions. In most cases, system calls are utilized to access (CPU) or memory structures (working memory and hard drive space).
For example, system calls are necessary for applications that require extra computing power or storage space or for applications that need external files (open, read, edit, etc.).