System Calls
A system call is a mechanism that allows user-level processes to request services from the operating system’s kernel.
• System calls provide an essential interface between a process and the operating system.
• They enable processes to perform operations such as file manipulation, process control, communication, and more, by invoking functions provided by the kernel.
Types of system calls
System calls can be categorized into several types based on their functionality:
- Process Control: It is a system calls that manage the creation, execution, and termination of processes, as well as operations like waiting for a process to finish, and altering a process’s priority.
fork()
: Creates a new process.exec()
: Executes a new program in the current process.exit()
: Terminates a process.wait()
: Waits for a process to change state.
- File Management: It is a system calls that handle file operations, such as creating, opening, reading, writing, closing, and deleting files.
open()
: Opens a file.close()
: Closes a file.read()
: Reads data from a file.write()
: Writes data to a file.unlink()
: Deletes a file.
- Device Management: It is a system calls that allow programs to interact with hardware devices, such as requesting access, releasing devices, reading from or writing to devices, and controlling device settings.
ioctl()
: Controls device-specific operations.read()
,write()
: Interact with device data.
- Information Maintenance: It is a system calls that provide functionality for retrieving or updating system information, such as time, date, system status, process attributes, and configuration settings.
getpid()
: Gets the process ID.gettimeofday()
: Gets the current time.sysinfo()
: Retrieves system information.
- Communication: It is a system calls that facilitate inter-process communication (IPC), enabling data exchange between processes through mechanisms like pipes, shared memory, sockets, or message passing.
pipe()
: Creates a pipe for inter-process communication.shmget()
,shmat()
,shmdt()
: Manage shared memory.msgget()
,msgsnd()
,msgrcv()
: Manage message queues.socket()
,bind()
,listen()
,accept()
: Network communication.
Handling System Calls
Handling of system calls involves a series of steps that the operating system’s kernel undertakes to process requests from user-space applications for various services.
Here’s a detailed look at the steps involved:
- System Call Invocation
A user-space application requests a system service by invoking a system call, usually through a library function that interacts with the OS.
- Switch to kernel Mode
When a system call is invoked, The CPU switches from user mode to kernel mode to access protected resources safely. This switch is triggered by an interrupt or trap instruction.
- System Call Identification
The OS identifies the requested system call using a unique identifier passed by the application.
- Parameter Validation
The OS verifies the parameters passed with the system call to ensure they are valid and accessible, preventing errors and security issues.
- System Call Execution
The OS kernel executes the requested service, such as file access, memory allocation, or process control, as identified by the system call.
- Result Generation
After executing the call, the OS generates a result, which could be data, a status code, or a response to the user’s request.
- Switch back to User Mode
Once the system call completes, the CPU switches back from kernel mode to user mode, allowing the user application to continue.
- Error Handling
If an error occurs during the system call, the OS returns an error code or message, allowing the application to handle it appropriately.