A program cannot use the hardware directly. It asks the operating system through system calls. System programs, in turn, use system calls to give users useful tools.
System Calls
A system call is the programming interface to the services of the operating system. When a program needs a service, such as reading a file, it executes a system call, which causes a trap (software interrupt). The CPU switches to kernel mode, the OS identifies the call by its number, performs the service, and returns the result to the program in user mode. Programs usually call the system through an API (application programming interface), such as the POSIX API in UNIX and Linux, or the Windows API. The C library function printf, for example, uses the write system call internally.
Definition: System call. A request made by a program to the operating system kernel for a service that the program cannot perform itself, causing a switch from user mode to kernel mode.
Passing Parameters
The parameters of a system call can be passed in three ways: in registers, which is fastest; in a block or table in memory, whose address is passed in a register; or by pushing them onto the stack. Linux uses registers for up to six parameters.
Types of System Calls
Process control: create, terminate, load, execute, wait, as in fork(), exec(), exit(), wait(). File management: create, open, read, write, close, delete, as in open(), read(), write(), close(). Device management: request, release, read and write devices, as in ioctl(). Information maintenance: get or set the time, date and system data, as in getpid(), time(). Communication: create connections and send or receive messages, as in pipe(), socket(), shmget(). Protection: set permissions, as in chmod(), chown().
System Programs
System programs, or system utilities, provide a convenient environment for program development and execution. They are not part of the kernel, but are supplied with the OS. They include file management tools (copy, move, list, delete), status information (date, disk space, process lists), file modification (text editors), programming language support (compilers, assemblers, debuggers), program loading and execution (loaders, linkers), communication (email, remote login, browsers), and background services (daemons). Most users see the OS mainly through system programs.
Table 1.4: Types of system calls
| Type | Purpose | UNIX examples |
|---|---|---|
| Process control | Create and end processes | fork, exec, exit, wait |
| File management | Use files | open, read, write, close |
| Device management | Use devices | ioctl, read, write |
| Information | Get or set system data | getpid, time |
| Communication | Exchange messages | pipe, socket, shmget |
| Protection | Control access | chmod, chown |
Example 1.4: Creating a process with fork
Problem: Explain what the program below prints when fork() is called once.
Solution:
- fork() creates a child process that is a copy of the parent.
- In the child, fork() returns 0; in the parent it returns the child's ID.
- The parent waits for the child with wait(), so the child's line is printed first.
Result: Both processes run, and the output shows one line from the child and one from the parent.
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
int main(void) {
pid_t pid = fork();
if (pid == 0) {
printf("Child: fork() returned 0\n");
} else {
wait(NULL);
printf("Parent: fork() returned the child's ID\n");
}
return 0;
}
Output
Child: fork() returned 0
Parent: fork() returned the child's ID
Common mistakes
- Thinking that library functions such as printf are system calls. They use system calls.
- Forgetting the mode switch that every system call involves.
- Confusing system programs with the kernel.
Key takeaways
- A system call is a program's request for an OS service.
- It traps to kernel mode and returns to user mode.
- The types are process, file, device, information, communication and protection.
- System programs are utilities supplied with the OS.
Practice questions
- What is a system call?
- Name three ways of passing parameters to a system call.
- Give two examples of process control system calls.
Answers to the practice questions
- A request by a program to the kernel for a service, which switches the CPU to kernel mode.
- In registers, in a memory block whose address is in a register, or on the stack.
- fork() and exec() (also exit(), wait()).
Sources
- Official BCA syllabus Operating Systems, course code 25BCA402DS03, Unit I
