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
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.
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
Process Control:
Examples:
fork()
,exec()
,exit()
,wait()
Functions: Creating and terminating processes, loading and executing programs, waiting for a process to finish execution.
File Management:
Examples:
open()
,read()
,write()
,close()
Functions: Creating, deleting, reading, writing, opening, and closing files.
Device Management:
Examples:
ioctl()
,read()
,write()
Functions: Requesting and releasing devices, reading from and writing to devices, setting device parameters.
Information Maintenance:
Examples:
getpid()
,gettimeofday()
,alarm()
,sleep()
Functions: Getting and setting system data, including time, process IDs, and system configuration.
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
Invocation: The application calls a library function that wraps the system call.
Trap to Kernel: The library function triggers a software interrupt or trap, switching the CPU to kernel mode.
Kernel Execution: The OS kernel handles the request, performing the necessary operation.
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
Security: By transitioning to kernel mode, the OS can enforce access control and ensure that only authorized operations are performed.
Abstraction: System calls provide a consistent interface to the hardware, abstracting the complexities from application programs.
Resource Management: They enable the OS to manage system resources efficiently and fairly among processes.
Example Walkthrough: read()
System Call in Unix/Linux
Application: Calls the
read()
function from the standard library.Library Function: Wraps the actual system call and sets up the parameters.
Trap: Issues a software interrupt to switch to kernel mode.
Kernel: Handles the request by locating the file descriptor, checking permissions, and reading data into a buffer.
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.