What is system calls in linux

System Calls in Linux

System calls are an integral part of the Linux architecture. Learn about the most common types of system calls in Linux.

System calls are slightly different from what it suggests from its naming schema.

It is a standard interface by which the user programs can interact with the Linux kernel to access the resources safely.

Still, sounds complex? Let me explain.

What are the system calls in Linux

When running a computer, you interact with various programs that require system resources like network, filesystem, memory, and CPU. And here’s where the concept of system calls kicks in! When a program requires a system resource, it sends a request for that resource to the kernel by making a system call.

Linux system calls

Suppose you run a text editor to open a file and make changes. So here, the text editor will send system calls like open() , write() , and close() .

In fact, every Linux command and program makes several types of system calls. You can easily see them with the strace command.

Take a look at how many system calls a simple echo command makes:

Using strace to see system calls in Linux

Types of system calls in Linux

The system calls are divided into five categories:

  1. Process management system calls
  2. File management system calls
  3. Device management system calls
  4. Network management system calls
  5. System information system calls

While there are a huge number of system calls, I’ll briefly discuss the most popular calls of each category.

So let’s start with the first one.

1. Process management system calls

The process management system calls provide a way to create, manage and control the processes. In simple words, process management is a way to handle multiple processes and plays a crucial role in multitasking.

And these are some common process management system calls:

  • fork() : As the name suggests, this creates a child process from the parent process which results in the exact same process except for some values like PID and parent PID.
  • exec() : This system call replaces the current process image with the new image and it is used when you want to run a different program from the current process.
  • wait() : This system call waits to will the child process gets terminated and then gives some information about the child process. The prime example of this would be the exit status.
  • exit() : This system call terminates the current process and returns the resources acquired by that process (which was terminated recently) to the system.

2. File system calls

The filesystem calls in Linux facilities other programs to interact with the filesystem.

Here are some of the popular system calls:

  • open() : As the name suggests, it is used to open the file.
  • read() : This system call can be used to read data from a wide range of data types including regular files, and special files like pipes and sockets, and it can also read from device files like /dev directory.
  • write() : It is used to modify files, generate log files, reports, etc.
  • close() : This will close the file and save the changes to the disk(if any).
Читайте также:  Linux ssh root разрешить

3. Network system calls

The network system calls are used to manage networks, send/receive data, resolve network addresses, etc.

And here are some of the popular network system calls:

  • socket() : This system call is used to create sockets which are endpoints for communication.
  • bind() : It is used to bind a socket to the specific address and port on the local network.
  • listen() : It is used to mark the socket as a passive listener so it can accept incoming requests from the other hosts.
  • accept() : This system call is used to accept the new incoming connection request.
  • connect() : It establishes the connection to the external network endpoint.
  • send() : As the name suggests, it is used to send data over the socket.
  • recv() : It is used to receive data over the socket.

4. Device management system calls

The device management system calls include all the calls mentioned in the File system calls so I won’t mention them here.

The device management system calls are used to manage devices that are connected to your system. In simple terms, the device management system calls provides a way to read/write data, and control and configure those devices.

Here are some common device management system calls:

ioctl() : It is used to send control commands to the connected devices. Think of you controlling a robotic arm using a computer! Yep, that’s how it is used.

mmap() : It is used to map the partition of a file into the memory of the own process. This results in direct access to the memory-mapped data as if it was a part of the process’s own memory.

For example, let’s say you want to process a large file in a program so rather than loading a whole file, you can map a portion of a file, use it and then unmap it.

5. System information system calls

As the name suggests, the system information system calls are used to get various kinds of information such as system resources, system time, system configuration, etc.

Here are some of the most commonly used system information calls:

  • getpid() : Gets the PID (process ID) of the current process.
  • getppid() : Gets the parent PID of the current process.
  • getuid() : It will get the UID (used ID) of the current process.
  • getgid() : It will get the GID (group ID) of the current process.
  • uname() : This system call gets the information about the system name, version, release info, and other system-related info.
  • sysinfo() : It gets general information about the system which includes free memory, total memory, number of current processes, etc.
  • time() : It gets the current system time in seconds since 1t January 1970.

Pretty basic yet crucial to make your system work. Isn’t it?

You can get detail information on each system call through the man pages. Since they are in section 2, use the man command like this: man 2 system_call_name

Your call on system calls

You may wonder why the system needs to call for resources in the first place.

Читайте также:  Linux запустить команду от root

If your applications are directly allowed to use the system resource, it will result in a significant security threat and conflicts over the system.

And it is the same reason why it is not recommended to use Linux as root (always) and why you need to use sudo, which elevates the privileges for some time.

Here are a few interesting take on system calls I found on the web:

And a good resource on the strace command use:

I hope you have a little better understanding of the system calls now.

Источник

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.

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.

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.

Читайте также:  Linux java add jar

Run the gcc compile instruction to compile this C code.

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

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.

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:

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.

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.

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.

Conclusion:

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.

Источник

Оцените статью
Adblock
detector