System calls in the operating system (OS)
A system call is a programming technique that enables a computer program to request a service from the kernel of the operating system (OS) on which it is executing. A system call serves as a route of communication between programs and the operating system. A system call is made when a computer program sends a request to the kernel of the operating system. The Application Program Interface (API) gives user programs access to the operating system's functionality through system calls. It functions as a link between a process and an operating system, allowing user-level programs to request services from the OS. The only way into the kernel system is through system calls. System calls are required by every program that requires resources.
A system call allows a user program to communicate with the operating system. The program requests a number of services, and the operating system responds by making a series of system calls to complete the request. A system call can be written in either assembly or high-level languages like C or Pascal. System calls are predefined functions that the operating system can invoke directly if a high-level language is being used.
![System calls in the operating system (OS)](https://static.tutorialandexample.com/operating-system/system-calls-in-the-operating-system-os1.png)
A system call is started by a program that executes a certain instruction, prompting a transition to kernel mode and allowing the program to request a service from the operating system. The operating system then handles the request, executes the required procedures, and returns the result to the program.
System calls are critical for the effective operation of an operating system because they offer a standardized manner for programs to access system resources. Without system calls, each program would have to invent its own means of accessing hardware and system functions, which would lead to inconsistencies and failures. In kernel mode, the system call takes precedence. Following the system call, control is returned to the user, and user processes can be resumed.
The following conditions necessitate system calls:
- When a file system must generate or remove files.A system call is also necessary for reading and writing files.
- Creating and implementing new processes.
- System calls are also necessary for network connections. It includes Packet sending and receiving.
- A system call is necessary to acquire access to hardware devices such as a printer or scanner.
Services provided by System calls are:
System call provides several services in the operating system. Some main services are as follows:
- Process creation and management.
- Main memory management.
- File access, directory management, and file system administration.
- Device handling (I/O).
- Protection.
- Networking.
Types of System Calls
There are several types of system calls. Some main types of system calls are as follows:
- Process control: System calls deal with processes such as process creation, process termination, etc. Example: End, abort, create, terminate, allocate, and free memory.
- Device management: System calls handle device manipulation, such as reading from and writing to device buffers.
- File management: System calls are responsible for file manipulation, such as creating a file, reading a file, writing to a file, etc. Example: create, open, close, delete.
- Information management: System calls handle data interchange between the operating system and the user program.
- Communication: System calls help with inter-process communication. They also deal with the establishment and termination of communication connections.
Features of system calls:
There are various features of system calls in the operating system. Some main features of system calls are as follows:
- Interface: System calls provide a well-defined interface between user programs and the operating system. Programs make requests by calling specified functions, and the operating system replies by executing the requested service and delivering a response.
- Security: System calls are used to get access to privileged activities that normal user programs do not have access to. The operating system uses this privilege to protect the system from malicious or unauthorized access.
- Kernel Mode: When a system call is performed, the program is temporarily moved from user mode to kernel mode. In the system call, the program has access to all system resources, including hardware, memory, and other processes.
- Context transitioning: A system call necessitates a context switch, which entails storing the current process's state and transitioning to kernel mode to perform the requested service. It can create overhead, which can have an influence on system performance.
- Error Resolution: Error codes returned by system calls that indicate problems with the requested service. These errors must be detected and managed by software.
- Synchronization: System calls can be used to synchronize access to common resources such as files or network connections. The operating system includes synchronization techniques, like locks and semaphores, to guarantee that several programs may securely use these resources.
Advantages:
There are various advantages of system calls in the operating system. Some main advantages of system calls are as follows:
- System calls allow programs to gain access to hardware resources like CD drives, printers, and network interfaces.
- System calls enable programs to allocate and delegate memory and access memory-mapped hardware devices.
- System calls allow programs to initiate and terminate activities and manage inter-process communication.
- System calls enable programs to get access to privileged resources, such as changing system settings or doing operations that need administrator rights.
- System calls provide a standardized interface via which programs communicate with the operating system, ensuring consistency and compatibility across hardware platforms and operating system versions.
Working process of system calls:
In a system call, the user space is a region of memory that is located where the programs execute. A system call is used to contact an operating system's kernel, which functions in kernel space. A program must first gain kernel authorization before creating a system call. It performs this by sending an interrupt request, which holds the operating system and grants control to the kernel.
If the request is approved, the kernel will carry out the requested action, such as creating or deleting a file. The kernel's output is sent to the application as input. When the input is received, the operation is resumed by the application. When the process is completed, the kernel shifts data in memory from kernel space to user space and returns the results to the application. System calls are required for an operating system to work properly, and they provide a strong method for programs to communicate with the underlying system in a regulated and safe manner.
System Calls in Windows and UNIX:
Process | Windows | Unix |
File management | CreateFile() ReadFile() WriteFile() | Open() Read() Write() Close() |
Device management | SetConsoleMode() ReadConsole() WriteConsole() | Ioctl() Read() Write() |
Process control | CreateProcess() ExitProcess() WaitForSingleObject() | Fork() Exit() Wait() |
Communication | CreatePipe() CreateFileMapping() MapViewOfFile() | Pipe() Shmget() Mmap() |
Information management | GetCurrentProcessID() SetTimer() Sleep() | Getpid() Alarm() Sleep() |
Protection | SetFileSecurity() InitializeSecurityDescriptor() SetSecurityDescriptorgroup() | Chmod() Umask() Chown() |
Now, we will discuss every function of the system call one by one:
- open(): The open () system function can be used to access files on a file system. It provides the necessary file resources as well as a handle that the process may utilize. A file can be opened by numerous processes at the same time or by only one. Everything is built around the structure and file system.
- read(): It is used to obtain information from a file on the file system. In general, three reasons are accepted.
- An explanation of a file.
- A buffer that stores read data.
- How many bytes should the file can be read?
The file to be read might be recognized by its file descriptor and opened using the open() method prior to reading.
close(): It is used to stop file system access. When this system call is used, it indicates that the program no longer uses the file, the buffers are flushed, the file information is changed, and the file resources are de-allocated as a result.
wait(): A procedure may need to wait until another process has been completed before proceeding. When a parent process generates a child process, the parent process's execution is delayed until the child process is completed. The parent process is terminated by utilizing the wait() system function. Once the child process has ended, the parent process retakes control.
write(): It is used to write data from a user buffer to a device, such as a file. Using this system call, a program can generate data in one of two ways.
An explanation of a file.
A pointer to the buffer where data is kept.
In bytes, the quantity of data that will be written from the buffer.
fork( ): Processes utilize the fork() system function to make clones of themselves. It is one of the most used ways of creating processes in operating systems. When a parent process generates a child process, the parent process's execution is paused until the child process completes. When the child process is done, the parent process reclaims control.
exit(): Use the exit() system function to leave a program. In multi-threaded contexts, this call signals that the thread execution is complete. The operating system restores the process's resources after calling the exit() system function.