Table of contents
1.
Introduction
2.
What is a system call?
2.1.
How do system calls work?
2.2.
Why are system calls necessary?
3.
Types of system calls
3.1.
File Related system calls
3.2.
Device Related system calls 
3.3.
Information Related system calls
3.4.
Process Control Related system calls
3.5.
Communication-Related system calls 
4.
When do we need system calls?
5.
Frequently Asked Questions
5.1.
Who handles system calls?
5.2.
How can system calls be accessed?
5.3.
Difference between interrupt and system call?
5.4.
Are system calls triggered by hardware?
6.
Conclusion
Last Updated: Mar 27, 2024

Introduction to System Calls

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?
Operating Systems

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.

User<->Kernal

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.

System Call INterface

 

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

Types of system calls

There are many different system calls, let's discuss some of them:

File Related system calls

The functions of a system call enable files to be created, read, and written.

WINDOWS LINUX

CreateFile()

WriteFile()

ReadFile()

CloseHandle()

open()

write()

read()

close()

Device Related system calls 

Device manipulations such as reading from and writing to device buffers are performed in these calls.

WINDOWS LINUX

SetConsoleMode()

ReadConsole()

WriteConsole()

ioctl()-input-output control

read()

write()

Information Related system calls

Information is transferred between the operating system and user programs using these system calls.

WINDOWS LINUX

GetCurrentProcessID()

Sleep()

SetTimer()

getpid()

sleep()

alarm()

Process Control Related system calls

Process creation, process termination, and so on are handled by these system calls.

WINDOWS LINUX

CreateProcess()

ExitProcess()

WaitForSingleObject()

fork()

wait()

exit()

Communication-Related system calls 

Interprocess communication is enabled by these calls. A communication connection can also be created or deleted.

WINDOWS LINUX

CreatePipe()

MapViewOfFile()

CreateFileMapping()

pipe()

shmget()-shared memory

mmap()

 

Here are the explanations of some system calls:

  • sleep()
    Whenever you call the Sleep method, it sets the process to wait until the specified amount of time has elapsed, and goes to find other processes to run. 
     
  • pipe()
    As the name suggests, a pipe is a connecting point between two processes so that the output from one becomes the input for the other. 
     
  • wait()
    During the execution of a process, it may wait for another process to complete. A parent process can create a child process that executes independently. In this case, the parent process may call wait(), which suspends the parent process while the child executes. 
     
  • kill()
    A process can be terminated using the kill() system called by the OS. It is important to note that a kill system call does not guarantee that the process will be terminated.
     
  • exit()
    This system call terminates the execution of the program. This call indicates that the thread has finished executing, especially in a multi-threaded environment. Following an exit() call, the OS gets back the resources used by the process.
     
  • fork()
    Processes are created using the fork() system call. With the fork() call, a process (a program in execution) creates an exact copy of itself. As a result, there are now two processes, a parent process, and a child process.
     
  • exec()
    Processes can also be created using the exec() system call. However, there is a significant difference between fork() and exec(). Fork() creates a new process without destroying the parent process. In contrast, exec() calls are designed to replace the address space, text segment, data segment, etc., of the existing process with that of the new process.

When do we need system calls?

The following situations require system calls:

  • Creating or deleting files is needed for a file system. Similarly, reading or writing files requires a call to the file system.
  • New process development and management.
  • System calls are also required for network connections. For example, sending and receiving packets is one of these.
  • It is necessary to make a system call to access hardware such as a printer, scanner, etc.

Must Read Evolution of Operating System

You can also read about the Memory hierarchy in computer network

Frequently Asked Questions

Who handles system calls?

The user threads are mapped to the kernel threads. Whenever a user thread pool makes a system call, its corresponding kernel thread pool handles the request.

How can system calls be accessed?

A developer may not access the system calls directly, but an API can provide access to them. The API includes functions that call the actual system calls. The API provides the following benefits:
1) A program that uses an API can compile and run on any system that supports that API.
2) APIs are often more user-friendly than system calls because they can be accessed anytime.

Difference between interrupt and system call?

Interrupts: Interrupts are signals that interrupt the regular operation of the processor. They may either be produced by hardware or by software. When a processor receives an interrupt, it stops all execution of current processes, saves the context to the stack, and jumps to the interrupt handling routine.
System Calls: Applications and user space processes call system calls to request Kernel services. In Linux, system calls are implemented through software interrupts, which can instantly switch between user and kernel modes.

Are system calls triggered by hardware?

Interrupts are events that change the order in which the processor executes instructions. Interrupts may be planned or unplanned. The hardware doesn't initiate system calls asynchronously, so they are not interrupting. During a system call, the code stream continues to run, but not during an interrupt.  

Conclusion

A system call helps a user program communicate with the operating system. An API (Application Programming Interface) or a program written by the user is typically written in user mode; on the other hand, a program written in kernel mode requires access to the OS's functionality. For this reason, we switch between kernel- and user-mode with a system call. 

Recommended Readings:


Do check out The Interview guide for Product Based Companies as well as some of the Popular Interview Problems from Top companies like Amazon, Adobe, Google, Uber, Microsoft, etc. on Coding Ninjas Studio.

Also check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc. as well as some Contests, Test Series, Interview Bundles, and some Interview Experiences curated by top Industry Experts only on Coding Ninjas Studio.

Live masterclass