Unix - System Calls in UNIX
1. What are System Calls in UNIX?
-
A system call is a programming interface provided by the kernel so that user programs can request services from the operating system.
-
Since applications can’t directly access hardware (for safety and abstraction), they make requests to the kernel through system calls.
-
Example: When you run
cat file.txt
, the program doesn’t read the disk directly — it calls system calls likeopen()
,read()
,write()
.
2. Role of System Calls
They allow user programs to:
-
Access files and directories.
-
Create, manage, and terminate processes.
-
Communicate with hardware devices (disks, printers, terminals).
-
Handle memory allocation.
-
Enable interprocess communication (IPC).
3. Categories of System Calls in UNIX
-
Process Control
-
fork()
→ create a new process -
exec()
→ execute a new program -
exit()
→ terminate a process -
wait()
→ wait for a child process to finish
-
-
File Management
-
open()
→ open a file -
read()
→ read data from a file -
write()
→ write data to a file -
close()
→ close a file -
stat()
→ get file information
-
-
Device Management
-
ioctl()
→ control device settings -
read()
,write()
→ also used for device files
-
-
Information Maintenance
-
getpid()
→ get process ID -
alarm()
→ set a timer -
sleep()
→ suspend process
-
-
Communication (IPC)
-
pipe()
→ create a pipe between processes -
shmget()
→ allocate shared memory -
msgget()
→ create message queue -
semop()
→ semaphore operations
-
4. Example of a System Call in C
C program that reads a file using system calls:
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
int main() {
int fd;
char buffer[100];
// open() system call
fd = open("file.txt", O_RDONLY);
if (fd < 0) {
perror("open");
return 1;
}
// read() system call
int n = read(fd, buffer, sizeof(buffer)-1);
if (n >= 0) {
buffer[n] = '\0';
printf("File contents: %s\n", buffer);
}
// close() system call
close(fd);
return 0;
}
Here, open()
, read()
, and close()
are system calls, not library functions.
5. How a System Call Works (Step by Step)
-
User program calls a C library function (e.g.,
fopen()
in stdio). -
That library function internally makes the system call (e.g.,
open()
). -
Control is passed from user mode → kernel mode using a software interrupt (trap).
-
The kernel executes the system call (accessing hardware if needed).
-
Kernel returns the result/status back to the user program.
6. Diagram – System Call Flow
+--------------------------+
| User Program |
| (e.g., cat file.txt) |
+--------------------------+
|
v
+--------------------------+
| C Library (glibc) |
| (e.g., fopen, printf) |
+--------------------------+
|
v
+--------------------------+
| System Call Interface |
| (open, read, write) |
+--------------------------+
|
v
+--------------------------+
| Kernel (in kernel mode)|
| - File system |
| - Process manager |
| - Device drivers |
+--------------------------+
|
v
+--------------------------+
| Hardware (Disk, CPU) |
+--------------------------+
Summary
-
System calls are the entry points into the kernel.
-
They provide controlled access to hardware and system resources.
-
Categories: process control, file management, device management, IPC, information maintenance.
-
Example:
open()
,read()
,write()
,fork()
,exec()
,wait()
.