What is a System Call?
A system call is a mechanism used by programs to request services from the operating system (OS). In simpler terms, it is a way for a program to interact with the underlying system, such as accessing hardware resources or performing privileged operations.
VIDEO CREDIT :- GATE SAMESTER
A user program can interact with the operating system using a system call. A number of services are requested by the program, and the OS responds by launching a number of systems calls to fulfill the request. A system call can be written in high-level languages like C or Pascal or in assembly language. If a high-level language is used, the operating system may directly invoke system calls, which are predefined functions.
A system call is initiated by the program executing a specific instruction, which triggers a switch to kernel mode, allowing the program to request a service from the OS. The OS then handles the request, performs the necessary operations, and returns the result back to the program.
System calls are essential for the proper functioning of an operating system, as they provide a standardized way for programs to access system resources. Without system calls, each program would need to implement its methods for accessing hardware and system services, leading to inconsistent and error-prone behavior.
Services Provided by System Calls?
- Process Control:
- Creating and terminating processes.
- Managing process scheduling and synchronization.
- File Management:
- Creating, deleting, reading, and writing files.
- Managing file permissions and attributes.
- Device Management:
- Interfacing with hardware devices.
- Controlling device operations and handling input/output.
- Information Maintenance:
- Retrieving system information such as system time and process status.
- Managing system resources and memory.
- Communication:
- Facilitating communication between processes through messages or shared memory.
Features of System Calls?
- Abstraction:
- System calls provide a high-level interface for programs to interact with hardware and system resources, abstracting the complexity of the underlying operations.
- Controlled Access:
- They allow user programs to access system resources in a controlled manner, ensuring security and stability by preventing direct access to hardware.
- Kernel Mode Transition:
- System calls facilitate a transition from user mode to kernel mode, enabling the execution of privileged operations that are restricted to the operating system.
- Error Handling:
- System calls typically include mechanisms for error detection and handling, allowing programs to respond appropriately to issues such as resource unavailability.
- Standardization:
- They provide a standardized set of functions for performing common operations, ensuring consistency across different applications and systems.
- Performance:
- While system calls can introduce overhead due to mode switching, they are optimized for performance to minimize the impact on application execution.
How does System Call Work?
- Initiation:
- A system call is initiated by a user program when it needs to request a service from the operating system. This is typically done by executing a specific instruction or using a library function that wraps the system call.
- Mode Switching:
- When the system call is invoked, the CPU switches from user mode to kernel mode. This transition is essential because it allows the program to execute privileged instructions that are only accessible to the operating system.
- System Call Interface:
- The operating system provides a system call interface, which is a set of predefined functions that the user program can call. Each system call corresponds to a specific service, such as file manipulation, process control, or network communication.
- Parameter Passing:
- Parameters required by the system call are passed from the user program to the operating system. This can be done through registers, stack, or memory, depending on the architecture and calling convention.
- Execution in Kernel Mode:
- Once the parameters are received, the operating system executes the requested service in kernel mode. This may involve accessing hardware resources, managing memory, or performing other system-level operations.
- Return to User Mode:
- After the service has been executed, the operating system prepares the result (if any) and switches back to user mode. The control is returned to the user program, along with any output or error codes.
- Error Handling:
- If an error occurs during the execution of the system call, the operating system will return an error code to the user program. The program can then handle the error appropriately.
Windows System Calls
- CreateProcess: Used to create a new process and its primary thread.
- ReadFile: Reads data from a file or input/output (I/O) device.
- WriteFile: Writes data to a file or I/O device.
- CreateFile: Creates or opens a file or I/O device.
- CloseHandle: Closes an open object handle.
Unix/Linux System Calls
- fork: Creates a new process by duplicating the calling process.
- exec: Executes a program in the context of the calling process.
- open: Opens a file or device.
- read: Reads from a file descriptor.
- write: Writes to a file descriptor.
- close: Closes a file descriptor.
- exit: Terminates the calling process.
- wait: Waits for a child process to stop or terminate.
0 Comments