System Calls in Operating system?

System Calls in Operating system?

System Calls in Operating Systems

Definition: System calls are the programming interface between the operating system (OS) and the application programs. They provide the means by which a program can request services from the OS kernel, such as performing hardware operations, creating processes, and handling I/O.

Key Concepts

  1. User Mode vs. Kernel Mode:

    • User Mode: The mode in which user applications run. It has restricted access to hardware and critical system data.

    • Kernel Mode: The mode in which the OS kernel runs. It has unrestricted access to hardware and all memory.

  2. Transition: A system call involves transitioning from user mode to kernel mode to perform the requested operation and then returning to user mode.

Types of System Calls

  1. Process Control:

    • Examples: fork(), exec(), exit(), wait()

    • Functions: Creating and terminating processes, loading and executing programs, waiting for a process to finish execution.

  2. File Management:

    • Examples: open(), read(), write(), close()

    • Functions: Creating, deleting, reading, writing, opening, and closing files.

  3. Device Management:

    • Examples: ioctl(), read(), write()

    • Functions: Requesting and releasing devices, reading from and writing to devices, setting device parameters.

  4. Information Maintenance:

    • Examples: getpid(), gettimeofday(), alarm(), sleep()

    • Functions: Getting and setting system data, including time, process IDs, and system configuration.

  5. Communication:

    • Examples: pipe(), shmget(), shmat(), semget(), semop()

    • Functions: Creating and managing communication channels like pipes, shared memory, and semaphores.

Steps in Making a System Call

  1. Invocation: The application calls a library function that wraps the system call.

  2. Trap to Kernel: The library function triggers a software interrupt or trap, switching the CPU to kernel mode.

  3. Kernel Execution: The OS kernel handles the request, performing the necessary operation.

  4. Return: The kernel returns the result to the application, and the CPU switches back to user mode.

Examples of System Calls

Unix/Linux System Calls

  • fork(): Creates a new process by duplicating the calling process.

  • exec(): Replaces the current process image with a new process image.

  • open(): Opens a file, returning a file descriptor.

  • read(): Reads data from a file descriptor.

  • write(): Writes data to a file descriptor.

  • close(): Closes a file descriptor.

  • wait(): Waits for a process to change state.

Windows System Calls

  • CreateProcess(): Creates 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.

  • CloseHandle(): Closes an open object handle.

  • WaitForSingleObject(): Waits until the specified object is in the signaled state or the time-out interval elapses.

Benefits of System Calls

  1. Security: By transitioning to kernel mode, the OS can enforce access control and ensure that only authorized operations are performed.

  2. Abstraction: System calls provide a consistent interface to the hardware, abstracting the complexities from application programs.

  3. Resource Management: They enable the OS to manage system resources efficiently and fairly among processes.

Example Walkthrough: read() System Call in Unix/Linux

  1. Application: Calls the read() function from the standard library.

  2. Library Function: Wraps the actual system call and sets up the parameters.

  3. Trap: Issues a software interrupt to switch to kernel mode.

  4. Kernel: Handles the request by locating the file descriptor, checking permissions, and reading data into a buffer.

  5. Return: Copies the data to the application's buffer, switches back to user mode, and returns the number of bytes read.

Error Handling

System calls typically return a value indicating success or failure. In Unix/Linux, a return value of -1 usually indicates an error, and the global variable errno is set to indicate the specific error.