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: Create, terminate, and manage processes.
fork()
: Creates a new process.exec()
: Replaces the current process image with a new one.exit()
: Terminates a process.wait()
: Waits for a process to change state.
- File Management: Create, delete, read, write, and manipulate 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: Request and release devices, read, and write device status.
ioctl()
: Controls device-specific operations.read()
,write()
: Interact with device data.
- Information Maintenance: Get and set system data, get process attributes.
getpid()
: Gets the process ID.gettimeofday()
: Gets the current time.sysinfo()
: Retrieves system information.
- Communication: Create and manage communication channels.
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.