Advertisement

HexaGuard: Mastering the Art of Digital Shadows

System Calls in Operating System 🚀

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).
System Call is Invoked
  • The application calls a system function like open(), which triggers a system call.
Mode Switch to Kernel Space
  • The CPU switches from User Mode → Kernel Mode to execute the request.
Kernel Executes the Request
  • The kernel processes the system call, interacts with hardware, and gets the required data.
Mode Switch Back to User Space
  • 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.

CategoryDescriptionExample System Calls
Process ControlManage processesfork(), exec(), exit(), wait()
File ManagementHandle filesopen(), read(), write(), close(), chmod()
Device ManagementControl hardware devicesioctl(), read(), write(), fcntl()
Information MaintenanceGet/set system datagetpid(), setuid(), gettimeofday()
CommunicationInter-process communicationpipe(), 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:

c

pid_t pid = fork(); // Creates a new process if (pid == 0) { execl("/bin/ls", "ls", NULL); // Replaces process with "ls" } else { wait(NULL); // Parent waits for child to finish }

(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

int fd = open("file.txt", O_RDONLY); // Open file in read mode char buffer[100]; read(fd, buffer, sizeof(buffer)); // Read data close(fd); // Close file

(C) Device Management System Calls

  • ioctl(): Sends commands to devices.
  • read(): Reads from a device.
  • write(): Writes to a device.

📌 Example:

c

int fd = open("/dev/usb0", O_WRONLY); // Open a USB device write(fd, "data", 4); // Write data to USB close(fd);

(D) Communication System Calls

  • pipe(): Creates a communication channel between processes.
  • send(): Sends a message.
  • recv(): Receives a message.

📌 Example:

c

int fd[2]; pipe(fd); // Create pipe write(fd[1], "Hello", 5); // Write to pipe char buffer[10]; read(fd[0], buffer, 5); // Read from pipe

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.

Post a Comment

0 Comments