A system call is a programmatic way in which a computer program requests a service from the kernel of the operating system on which it is executed.
System Calls are,
- A way for programs to interact with the operating system.
- Provide the services of the operating system to the user programs.
- Only entry points into the kernel and are executed in kernel mode.
System Call
How do System Calls Work?
- A system call can be written in high-level languages like C or C++ or Pascal or in assembly language.
- A system call is initiated by the program executing a specific instruction, which triggers a switch to kernel mode, allowing the program to request a service from the OS. The OS then handles the request, performs the necessary operations and returns the result back to the program.
- Without system calls, each program would need to implement its methods for accessing hardware and system services, leading to inconsistent and error-prone behavior.
Types of System Calls
Services provided by an OS are typically related to any kind of operation that a user program can perform like creation, termination, forking, moving, communication, etc. Similar types of operations are grouped into one single system call category. System calls are classified into the following categories:
-(2).png)
Read more about - Different Types of System Calls in OS
Examples of a System Call in Windows and Unix
System calls for Windows and Unix come in many different forms. These are listed in the table below as follows:
| Process | Windows | Unix |
|---|
| Process Control | CreateProcess() ExitProcess() WaitForSingleObject() | Fork() Exit() Wait() |
|---|
| File manipulation | CreateFile() ReadFile() WriteFile() | Open() Read() Write() Close() |
|---|
| Device Management | SetConsoleMode() ReadConsole() WriteConsole() | Ioctl() Read() Write() |
|---|
| Information Maintenance | GetCurrentProcessID() SetTimer() Sleep() | Getpid() Alarm() Sleep() |
|---|
| Communication | CreatePipe() CreateFileMapping() MapViewOfFile() | Pipe() Shmget() Mmap() |
|---|
| Protection | SetFileSecurity() InitializeSecurityDescriptor() SetSecurityDescriptorgroup() | Chmod() Umask() Chown() |
|---|
- Open(): Accessing a file on a file system is possible with the open() system call. It gives the file resources it needs and a handle the process can use. A file can be opened by multiple processes simultaneously or just one process. Everything is based on the structure and file system.
- Read(): Data from a file on the file system is retrieved using it. In general, it accepts three arguments:
1. A description of a file.
2. A buffer for read data storage.
3. How many bytes should be read from the file
- Wait(): In some systems, a process might need to hold off until another process has finished running before continuing. When a parent process creates a child process, the execution of the parent process is halted until the child process is complete. The parent process is stopped using the wait() system call. The parent process regains control once the child process has finished running.
- Write(): Data from a user buffer is written using it to a device like a file. A program can produce data in one way by using this system call. generally, there are three arguments:
1. A description of a file.
2. A reference to the buffer where data is stored.
3. The amount of data that will be written from the buffer in bytes.
- Fork(): The fork() system call is used by processes to create copies of themselves. It is one of the methods used the most frequently in operating systems to create processes. When a parent process creates a child process, the parent process's execution is suspended until the child process is finished. The parent process regains control once the child process has finished running.
- Exit(): A system call called exit() is used to terminate a program. In environments with multiple threads, this call indicates that the thread execution is finished. After using the exit() system function, the operating system recovers the resources used by the process.
Methods to Pass Parameters to OS
If a system call occur, we have to pass parameter to the Kernel part of the Operating system. for example look at the given open() system call:
C
// function call example
#include <fcntl.h>
int open(const char *pathname, int flags, mode_t mode);
Here pathname, flags and mode_t are the parameters. So it is to be noted that :
- We can't pass the parameters directly like in an ordinary function call.
- In kernel mode there is a different way to perform a function call.
So we can't run it in the normal address space that the process had already created and hence we can't place the parameters in the top of the stack because it is not available to the kernel of the operating system for processing. so we have to adopt any other methods to pass the parameters to the kernel of the OS. We can done it through,
- Passing parameters in registers
- Address of the block is passed as a parameter in a register.
- Parameters are pushed into a stack.
1. Passing Parameters in Registers
- It is the simplest method among the three, here we directly pass the parameters to registers.
- But it will be limited when, number of parameters are greater than the number of registers.
- Here is the C program code:
C
// Passing parameters in registers.
#include <fcntl.h>
#include <stdio.h>
int main()
{
const char *pathname = "example.txt";
int flags = O_RDONLY;
mode_t mode = 0644;
int fd = open(pathname, flags, mode);
// in function call open(), we passed the parameters pathanme,flags,mode to the kernal directly
if (fd == -1)
{
perror("Error opening file");
return 1;
}
// File operations here...
close(fd);
return 0;
}
2. Address of The Block is Passed as Parameters
- It can be applied when the number of parameters are greater than the number of registers.
- Parameters are stored in blocks or table.
- The address of the block is passed to a register as a parameter.
- Most commonly used in Linux and Solaris.
- Here is the C program code:
C
// Address of the block is passed as parameters
#include <fcntl.h>
#include <stdio.h>
int main()
{
const char *pathname = "example.txt";
int flags = O_RDONLY;
mode_t mode = 0644;
int params[3];
// Block of data(parameters) in array
params[0] = (int)pathname;
params[1] = flags;
params[2] = mode;
int fd = syscall(SYS_open, params);
// system call
if (fd == -1)
{
perror("Error opening file");
return 1;
}
// File operations here...
close(fd);
return 0;
}
3. Parameters Are Pushed in a Stack
- In this method parameters can be pushed in stack using the program and popped out using the operating system
- So the Kernel can easily access the data by retrieving information from the top of the stack.
- Here is the C program code
C
// parameters are pushed into the stack
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
int main()
{
const char *pathname = "example.txt";
int flags = O_RDONLY;
mode_t mode = 0644;
int fd;
asm volatile("mov %1, %%rdi\n"
"mov %2, %%rsi\n"
"mov %3, %%rdx\n"
"mov $2, %%rax\n"
"syscall"
: "=a"(fd)
: "r"(pathname), "r"(flags), "r"(mode)
: "%rdi", "%rsi", "%rdx");
if (fd == -1)
{
perror("Error opening file");
return 1;
}
// File operations here...
close(fd);
return 0;
}
Explore
Basics
Process Scheduling
Process Synchronization
Deadlock
Multithreading
Memory Management
Kernel & System-Level Concepts
Disk Management
Important Links