Introduction to system calls

In operating systems, the user is not allowed to directly access the kernel from user mode, and the user needs an interface between the user process and operating systems to access the kernel services. System calls provide this interface.

In this answer, we'll learn about system calls, why we need them, and what sort of functionalities system calls can provide us.

System calls

In operating systems, the system call is a systematic way of requesting services from the kernel of the operating system on which its executed. We can also say it's a way the user program can interact with the operating system. The user can only request the operating system's services using system calls. Have a look at the figure below:

Note: The system calls provide OS services through Application Program Interface (API).

Why system calls?

We require systems calls in the following scenarios:

  • To access any hardware device.

  • Establishing a network connection includes sending and receiving data packets over the network.

  • Whenever we want to create a new process or manage existing processes.

  • Read or writing from files, file system use system calls.

These were some general situations where system calls are necessary, but other conditions also exist. To understand them better, let's briefly discuss system calls' function.

Functions of system calls

The following are the functions of system calls and the solution to the situation mentioned above:

  • We can use system calls to create and manage processes.

  • System calls help in managing main memory.

  • We can use system calls' file system to manage file access and directories.

  • The user can handle all Input-Output devices(I/O).

  • It enhances the process's security.

You may have gotten an idea of how essential system calls are. Let's discuss its types now.

Types of system calls

There are following five different types of system calls:

  • Process control

  • File management

  • Device management

  • Information maintenance

  • Communication

Let's discuss each category briefly:

Process control

This system handles all services related to processes such as process termination, creation, and so on.

For example, some Linux process control system calls are fork(), exit(), wait(), and more.

File management

This system call is responsible for all services related to file manipulation, such as writing into a file, reading from a file, creating a file, and so on.

For example, some Linux file management system calls are open(), write(), read(), close(), and more.

Device management

This system call is responsible for all services related to device manipulation, such as reading or writing to the device buffers.

For example, some Linux device management system calls are ioctl(), read(), and more.

Information maintenance

This system call is responsible for all services related to information handling and its transfer between the process or operating system and the user program.

For example, some Linux information maintenance system calls are : getpid(), alarm(), sleep(), and so on.

Communication

This system call is responsible for inter-process communication between co-operating processes. These calls are also responsible for handling the communication link between processes.

For example, some Linux communication system calls are pipe(), shmget(), mmap(), and more.

Important system calls

Let's discuss some of the essential systems calls provided by the OS and primarily used. These system calls are related to file management or file handling:

  • Open (): This allocates resources to a file and provides a pointer that a process uses to point to that file. This file can be opened by several processes simultaneously, depending on the file system.

  • Read (): This system call is used to access the data from a file stored in the file system. Before reading, the file should be opened by open() a system call and can be identified by its file descriptor.

  • Write (): This system call is used to write the data to a file that is stored in the file system. Before reading, the file should be opened by open() a system call and can be identified by its file descriptor. It's one of the ways to output data from a

  • Close(): This call closes all the resources allocated to that file that was opened using open() the call. It's used to terminate access to the file system. All buffers get clear when you use this call, and the file metadata is updated.

Code example

#include<stdio.h>
#include<fcntl.h>
#include<errno.h>
#include<stdlib.h>
#include<string.h>
extern int errno;
int main()
{
// Using open system Call with read only mode
int file_d = open("tmp.txt", O_RDONLY | O_CREAT);
printf("Opening the file with file_d = %d \n", file_d);
if (file_d ==-1)
{
// print type of error
printf("Error No # % d\n", errno);
// print program detail "Success or failure"
perror("Program Error");
}
// Using close system Call
close(file_d);
// Using Open system Call with write only mode
file_d = open("tmp.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644);
// Using write system Call
file_d = write(file_d, "hello from team Educative\n", strlen("hello from team Educative\n"));
printf("called write (hello from team Educative) \n");
// Using close system Call
close(file_d);
printf("closed the file.\n");
return 0;
}

Explanation

  • Line 16: We open a temporarily created text file with read-only mode by the open() system call.

  • Line 30: We close the file using close() system call.

  • Line 33: We open a temporarily created text file with write-only mode by the open() system call.

  • Line 36: We write to the file using write() system call.

Copyright ©2024 Educative, Inc. All rights reserved