System calls act as intermediaries between applications and the kernel. They provide an abstraction layer that shields essential system components, like the CPU and memory, from inadvertent errors and targeted attacks.
Learn how different system calls work, understand their features, and ensure your system runs securely and predictably.
What Is a System Call?
A system call is an interface between a program running in user space and the operating system (OS). Application programs use system calls to request services and functionalities from the OS's kernel. This mechanism allows the program to call for a service, like reading from a file, without accessing system resources directly.
When a program invokes a system call, the execution context switches from user to kernel mode, allowing the system to access hardware and perform the required operations safely. After the operation is completed, the control returns to user mode, and the program continues its execution.
This layered approach facilitated by system calls:
- Ensures that hardware resources are isolated from user space processes.
- Prevents direct access to the kernel or hardware memory.
- Allows application code to run across different hardware architectures.
What Is the Purpose of the System Call?
System calls serve several important functions, which include:
- User-Kernel Boundary. System calls serve as the authorized gateway for user programs when requesting services from the kernel. They ensure that user programs cannot arbitrarily access kernel functions or critical system resources.
- Resource Management. User programs can request and manage vital resources like CPU time, memory, and file storage via system calls. The OS oversees the process and guarantees that it is completed in an organized manner.
- Streamlined Development. System calls abstract the complexities of hardware. This allows developers to perform operations like reading and writing to a file or managing network data without needing to write hardware-specific code.
- Security and Access Control. System calls implement checks to ensure that requests made by user programs are valid and that the programs have the necessary permissions to perform the requested operations.
- Inter-Process Communication (IPC). System calls provide the mechanisms for processes to communicate with each other. They offer features like pipes, message queues, and shared memory to facilitate this inter-process communication.
- Network Operations. System calls provide the framework for network communications between programs. Developers can devote their attention to building their application's logic instead of focusing on low-level network programming.
How Do System Calls Work?
This high-level overview explains how system calls work:
1. System Call Request. The application requests a system call by invoking its corresponding function. For instance, the program might use the
read() function to read data from a file.
2. Context Switch to Kernel Space. A software interrupt or special instruction is used to trigger a context switch and transition from the user mode to the kernel mode.
3. System Call Identified. The system uses an index to identify the system call and address the corresponding kernel function.
4. Kernel Function Executed. The kernel function corresponding to the system call is executed. For example, reading data from a file.
5. System Prepares Return Values. After the kernel function completes its operation, any return values or results are prepared for the user application.
6. Context Switch to User Space. The execution context is switched back from kernel mode to user mode.
7. Resume Application. The application resumes its execution from where it left off, now with the results or effects of the system call.
Note: The exact number of steps and the functionality of system calls can vary depending on the operating system.
What Are the Features of System Calls?
The following features are indicative of system calls:
- Security. System calls ensure that user-space applications cannot harm the system or interfere with other processes.
- Abstraction. For example, programs do not need to know the specifics of network hardware configurations to send data over the internet or disk operations to read a file, as the OS handles these tasks.
- Access Control. System calls enforce security measures by checking whether a program has the appropriate permissions to access resources.
- Consistency. Interactions between the OS and program remain consistent, regardless of the underlying hardware configuration. The same program can run on different hardware if the operating system supports it.
- Synchronous Operation. Many system calls operate synchronously, blocking the calling process until the operation is complete. However, there are also asynchronous system calls that allow processes to continue execution without waiting.
- Process Control. System calls facilitate stable process management and multitasking through process creation, termination, scheduling, and synchronization mechanisms.
- File Management. System calls support file operations such as reading, writing, opening, and closing files.
- Device Management. System calls enable processes to request device access, perform read or write operations on these devices, and release them afterward.
- Resource Management. System calls help allocate and deallocate resources like memory, CPU time, and I/O devices.
- Maintenance. System calls are used to obtain or configure system information, such as the date and time or process status.
- Communication. System calls allow processes to communicate with each other and synchronize their actions.
- Error Handling. When a system call cannot be completed, it returns an error code that the calling program can process.
Types of System Calls
The following list categorizes system calls based on their functionalities:
1. Process Control
System calls play an essential role in controlling system processes. They enable you to:
- Create new processes or terminate existing ones.
- Load and execute programs within a process's space.
- Schedule processes and set execution attributes, such as priority.
- Wait for a process to complete or signal upon its completion.
2. File Management
System calls support a wide array of file operations, such as:
- Reading from or writing to files.
- Opening and closing files.
- Deleting or modifying file attributes.
- Moving or renaming files.
3. Device Management
System calls can be used to facilitate device management by:
- Requesting device access and releasing it after use.
- Setting device attributes or parameters.
- Reading from or writing to devices.
- Mapping logical device names to physical devices.
4. Information Maintenance
This type of system call enables processes to:
- Retrieve or modify various system attributes.
- Set the system date and time.
- Query system performance metrics.
The communication call type facilitates:
- Sending or receiving messages between processes.
- Synchronizing actions between user processes.
- Establishing shared memory regions for inter-process communication.
- Networking via sockets.
6. Security and Access Control
System calls contribute to security and access control by:
- Determining which processes or users get access to specific resources and who can read, write, and execute resources.
- Facilitating user authentication procedures.
Examples of System Calls
The table below lists common Unix and Windows system calls and their descriptions.
Note: System call behavior, parameters, and return values might differ depending on the OS and version. Consult the OS's manual or documentation for detailed information.
|UNIX SYSTEM CALLS||DESCRIPTION||WINDOWS API CALLS||DESCRIPTION|
|Create a new process.||Create a new process.|
|Terminate the current process.||Terminate the current process.|
|Make a process wait until its child processes terminate.||Wait for a process or thread to terminate.|
|Execute a new program in a process.||Execute a new program in a new process.|
|Get the unique process ID.||Get the unique process ID.|
|Open a file (or device).||Open or create a file or device.|
|Close an open file (or device).||Close an open object handle.|
|Read from a file (or device).||Read data from a file or input device.|
|Write to a file (or device).||Write data to a file or output device.|
|Change the read/write location in a file.||Set the position of the file pointer.|
|Delete a file.||Delete an existing file.|
|Rename a file.||Move or rename a file.|
|Create a new directory.||Create a new directory.|
|Remove a directory.||Remove an existing directory.|
|Change the current directory.||Change the current directory.|
|Get file status.||Get extended file attributes.|
|Get status of an open file.||Get file information using a file handle.|
|Create a link to a file.||Create a hard link to an existing file.|
|Get the status of an open file.||Create a symbolic link.|
|Increase/decrease the program's data space.||Reserve, commit, or free a region of memory.|
|Map files or devices into memory.||Map a file into the application's address space.|
|Get the current time.||Get the current system time.|
|Get the status of an open file.||Set a timer object.|
|Set an alarm clock for the delivery of a signal.||Get the username or ID.|
|Get the group ID.||Get the group information of a security token.|
|Create a new socket.||Create a new socket.|
|Bind a socket to a network address.||Bind a socket to a network address.|
|Bind a socket to a network address.||Listen for connections on a socket.|
|Accept a new connection on a socket.||Accept a new connection on a socket.|
|Initiate a connection on a socket.||Initiate a connection on a socket.|
|Send and receive data on a socket.||Send and receive data on a socket.|
|Security and Access Control|
|Change the permissions/mode of a file.||Change the file attributes or security info.|
|Change the owner and group of a file.||Set the security information.|
What Are the Rules for Passing Parameters to the System Call?
When a user-space program invokes a system call, it typically needs to pass additional parameters to specify the request. System performance depends on how efficiently these parameters are passed between user and kernel space.
The method for passing parameters depends on the system architecture, but some general rules apply:
- Limited Number of Parameters. System calls are often designed to accept a limited number of parameters. This rule is intended to streamline the interface and compel users to utilize data structures or memory blocks.
- Leveraging CPU Registers. CPU registers are the fastest accessible memory locations. The number of CPU registers is limited, which restricts the number of call parameters that can be passed. Use CPU registers when passing a small number of system call parameters.
- Using Pointers for Data Aggregation. Instead of passing many parameters or large data sizes, use pointer variables to point to memory blocks or structures (containing all the parameters). The kernel uses the pointer to access this memory block and retrieve the parameters.
- Data Integrity and Security Checks. The kernel must validate any pointers passed from user space. It checks that these pointers only target areas the user program can access. It also double-checks all data coming from user programs before using it.
- Stack-based Parameter Handling. Some systems push parameters onto a stack and allow the kernel to remove them for processing. This method is less common than using CPU registers and pointers as it is more challenging to implement and manage.
- Data Isolation Through Copying. The kernel often copies data from user space to kernel space (and vice versa) to protect the system from erroneous or harmful data. Data passed between user space and kernel space should not be shared directly.
- Return Values and Error Handling. The system call returns a value, typically a simple success/error code. In case of an error, always seek out additional information about the error. Error responses are often stored in specific locations, like the errno variable in Linux.
Note: The rules and methods above vary based on the architecture (x86, ARM, MIPS, etc.) and the specifics of the operating system. Always refer to the OS documentation or source code for precise information.
This article explained how system calls work and why they are essential for ensuring seamless operations, security, and managing hardware and software resources.
Continue exploring system calls by reading about the role of the select() system call in Linux, which is essential for handling multiple I/O operations simultaneously.