Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Linux provides users not only with the greatest comfort level. But also with the highest security and stability. To provide the best security and stability, many steps are taken. One of the most important steps is the strict separation of the os core (or the kernel) and user application. But this limitation is that processes that do not belong to the system have no direct access to the memory and the CPU. And instead, they rely on so-called system calls.
In this article, we will learn about the system call in Linux with its various types and examples. So let’s get started.
What is a System call in Linux?
A system call is a method that acts as a bridge between a process and the operating system. It is the method by which a computer software requests a service from the operating system's kernel.
A system call in Linux transfers control from unprivileged user mode to privileged kernel mode. Linux has various libraries to manage system calls. They take care of collecting the system-call arguments. And arranging the arguments in special forms.
Why are system calls necessary?
To understand the primary need for system calls in Linux, we need to know the operating system's working. A modern operating system can roughly be divided into two modes:
Kernel mode: It is a powerful and privileged mode used by the operating system kernel.
User mode: It is where most user applications run.
User mode mainly uses command-line programs and graphical user interfaces (GUI). System calls operate in the background, interacting with the kernel mode to complete tasks.
Types of System Calls in Linux
As we know, system calls are control units for the interaction between application processes and the operating system. These system calls are mainly classified into five categories.
Process control
All processes in a computer system must be controlled so that they can be stopped or managed by other processes at any moment. As a result, system calls in this category check the execution and termination of processes. System calls in Linux under process control are fork(), exit(), and exec().
It is one of the most used systems called under process control.
exit()
The exit() method is used by a program or process to terminate its execution.
The operating system can reclaim resources used by the process after the exit() system call.
exec()
The exec() method is used to execute the user program.
After the exec() system call, the currently running process is immediately terminated. And the new process starts executing.
2. File Management
User programs need file management system calls to access common file operations. These file manipulation methods include 'create,' 'delete,' 'open,' 'close,' 'write,' and 'read.'
open():
open() is the system call for opening a file.
read():
read() system call opens the file in read mode.
Many programs can call the read() method on the same file.
write():
write() system call opens the file in write mode.
Many programs can not execute the write() method on the same file.
close():
This system call will close the opened file.
3. Device management
Device management is useful for jobs like writing into device buffers, reading from device buffers, etc. The Linux System calls under device management is ioctl().
ioctl():
ioctl() is the Input and Output Control in Linux.
ioctl is the system call for input/output operations that common system calls cannot express.
4. Information maintenance
User programs are related to a large amount of information. It handles information transfer between the OS and the user program. The System calls under this are getpid(), alarm(), and sleep().
getpid():
getpid refers to Get the Process ID.
The getpid() method returns the process ID of the calling program.
alarm():
This system call sets an alarm clock for the delivery of a signal i.e., when it has to be reached.
sleep():
This System call temporarily suspends the execution of the running process.
5. Inter-process communication
The OS and the various active applications interact smoothly if the individual processes are coordinated with one another. Hence, communication via proper system calls is essential. The system calls under this are pipe(), shmget(), and mmap().
pipe():
The pipe() system call is useful in communicating between different programs.
It is mainly useful in inter-process communication.
The pipe() method opens the file descriptors.
shmget():
shmget refers to the shared memory segment.
This system call is useful in accessing the shared memory and the messages to communicate with the programs.
mmap():
This system call is useful in mapping or unmapping files or devices into memory.
Example of a System call in Linux
In the above section, we learned about the system call in Linux. Now, let’s see an example to understand it better.
In our example, we will list the files inside a folder. We will see how the system call works internally to list the files.
Step 1: Before moving on with the example, e will create a folder called “Codingninjasdir.” And two files inside it, namely “file1” and “file2,” using the terminal in Linux. Below is the command for the same.
Step 2: Now, we can list the files inside the “testdir” folder using the “ls testdir/” command in the Linux terminal.
The ls command will internally call the functions from system libraries (i.e., glibc) on Linux. This library invokes the system calls that do most of the tasks. The system call involved in the above process are as follows:
opendir(“testdir/”): It opens the testdir folder.
readdir(): It reads the files inside the folder, i.e., file1 and file2.
closedir(): It closes the opened folder.
Let’s take another example for C++ or C file type and their editing options.
We will have to create a new C/C++ file with the command touch test. cpp.
Now we will open the file in the nano editor with the command nano test.cpp. It will open as:
After writing the code in the nano editor, enter ctrl + s to save the file and then ctrl + x to exit the editor.
For compiling the code, we will type the command g++ -o test test.cpp, Now for executing the code, we will type./test (The executable file of test.cpp). The Series of commands will be as:
Frequently Asked Questions
What are the types of System Calls in Linux?
System calls in Linux are mainly of five types. Process Control, File Management, Device Management, Information Maintenance, and Communication.
When do we need system calls?
System calls are necessary whenever any process running in user mode wishes to use a function or service that can only be run in the kernel mode.
How do I list system calls in Linux?
To list system calls in Linux, you can use tools like strace or syscall utility. Simply execute your program with strace or use syscall with appropriate options to display system calls.
How many system calls does Linux implement?
Linux implements over 300 system calls, providing essential functions for interacting with the kernel. Common operations include process management, file system manipulation, input/output, memory management, and networking.
Conclusion
In this article, we saw that the system calls in Linux are methods that act as a bridge between the user app and OS. We also learned the five primary types of system calls in Linux, with an example.
If you found this blog interesting, please visit these articles: