System calls are the interface between user programs and the operating system, allowing applications to request services like file access, process control, or device handling. They are the only way for a program to switch from user mode to kernel mode and safely use system resources managed by the kernel.
Types of System Calls
Services provided by an OS are typically related to any kind of operation that a user program can perform like creation, termination, forking, moving, communication, etc. Similar types of operations are grouped into one single system call category. System calls are classified into the following categories:
-(2).png)
1. File System Operations
These system calls are made while working with files in OS, File manipulation operations such as creation, deletion, termination etc.
- open(): Opens a file for reading or writing (text, audio, etc.).
- read(): Reads data from an opened file.
- write(): Writes or saves data to a file.
- close(): Closes an opened file.
- seek(): Moves the file pointer to a specific position in a file (e.g., jump to line 47 to read from there).
2. Process Control
These types of system calls deal with process creation, process termination, process allocation, deallocation etc. Basically manages all the process that are a part of OS.
- fork(): Creates a new child process by duplicating the parent process.
- exec(): Replaces the current process with a new program (overlay execution).
- wait(): Makes the parent process wait until its child process finishes.
- exit(): Terminates the current process.
- kill(): Sends a signal to a process (e.g., terminate, stop, or restart).
3. Memory Management
These types of system calls deals with memory allocation, deallocation & dynamically changing the size of a memory allocated to a process. In short, the overall management of memory is done by making these system calls.
- brk(): Sets the end address of the heap, directly resizing heap memory.
- sbrk(): Adjusts the heap size by a positive (increase) or negative (decrease) value.
- mmap(): Maps a file or device into a process’s memory space so it can be accessed like normal memory.
- munmap(): Removes a memory mapping from a process’s address space.
- mlock() / munlock(): Locks pages in memory to prevent swapping; unlock releases them back to normal management.
4. Interprocess Communication (IPC)
When two or more process are required to communicate, then various IPC mechanism are used by the OS which involves making numerous system calls. Some of them are :
- pipe(): Creates a one-way communication channel between processes (e.g., parent → child).
- socket(): Creates a network socket for communication between processes on the same or different systems.
- shmget(): Allocates a shared memory segment so multiple processes can access the same memory space.
- semget(): Creates or accesses a semaphore, used for process synchronization when sharing resources.
- msgget(): Creates or accesses a message queue, allowing processes to exchange messages in a structured way.
5. Device Management
The device management system calls are used to interact with various peripherial devices attached to the PC or even the management of the current device.
- SetConsoleMode(): Sets the input/output mode of the console (e.g., controls command-line behavior in Windows).
- WriteConsole(): Writes data to the console screen.
- ReadConsole(): Reads data from the console input.
- open(): Opens a file or device and returns a file descriptor for access.
- close(): Closes a previously opened file or device.
What is The Purpose of System Calls in OS?
System Calls act as a bridge between an operating system (OS) and a running program. They are usually written as assembly language instructions and are detailed in manuals for programmers working with assembly language.
When a program running in user mode needs to access a resource, it makes a System Call. This request is sent to the OS kernel to obtain the needed resource.
System Calls are used for various tasks, such as:
- Creating or executing files in the file system.
- Reading from and writing to files.
- Developing and managing new procedures in programs.
- Making network connections, including sending and receiving data packets.
- Accessing hardware devices like printers and scanners.
Methods to Pass Parameters to OS
If a system call occur, we have to pass parameter to the Kernel part of the Operating system. for example look at the given open() system call:
// Example showing how parameters are passed from user program to OS using open() system call
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
int main() {
int fd = open("test.txt", O_CREAT | O_RDWR, 0644);
if (fd < 0) {
perror("open failed");
return 1;
}
write(fd, "Hello OS\n", 9);
close(fd);
printf("File created and data written successfully.\n");
return 0;
}
Here pathname, flags and mode_t are the parameters. So it is to be noted that :
- We can't pass the parameters directly like in an ordinary function call.
- In kernel mode there is a different way to perform a function call.
So we can't run it in the normal address space that the process had already created and hence we can't place the parameters in the top of the stack because it is not available to the kernel of the operating system for processing. so we have to adopt any other methods to pass the parameters to the kernel of the OS. We can done it through,
- Passing parameters in registers
- Address of the block is passed as a parameter in a register.
- Parameters are pushed into a stack.
1. Passing Parameters in Registers
- It is the simplest method among the three, here we directly pass the parameters to registers.
- But it will be limited when, number of parameters are greater than the number of registers.
- Here is the C program code:
2. Address of The Block is Passed as Parameters
- It can be applied when the number of parameters are greater than the number of registers.
- Parameters are stored in blocks or table.
- The address of the block is passed to a register as a parameter.
- Most commonly used in Linux and Solaris.
- Here is the C program code:
3. Parameters Are Pushed in a Stack
- In this method parameters can be pushed in stack using the program and popped out using the operating system
- So the Kernel can easily access the data by retrieving information from the top of the stack.