C Programming

What is a System Call in Linux And How Does it Work with Examples

A system call is a function that allows a process to communicate with the Linux kernel. It’s just a programmatic way for a computer program to order a facility from the operating system’s kernel. System calls expose the operating system’s resources to user programs through an API (Application Programming Interface). System calls can only access the kernel framework. System calls are needed for all the services that need resources.

The Linux kernel is proprietary software that loads and operates on the device at the least potential stage. Its job is to organize all that happens on the machine, from the keyboard, disk drive, and network events to providing time slices for concurrent execution of different programs. The separation of software and hardware creates a secure bubble that improves protection and reliability. Unprivileged applications are unable to reach other program’s storage, and if one fails, the kernel suspends the process so that it does not damage the entire system.

Wafer Thin Wrapper:

The Linux system calls are not rendered explicitly to the kernel in certain programs. Almost all the programs use the basic C library and offer a lightweight but essential wrapper over the Linux system calls. The repository then provides the accompanying Linux machine call after ensuring that the feature parameters are translated into the right processor registers. Whenever the wrapper receives data from the system call, it analyses it and contributes it to the program clearly. Any machine-interactive operation in a program is ultimately converted into a system call. So, let’s have a look at some of them. There is a long list of Linux system calls which we can use in our Linux system. Here is the list of some common and mostly used Linux system calls.

  • Open
  • Close
  • Exec
  • Write
  • Read
  • Lseek
  • Select

Let’s discuss some of the Linux system calls using the C language in our article to get hands-on with it.

Open System Call:

We can use the “Open” system call in our Linux distribution to swiftly open the document, which we will specify in our code of C language. Launch the command terminal firstly. You can use the shortcut “Ctrl+Alt+T”. Suppose you have a text file “test.txt” in the home directory, and it contains some content in it. So, in the beginning, you have to create a new C type filename “new.c” in the terminal via nano editor. Therefore, try out the simple below nano instruction.

$ nano new.c

Now, the Nano editor has been launched. Type the below-shown code in it. We have two file descriptors in the code. Both the files can be opened using the open system call. The first descriptor contains a read call, and the second contains the write function. The first open call is opening the text file “test.txt” and saving its content into file descriptor “fd”. The second open system call is creating a file named “target”. The document “target” has been reimbursed to an “fd1” file descriptor. The write instruction is used to transcribe the bytes of data in the buffer. Tap “Ctrl+S” to save the code and hit the shortcut key “Ctrl+X” to quit the file.

Run the gcc compile instruction to compile this C code.

$ gcc new.c

Let’s execute the code using the simple “a.out” query in the shell as follows:

$ ./a.out

The output data has been transmitted to the file “target”. Let’s check the “target” file using the “cat” query. The output screen is showing the 20 character data in the “target” file.

$ cat target

Exec System Call:

The exec system call is being cast off to run a file that is currently being processed. The former executable file is substituted, and the current file is operated whenever exec is called. By using an exec system call, we may assume that doing so will overwrite the old document or application in the loop with a fresh one. New software is used to override the whole process’s material. The document whose title is given in the statement whenever invoking exec() is substituted for the user information section that runs the exec() system call (). So open the command terminal and, using the nano editor, create a new C type file as follows:

$ nano exp.c

The editor has been opened now. Write out the whole below C language code in it. There are three main libraries included in it. After that, the main function has been instantiated. The print statement has been showing the string data and the Process Id of the file “exp.c”. The getpid() function has been used for this purpose. Then we have a character type array with some values in it. The exec system call has been used to take the file name and the one-line above array as an argument. Now the file “hello.c” will be processed. After that, another print statement comes so far, but it will never be executed. Press “Ctrl+S” to save this file. Hit “Ctrl+X” to exit.

Now it’s time to create another c file, “hello.c” using the nano editor. Use the below query in the shell to do so.

$ nano hello.c

Write the below code in it. This code contains two print statements in the main function. The first is only printing a string given in it, and the second one is printing the string while fetching the process ID of the currently used file, which is “hello.c”.

Let’s compile both files one after another using gcc.

$ gcc –o exp exp.c

$ gcc –o hello hello.c

When we execute the exp.c file, it will output the first print statement from the exp.c file and both the print lines from the hello.c file.

$ ./exp


We have elaborated on the whole concept of Linux system calls and how they can be used in your Linux system. We have used Ubuntu 20.04 while implementing this concept.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.