1. Home
  2. Docs
  3. Operating System
  4. Operating system Overview
  5. System Call and Handling of System Calls

System Call and Handling of 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.

System calls can be categorized into several types based on their functionality:

  1. 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.
  2. 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.
  3. Device Management: Request and release devices, read, and write device status.
    • ioctl(): Controls device-specific operations.
    • read(), write(): Interact with device data.
  4. Information Maintenance: Get and set system data, get process attributes.
    • getpid(): Gets the process ID.
    • gettimeofday(): Gets the current time.
    • sysinfo(): Retrieves system information.
  5. 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 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:

The process of handling a system call involves several steps:

  1. Trap to Kernel: The CPU traps (switches) to the kernel mode using a specific instruction that causes an interrupt or exception.
  2. System Call Dispatch: The kernel identifies the specific system call requested by examining a system call number passed by the user process.
  3. Kernel Routine Execution: The kernel executes the corresponding routine to handle the system call. This might involve accessing hardware, modifying kernel data structures, or performing other privileged operations.
  4. Return to User Space: Once the kernel has completed the requested operation, it prepares a return value and restores the CPU to user mode, passing control back to the user process.

Example: The ‘read‘ System Call

Let’s consider the read system call as an example:

  1. User-Level Call: A user program calls the ‘read‘ function in the standard library:
ssize_t bytesRead = read(fileDescriptor, buffer, count);
  1. Library Function: The ‘read‘ function in the C library sets up the necessary parameters and issues a ‘syscall‘ instruction to switch to kernel mode.
  2. Trap to Kernel: The ‘syscall‘ instruction triggers a software interrupt, causing the CPU to switch to kernel mode.
  3. System Call Handler: The kernel’s system call handler inspects the system call number (associated with ‘read‘) and dispatches control to the appropriate handler function.
  4. Execution: The kernel function for ‘read‘ performs the necessary operations to read data from the file into the buffer.
  5. Return to User Mode: The kernel function completes its work, sets up the return value (number of bytes read), and switches back to user mode.
  6. User-Level Execution: Control returns to the user process, which continues execution with the data now available in the buffer.

This entire sequence ensures that the user process can perform operations requiring kernel-level privileges in a controlled and safe manner.

How can we help?

Leave a Reply

Your email address will not be published. Required fields are marked *