A system call is a function used by the user program to request the operating system’s services. System call creates an interface that helps user programmers to communicate with the operating system to request its services.
System calls are always executed in the kernel mode of the operating system. In this context, we will be discussing the system call, and how to invoke them. We will even discuss its working, types and how to pass parameters to such calls.
Content: System Call in Operating System
- What is a System Call?
- How System Calls are Invoked?
- How Does System Call Work?
- Types of System Calls
- Example of How System Calls are Used?
- How to Pass Parameters to System Call?
What is a System Call?
A system call is a function or a method. The user programs use these functions to invoke the operating system’s services.
While designing the application program, the programmer has to include desired system calls. This will help the user program get the operating system’s services.
Thus, the system call creates an interface between the user program and the operating system.
How System Calls are Invoked?
Each operating system has its own set of system calls. And to invoke these system calls it has defined set functions. We refer to this set of functions as an application programming interface i.e., API.
Thus, for creating an application program. The programmer uses the functions provided by API. And these functions in the user program invoke the actual system calls to get the services of the OS.
For example, if a user program wants to create a new process in Windows OS. Then the API function provided by Win32 API to create a new function is CreateProcess(). This CreateProcess() function invokes the system call NTCreateProcess() of Windows OS.
API also specify what parameters must be passed with each function. And what return value will the function provide.
The most common types of API are:
- Win 32 API of Windows operating system
- POSIX API of POSIX-based operating system
- Java API of Java-based operating system
The different operating systems may have different names for the same system call.
How Does System Call Work?
We have learned about the system call. And we also know how to invoke them. Now when invoked how does it works?
- The user program starts executing in the user mode. And it continues till the instruction with API function occurs.
- As soon as the processor identifies an API function. It invokes the associated system call. And then the control of the process goes to the kernel mode.
- As soon as the system call’s execution gets complete the control of the process returns to the user mode.
Types of System Calls
We can classify system calls on the basis of services they invoke.
System calls used to control a process provides the following services.
- End the process normally, to abort the process forcefully.
- Load a process to the main memory, and execute the process.
- Create a new process, and terminate the current process.
- Wait for a process to finish its execution. Wait till a certain event occurs, signal when the event has occurred.
- Allocate memory to a process, if the process terminates then free the memory.
This kind of system call provides the following services:
- Create and delete files.
- Open and close the file.
- Read from a file, write to a particular file.
- To get the attribute of a file and to set the attribute of a file.
A system call that manages the I/O devices provides the following services:
- A process while executing may require devices. Such as access to I/O devices, files system, main memory etc.
- So, it can request a device and after the task is over it can release the device.
- Once the process gets access to a requested device. It can perform read, write or reposition operations.
- To get the attribute of a certain device or set the attribute of a certain device.
- The call can be made to attach a device to the executing processor to detach it.
This system calls transfers the information between the user program and OS. So, the services provided by this kind of system call are:
- Get the date or time of the system. Set the date or time of the system.
- Get the information about the system. Set the system information.
- Get the attributes of a particular process of the operating system. Or of a certain file of the system or of the attached devices.
- Set the attributes of a particular process of the operating system. Or of a certain file of the system or of the attached devices.
This kind of system call helps in connecting the system to a network. Services provided by these system calls are:
- Create a new connection to transfer the data. Delete a connection after the transmission gets complete.
- Send a message on a particular connection. Receive a message from a particular connection.
- Attach a particular remote device to the network. Detach a particular remote device from the network.
Example of How System Calls are Used?
To understand the system call let us overview a scenario. There are two files say the source file and the destination file. Now the programmer writes a program that reads the source file’s content. And then copy the content to the destination file.
To perform this simple task the operating system has to execute a series of system calls. The sequence of system calls is executed to copy the content of the source file to the destination file.
Requirement – The name of the two files.
For Interactive System
- Call 1 – Prompt a message on the screen to provide the name of the source and the destination file.
- Call 2 – Read the characters types using the keyboard and identify the name of the two files.
For Icon-based or Mouse-based System
- Call – Pop up a window that displays a menu of files. The user can then select the source file.
- Call – Pop up a window displaying the file menu to select the destination file.
Now the processor has the names of both files.
- Call – Open the source file.
- Call – Open or create the destination file.
If an error occurs, Such as the input file doesn’t exist or the file is protected.
- Call – Print an error message on the screen.
- Call – Abort the program.
If an input file exists. But while creating the destination file the system identifies that system already has a file with the same name then.
- Call – abort the program.
- Call – Delete the existing file and create a new destination file.
As the program has both the files. It will now enter a loop.
- Call – Read from the source file.
- Call – Write to the destination file.
The possible error that might occur is hardware failure while reading, the failure in a write operation. Such as no more disk space or the printer is out of paper etc. If no error occurs in the read and writes operation then:
- Call – Close both the files.
- Call – Prompt a message. The message must display the successful execution of the program.
- Call – Terminate the program.
As you can notice even in performing such a simple task the system executes a number of system calls. Thus the operating system executes thousands of system call frequently.
How to Pass Parameters to System Call?
The three general approaches for passing parameters to system calls are:
- The parameters can be passed to the system call in registers.
- If the number of parameters is more and they are unable to accommodate in the registers. The system can store them on the block in memory. And the address of this block is passed as a parameter.
- The parameters are pushed onto the system stack and popped up when required.
The last two methods are more convenient. As they do not restrict the number and the length of the parameters.
So, this is all about the system call that helps the user programmers to invoke the services of the operating system. We have seen how to invoke them. How does it work? We have also seen the types of system calls and also how to pass to a parameters system call.
I love this makes me understand OS the more, but please explain more on the example of writing and reading a file in system call .