The operating system provides two main functions, acting as an interface between the user and the hardware. And to allocate resources appropriately. For the most part, allocating resources happens in the background and is not visible to the user. But the former part of acting as an interface happens on the GUI. For example, this includes reading, writing, creating, and deleting files on any directory. The user interface of an operating system is primarily for dealing with abstractions. All this happens by various system calls.
System calls differ from one OS to another, but the underlying concept remains the same.
What are system calls in OS?
Let’s say we have a company that offers various tools and resources to clients.
- Clients need these tools and resources as a service.
- A manager communicates with the clients and asks the company to serve as per the clients’ requirements.
- The clients ask for tools and resources to the manager, according to their needs.
- The manager follows a process to get the service from the company.
Here, the manager works as a mediator between the clients and the company.
In this scenario, the company is the operating system. The tools and resources are the resources of the OS. The clients are the users. And the manager is a mediator between the company and the clients, which takes up the role of system calls.
Definition: In computing, there is an interface between the operating system and the user program, and it is defined by a set of extended instructions that the operating system provides, and that set of instructions is called system calls.
In other words, a system call is a way for programs to interact with the operating system. It is a way in which a computer program requests a service from the kernel of the operating system.
How are system calls made?
A computer program makes a system call when it needs to request the operating system’s kernel. The system call provides the services of the operating system to the user programs via an API (Application Program Interface). It is the only entry point into the kernel system. As system calls work as an interface between the operating system and the user programs, all those programs or processes that need resources for their execution have to use system calls.
Now, we need to understand the big difference which lies between a system call and a user function. Following are listed some of the several ways in which a system call differs from a user function.
- A system call has more privilege than a regular subroutine. A system call runs with kernel-mode privilege in the kernel protection domain.
- System call code and data are located in global kernel memory.
- System call routines can create and use kernel processes to perform asynchronous processing.
- System calls cannot use shared libraries or any symbols not found in the kernel protection domain.
Types of System Calls in OS
There are five main types of system calls.
- Process control: These include CreateProcess, ExitProcess
- File management: These include CreateFile, ReadFile, DeleteFile
- Device management: These include RequestDevice, ReadDevice
- Information maintenance: This includes GetLocalTime
- Communication: This includes CreatePipe
The five different system calls are self-explanatory, but you can read about these types of system calls in detail before proceeding.
How to Make a System Call in Operating Systems
Since we now know what a system call is, we move on to learn how a system call is created. To make a system call, a certain set of steps is followed.
Let’s start from the beginning. There are 11 steps involved, and all of them have been pointed out in the above figure. We will take the example of a simple ‘read system call.’ It has three parameters along with it, the first one specifying the file, the second specifying the pointing to the buffer, and the third one giving the number of bytes to read.
As with all system calls, this is invoked from C programs by calling a library procedure with the same name, read. This is the same name as the operation we are doing. It differs from other system calls. The call from the C program may look like this:
count = read(fd, buffer, nbytes)
11 steps involved in making a system call
- In steps 1-3, the calling program pushes the parameters onto the stack. The first and third parameters are called by value, but the second one is called by its address as denoted by the & symbol.
- In step 4, the actual call to the library procedure is made. This instruction is the normal procedure call instruction used to call all procedures.
- In step 5, the library procedure places the system call number in a place where the operating system expects it, such as a register.
- In step 6, the library procedure executes a TRAP instruction to switch from user mode to kernel mode and start execution at a fixed address within the kernel.
- In step 7, the kernel examines the system call number and then dispatches it to the correct system call handler. This correct number is given in the table of system call handlers by pointers referenced at the system call number.
- In step 8, the system call handler runs.
- In step 9, the operation is completed, and the user is given back control once the TRAP instruction is set.
- In step 10, this procedure returns to the user program, like how all normal library procedures do.
- In step 11, the operating system has to clear the stack, so it increments it enough so that it is empty.
Examples of System Calls for Process Management
A lot of services such as memory management, file system management, etc. are provided by the system calls, and one of them is process management.
A set of system calls given below is used to manage a process:
- fork( ): To create a process we use a method called fork( ). It creates a child process identical to the parent in every way.
- exec( ): To run a program, that is, to execute a program, exec( ) method is used.
- wait( ): To make a process wait, wait( ) is used.
- getpid( ): Every process has a unique process id, to get that unique id getpid( ) is used.
- getppid( ): To get the unique process id of a parent process, getppid( ) is used.
- exit( ): It is used to terminate a process with an exit status.
Examples of System Calls for File Management
For File Management, following system calls mainly are used:
- open(): This system call is used to open a file for reading, writing, or both.
- read(): To read the content from a file into the buffer, we use a read() system call.
- write(): It is used to write content into the file from the buffer.
- Close (): This system call is used to close the opened file.
Examples of System Calls for Directory Management
For Directory Management, following system calls are mainly used:
- mkdir( ): Used to create a new directory.
- rmdir( ): Used to remove a directory.
- link( ): Used to create a link to an existing file.
- opendir( ): Used to open a directory for reading.
- closedir( ): This is used to close a directory.
Examples of Miscellaneous System Calls
Till now, we have seen some of the service-specific system calls. Following is a list which contains some of the miscellaneous system calls:
- chdir( ): Used to change the working directory.
- chmod( ): Used to change a file’s protection bits.
- kill( ): Used to send a signal to a process.
- time( ): It gets the elapsed time since January 1, 1970.
- ulimit( ): Used to get and set user limits.
- acct( ): It is used to enable or disable process accounting.
- alarm( ): It is used to set a process alarm clock.
- modload( ): It loads dynamically loadable kernel module.
- moduload( ): It unloads the kernel module.
- modpath( ): It is used to change the path from which the modules are loaded.