System Calls in OS (Operating System)
Overview
System call is the special function that is used by the process to request action from the operating system which cannot be carried out by normal function. System calls provide the interface between the process and the operating system.
What are System Calls in OS?
Professor: Alice can you tell me the two main functions of the operating system (OS)?
Alice: Resource allocation and Providing environment so that process can execute
Professor: Very good Alice, seems like you have your basics clear. So now let’s understand how the process interacts with the OS. What are system calls in OS, Different Types of System calls, and a few examples of system calls as well?
When the process is being run, if the process requires certain actions that need to be carried out by Operating System, the process has to go call the function which can interact with the kernel to complete the actions. This special type of function call is known as System Calls in OS. System calls provide the interface so that the process can communicate with the Operating System.
Note: Kernel is the core component of the Operating System that has complete control of the hardware.
Bob: But Professor, How are System Calls in OS made?
Professor: Let’s understand that in detail bob.
How Does the System Call Work?
Professor: Before an understanding of the working of system calls in OS let us understand the important concept of modes of operation.
In our computer system, we have two modes available.
- User Mode: In this mode, execution is done on behalf of the user.
- Monitor/Kernel-Mode: In this mode, execution is done on behalf of OS.
- So when the process is under execution process executes under user mode, but when the process requires some OS resources to complete the task, the process makes a system call.
- System calls in OS are executed in kernel mode on a priority basis.
- On completion of the system call, the control is passed to process in user mode.
- The process continues the execution in user mode.
Alice: Professor, for any type of access, do we have a specific type of system call?
Professor: Yes Alice, we have different types of system calls, which we will see in the next section.
Types of System Calls in OS
There are mainly 5 types of system calls available:
- Process Control
- File Management
- Device Management
- Information Maintenance
- Communication
- Process Control: It handles the system calls for process creation, deletion, etc. Examples of process control system calls are: Load, Execute, Abort, and Wait for Signal events for process.
- File Management: File manipulation events like Creating, Deleting, Reading Writing etc are being classified under file management system calls.
- Device Management: Device Management system calls are being used to request the device, release the device, and logically attach and detach the device.
- Information Maintenance: This type of system call is used to maintain the information about the system like time and date.
- Communications: In order to have interprocess communications like send or receive the message, create or delete the communication connections, to transfer status information etc. communication system calls are used.
Professor: I know this is a bit theoretical, so let’s see some real-world system calls.
Example of System Calls in Windows and Unix
Types of System Calls | Windows | Linux |
---|---|---|
CreateProcess() | fork() | |
Process Control | ExitProcess() | exit() |
WaitForSingleObject() | wait() | |
CreateFile() | open() | |
ReadFile() | read() | |
File Management | WriteFile() | write() |
CloseHandle() | close() | |
SetConsoleMode() | ioctl() | |
Device Management | ReadConsole() | read() |
WriteConsole() | write() | |
GetCurrentProcessID() | getpid() | |
Information Maintenance | SetTimer() | alarm() |
Sleep() | sleep() | |
CreatePipe() | pipe() | |
Communication | CreateFileMapping() | shmget() |
MapViewOfFile() | mmap() |
Professor: As we can’t see each and every system call in detail, let us take read() system call of file management and understand how it works.
- As we can see in the diagram, initially, the process pushes the parameter onto the stack.
- After that, the read routine from the library is called, which puts the system call code (unique code for each system call is assigned) into the register.
- Trap instruction is used which causes the transfer of control from user mode to kernel mode.
- Kernel reads the system call code present in the register and finds out that the system call is read, so the system call handler executes the read system call.
- After the execution of the read system call, the transfer is given back to the user mode.
- The library routine (Library routine is nothing but the line of codes that forms the library and the functions that can be called by the process so that we don't need to write the function every time. e.g... max function) in user mode returns the output to the process which called it.
- On receiving the output, the process continues to execute the next instruction.
Note: Trap is the kind of interrupt that is synchronous to the process for executing the functionality.
Bob: ohh! Never thought that simply reading our computer carries out so many tasks.
Professor: Yes Bob, but do you know what parameters we can pass to the system call?
Bob: No professor, and let me guess! Is this the next topic that we will be studying?
Professor: Smart Bob!
Bob: Giggle
Rules for Passing Parameters in System Call
- The floating-point parameters can’t be passed as a parameter in the system call.
- Only a limited number of arguments can be passed in the system call.
- If there are more arguments, then they should be stored in the block of memory and the address of that memory block is stored in the register.
- Parameters can be pushed and popped from the stack only by the operating system.
Professor: Let’s see some important system calls of our computer system.
Important System Calls Used in OS
- wait(): There are scenarios where the current process needs to wait for another process to complete the task. So to wait in this scenario, wait() system call is used.
- fork(): This system call creates the copy of the process that has called it. The one which has called fork is called the parent process and the newly created copy is called the child process.
- exec(): This system call is called when the running process wants to execute another executable file. The process id remains the same while the other resources used by the process are replaced by the newly created process.
- kill(): Sometimes while working, we require to terminate a certain process. So kill system call is called which sends the termination signal to the process.
- exit(): When we are actually required to terminate the program, an exit system call is used. The resources that are occupied by the process are released after invoking the exit system call.
Alice: Professor, so kill() system call won’t necessarily terminate the process, right?
Professor: Correct Alice, good you understood that. Most students get confused with that system call. Let’s recall what we learned.
Take your first step towards mastering operating systems. Enroll in our Operating System free course and develop a comprehensive understanding of OS.
Conclusion
- System call is the interface through which the process communicates with the system call.
- Computer system operates in two modes: User Mode and Kernel Mode
- Process executes in user mode, and when a system call is made, the mode is switched to kernel mode. Once the system call execution is completed, the control is passed back to the process in user mode.
- System calls in OS are made by sending a trap signal to the kernel, which reads the system call code from the register and executes the system call.
- Major type of sytem calls are Process Control, File Management, Device Management, Information maintenance and Communicaiton.
- Rules for parameter passing while making a system call are it should not be a floating number, a limited number of arguments should be passed and if arguments are more, they should be stored in a memory block and the address of that memory block should be passed, and the push, pop operations from the stack will be made only by the operating system.
- wait(), fork(), exec(), kill() and exit() are few important system calls of our computer system.