What is a System Call?
A System Call is a request made by a user-space program to the kernel to access hardware resources (CPU, memory, disk, network, etc.) or perform privileged operations (such as creating processes, reading/writing files, and managing devices).
🔹 Since user applications cannot directly access hardware, they use system calls as a gateway to interact with the operating system.
1. How System Calls Work?
User Process Initiates a Request- A user application wants to perform an operation (e.g., open a file).
- The application calls a system function like
open()
, which triggers a system call.
- The CPU switches from User Mode → Kernel Mode to execute the request.
- The kernel processes the system call, interacts with hardware, and gets the required data.
- The kernel sends the result back to the application and switches Kernel Mode → User Mode.
📌 Example:
If a program wants to read a file, it calls read()
. The OS kernel reads the file and returns the data.
2. Types of System Calls
System calls are categorized based on their functionality.
Category | Description | Example System Calls |
---|---|---|
Process Control | Manage processes | fork() , exec() , exit() , wait() |
File Management | Handle files | open() , read() , write() , close() , chmod() |
Device Management | Control hardware devices | ioctl() , read() , write() , fcntl() |
Information Maintenance | Get/set system data | getpid() , setuid() , gettimeofday() |
Communication | Inter-process communication | pipe() , shmget() , msgsnd() , recv() |
3. Common System Calls
(A) Process Control System Calls
fork()
: Creates a new child process.exec()
: Replaces the current process with a new one.exit()
: Terminates a process.wait()
: Makes a process wait for another to complete.
📌 Example:
(B) File Management System Calls
open()
: Opens a file.read()
: Reads data from a file.write()
: Writes data to a file.close()
: Closes a file.
📌 Example:
(C) Device Management System Calls
ioctl()
: Sends commands to devices.read()
: Reads from a device.write()
: Writes to a device.
📌 Example:
(D) Communication System Calls
pipe()
: Creates a communication channel between processes.send()
: Sends a message.recv()
: Receives a message.
📌 Example:
4. System Call Flow Example
📌 Example: How read()
System Call Works
1️⃣ User program calls read(fd, buffer, size)
.
2️⃣ CPU switches to Kernel Mode.
3️⃣ Kernel checks file permissions and reads the file.
4️⃣ Kernel copies data to User Space.
5️⃣ CPU switches back to User Mode and returns the data.
5. Why Are System Calls Important?
✅ Security: Prevents direct hardware access by user programs.
✅ Abstraction: Hides low-level details from developers.
✅ Portability: System calls make programs work across different OSs.
✅ Resource Management: Efficient handling of CPU, memory, and devices.
6. Conclusion
🔹 System Calls are the only way user applications can interact with the OS kernel.
🔹 They provide safe, controlled access to system resources like files, devices, and memory.
🔹 OS security, multitasking, and resource allocation depend on system calls.
0 Comments