Open In App

Introduction of System Call

In computing, a system call is a programmatic way in which a computer program requests a service from the kernel of the operating system it is executed on. A system call is a way for programs to interact with the operating system. A computer program makes a system call when it makes a request to the operating system’s kernel. System call provides the services of the operating system to the user programs via Application Program Interface(API). It provides an interface between a process and an operating system to allow user-level processes to request services of the operating system. System calls are the only entry points into the kernel system. All programs needing resources must use system calls. 

A user program can interact with the operating system using a system call. A number of services are requested by the program, and the OS responds by launching a number of systems calls to fulfill the request. A system call can be written in high-level languages like C or Pascal or in assembly language. If a high-level language is used, the operating system may directly invoke system calls, which are predefined functions.



A system call is a mechanism used by programs to request services from the operating system (OS). In simpler terms, it is a way for a program to interact with the underlying system, such as accessing hardware resources or performing privileged operations.

A system call is initiated by the program executing a specific instruction, which triggers a switch to kernel mode, allowing the program to request a service from the OS. The OS then handles the request, performs the necessary operations, and returns the result back to the program.



System calls are essential for the proper functioning of an operating system, as they provide a standardized way for programs to access system resources. Without system calls, each program would need to implement its own methods for accessing hardware and system services, leading to inconsistent and error-prone behavior.

Services Provided by System Calls

Features of System Calls

System Calls Advantages

How does System Call Work?

Here is the detailed explanation step by step how system call work:

Examples of a System Call in Windows and Unix

System calls for Windows and Unix come in many different forms. These are listed in the table below as follows:

Process Windows Unix
Process Control

CreateProcess()

ExitProcess()

WaitForSingleObject()

Fork()

Exit()

Wait()

File manipulation

CreateFile()

ReadFile()

WriteFile()

Open()

Read()

Write()

Close()

Device Management

SetConsoleMode()

ReadConsole()

WriteConsole()

Ioctl()

Read()

Write()

Information Maintenance

GetCurrentProcessID()

SetTimer()

Sleep()

Getpid()

Alarm()

Sleep()

Communication

CreatePipe()

CreateFileMapping()

MapViewOfFile()

Pipe()

Shmget()

Mmap()

Protection

SetFileSecurity()

InitializeSecurityDescriptor()

SetSecurityDescriptorgroup()

Chmod() 

Umask()

Chown()

open(): Accessing a file on a file system is possible with the open() system call. It gives the file resources it needs and a handle the process can use. A file can be opened by multiple processes simultaneously or just one process. Everything is based on the structure and file system.

read(): Data from a file on the file system is retrieved using it. In general, it accepts three arguments:

  1. A description of a file.
  2. A buffer for read data storage.
  3. How many bytes should be read from the file
    Before reading, the file to be read could be identified by its file descriptor and opened using the open() function.

wait(): In some systems, a process might need to hold off until another process has finished running before continuing. When a parent process creates a child process, the execution of the parent process is halted until the child process is complete. The parent process is stopped using the wait() system call. The parent process regains control once the child process has finished running.

write(): Data from a user buffer is written using it to a device like a file. A program can produce data in one way by using this system call. generally, there are three arguments:

  1. A description of a file.
  2. A reference to the buffer where data is stored.
  3. The amount of data that will be written from the buffer in bytes.

fork(): The fork() system call is used by processes to create copies of themselves. It is one of the methods used the most frequently in operating systems to create processes. When a parent process creates a child process, the parent process’s execution is suspended until the child process is finished. The parent process regains control once the child process has finished running.

exit(): A system call called exit() is used to terminate a program. In environments with multiple threads, this call indicates that the thread execution is finished. After using the exit() system function, the operating system recovers the resources used by the process.

Methods to pass parameters to OS

If a system call occur, we have to pass parameter to the Kernal part of the Operating system.

For example look at the given open() system call:




//function call example
 
#include <fcntl.h>
 
int open(const char *pathname, int flags, mode_t mode);

Here pathname, flags and mode_t are the parameters.

So it is to be noted that :

So we can’t run it in the normal address space that the process had already created and hence we cant place the parameters in the top of the stack because it is not available to the Kernal of the operating system for processing. so we have to adopt any other methods to pass the parameters to the Kernal of the OS.

We can done it through,

  1. Passing parameters in registers
  2. Address of the block is passed as a parameter in a register.
  3. Parameters are pushed into a stack.

Let us discuss about each points in detail:

1. Passing parameters in registers.




// Passing parameters in registers.
 
#include <fcntl.h>
#include <stdio.h>
 
int main()
{
    const char* pathname = "example.txt";
    int flags = O_RDONLY;
    mode_t mode = 0644;
 
    int fd = open(pathname, flags, mode);
  // in function call open(), we passed the parameters pathanme,flags,mode to the kernal directly
 
    if (fd == -1) {
        perror("Error opening file");
        return 1;
    }
 
    // File operations here...
 
    close(fd);
    return 0;
}

2.Address of the block is passed as parameters




//Address of the block is passed as parameters
 
#include <stdio.h>
#include <fcntl.h>
 
int main() {
    const char *pathname = "example.txt";
    int flags = O_RDONLY;
    mode_t mode = 0644;
 
    int params[3];
          // Block of data(parameters) in array
    params[0] = (int)pathname;
    params[1] = flags;
    params[2] = mode;
 
    int fd = syscall(SYS_open, params);
          // system call
 
    if (fd == -1) {
        perror("Error opening file");
        return 1;
    }
 
    // File operations here...
 
    close(fd);
    return 0;
}

3.Parameters are pushed in a stack




//parameters are pushed into the stack
 
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
 
int main() {
    const char *pathname = "example.txt";
    int flags = O_RDONLY;
    mode_t mode = 0644;
 
    int fd;
    asm volatile(
        "mov %1, %%rdi\n"
        "mov %2, %%rsi\n"
        "mov %3, %%rdx\n"
        "mov $2, %%rax\n"
        "syscall"
        : "=a" (fd)
        : "r" (pathname), "r" (flags), "r" (mode)
        : "%rdi", "%rsi", "%rdx"
    );
 
    if (fd == -1) {
        perror("Error opening file");
        return 1;
    }
 
    // File operations here...
 
    close(fd);
    return 0;
}

Frequently Asked Question

Q.1: How does a system call work?

Answer:

When a program executes a system call, it transitions from user mode to kernel mode, which is a higher privileged mode. The transition is typically initiated by invoking a specific function or interrupting instruction provided by the programming language or the operating system.

Once in kernel mode, the system call is handled by the operating system. The kernel performs the requested operation on behalf of the program and returns the result. Afterward, control is returned to the user-level program, which continues its execution.

Q.2: Why are system calls necessary?

Answer: 

System calls are necessary for several reasons:

Access to privileged operations: Many operations, such as managing hardware devices or modifying system configurations, require higher privileges that are only accessible through system calls.

Resource management: System calls provide a standardized interface for allocating and managing system resources like memory, files, and devices, ensuring fair and controlled access by different processes.

Abstraction: System calls abstract the underlying complexities of the operating system, allowing application developers to interact with the system in a higher-level, platform-independent manner.

Security and protection: System calls enforce access control and security policies, preventing unauthorized access to sensitive resources and protecting the integrity of the system.


Article Tags :