tags: os operating-system computer architecture is_child_of: operating-systems
What are System calls?
System calls are the abstractions provided by operating system to access low level resources such as IO handling, read/write files, etc.
These are the way in which a user program can request service from the kernel of the operating system.
There are libraries provided which lets user program make use of system calls.
For example, in python we have os
package providing system calls to read/write files and some other features.
import os
fd = os.open('file.txt', os.O_RDONLY)
content = os.read(fd, 100)
print(content.decode())
There are library procedures which make use of system calls. We hardly use system calls directly. We use library procedures to deal with system calls.
Running a system call
When a system call is invoked, following things happens in a sequence.
- User program calls library procedure to make a system call.
- Variables are pushed on to the stack as the control is going to procedure call.
- Procedure sets a value at a specified register or location the operating system expects for some system call.
- Procedure executes a TRAP instruction which in turn executes the TRAP handler in the kernel mode.
- Once the TRAP handler done with its work, the control is returned back to procedure call and in return the control goes back to the user program.
Procedure call Vs Trap call
Both of these are instruction calls. However, they differ in following two ways.
- Execution of TRAP instruction changes mode from user to kernel.
- Procedure call instruction contains the address where to find the instruction. This address is dynamic. Whereas, TRAP instruction as a 8 bit location which points to a table in memory that has the location of TRAP handler to execute. And this is fixed by the system architecture.