OS Notes
Understanding system calls — the interface between user programs and the OS kernel, categories of system calls, how system calls work internally, and examples in Linux and Windows.
Introduction
When your program wants to read a file, create a process, or send data over the network, it cannot do these things directly — these operations require hardware access that only the kernel has. So your program asks the kernel to do it by making a system call. System calls are the interface between user programs and the operating system kernel.
Think of system calls like a restaurant menu. You (the user program) cannot go into the kitchen (hardware) and cook your own food. Instead, you place an order (system call) with the waiter (kernel), who goes to the kitchen and brings back your food. The menu defines exactly what services are available.
How a System Call Works
Example: read() System Call on Linux
// User program calls:
ssize_t bytes = read(fd, buffer, 100);
// What actually happens:
// 1. C library (glibc) sets up:
// - System call number (0 for read on x86-64) in RAX register
// - fd in RDI register
// - buffer pointer in RSI register
// - count (100) in RDX register
// 2. Executes SYSCALL instruction
// 3. CPU traps to kernel
// 4. Kernel's sys_read() function executes
// 5. Data is read from the file into buffer
// 6. Return value (bytes read) placed in RAX
// 7. Returns to user mode
// 8. glibc returns the value to the user programCategories of System Calls
1. Process Control
| Call | Purpose | Example |
|---|---|---|
| fork() | Create new process | Starting a new program |
| exec() | Replace process image | Running a different program |
| exit() | Terminate process | Program finished |
| wait() | Wait for child process | Parent waiting for child |
| kill() | Send signal to process | Terminating a process |
2. File Management
| Call | Purpose | Example |
|---|---|---|
| open() | Open a file | Opening a document |
| read() | Read from file | Loading file contents |
| write() | Write to file | Saving data |
| close() | Close file descriptor | Done with file |
| lseek() | Move file position | Jump to specific location |
3. Device Management
| Call | Purpose | Example |
|---|---|---|
| ioctl() | Device control | Setting terminal mode |
| read()/write() | Device I/O | Reading from keyboard |
| mmap() | Map device memory | GPU memory access |
4. Information Maintenance
| Call | Purpose | Example |
|---|---|---|
| getpid() | Get process ID | Identifying current process |
| time() | Get system time | Timestamps |
| gettimeofday() | Get precise time | Performance measurement |
5. Communication
| Call | Purpose | Example |
|---|---|---|
| pipe() | Create pipe | Shell pipelines |
| socket() | Create network endpoint | Network communication |
| send()/recv() | Network I/O | Sending/receiving data |
| shmget() | Shared memory | Fast IPC |
6. Protection
| Call | Purpose | Example |
|---|---|---|
| chmod() | Change permissions | Making file executable |
| chown() | Change ownership | Transferring file ownership |
| setuid() | Set user ID | Running as different user |
System Call vs Library Function
// Library function — runs entirely in user space
strlen("hello"); // No system call, just counts characters in memory
// System call — crosses into kernel
open("file.txt", O_RDONLY); // Requires kernel to access file system
// Library function that USES system calls internally
printf("Hello\n"); // Eventually calls write() system callThe C standard library provides wrapper functions that make system calls easier to use. printf() buffers data in user space and periodically calls write() to actually output it.
System Call Performance
System calls are expensive compared to regular function calls because they require:
- Saving user-mode registers
- Switching to kernel mode (privilege level change)
- Executing kernel code
- Switching back to user mode
- Restoring user registers
A regular function call takes ~1-5 nanoseconds. A system call takes ~100-1000 nanoseconds (50-200x more expensive). This is why buffered I/O (reducing the number of system calls) is important for performance.
Real-World Analogy
System calls are like calling customer service. You (user program) cannot fix the problem yourself (access hardware). You call the help desk (system call), get put on hold (mode switch), explain what you need (parameters), the agent handles it in the back office (kernel execution), and gives you the result (return value). Each call has overhead (waiting, explaining), so you batch your requests when possible (buffered I/O).
Key Takeaways
- System calls are the ONLY way user programs can request kernel services
- They involve a mode switch from user mode to kernel mode (expensive ~100-1000 ns)
- Six categories: process control, file management, device management, info, communication, protection
- The C library provides convenient wrappers around raw system calls
- Buffering reduces the number of expensive system calls for better performance
- Each OS has its own system call numbers and conventions (Linux: ~450 syscalls, Windows: ~2000)
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for System Calls.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Operating Systems topic.
Search Terms
operating-systems, operating systems, operating, systems, system, basics, calls, system calls
Related Operating Systems Topics