System Call
System call is a way through which we access kernel mode. There are different types of system call to perform different tasks. When System Call is generated then CPU switch form user mode to kernel mode and performs that particular task defined by system call.
Explanation of System Call
As in above diagram, User Applications run in user mode, and operating system run in kernel mode. As OS control all the hardware’s so User cannot access hardware without kernel mode. To access kernel mode user application generate a system call because System call is a source to go to kernel mode.
CPU switch in two modes, while working
- User mode
- Kernel mode
Let’s explain User Mode vs Kernel Mode with examples
When system is Turn ON then CPU is in kernel mode, by default, because it loading OS into main memory and OS exist in Kernel mode. After log IN, CPU switch to user mode to run user applications.
1. Watching Movie in windows OS,
If we want To play movie then we have to double click on that particular video. As, that video exist in main memory and main memory is hardware.So, CPU cannot access the hardware in user mode. System have to request through system call to open the file in main memory. So, OPEN () system call is generated and CPU switch to Kernel mode. Now OS in kernel, open that particular file from main memory. After opening the file CPU switch back to user mode. Now we are watching the movie in user mode. When user want to close the file then CLOSE system call again generated.
2. Using MS WORD
To open MS WORD we have to double click on that particular file. As, that file exist in main memory and main memory is a hardware. So, User cannot access the hardware in user mode. User have to request through system call to open the file in main memory. So, OPEN () system call is generated and CPU switch to Kernel mode. Now OS is in kernel mode, it open that particular file from main memory. After opening the file, CPU switch back to user mode. Now we are writing some text in user mode. When user want to save the file then WRITE () system call is again generated. CPU switches to kernel mode to save the file in main memory.
3. Using C++
When we are coding in language translator then we are in user mode and when we use PRINTF () command to print something on monitor, then CPU switch to Kernel mode to print on Monitor because monitor is a hardware.
Hence it proved: CPU will shift to Kernel mode Each time when CPU wants to access hardware.
Note: When CPU switch to kernel mode from user mode then a Trap generated which change the mode bit from 1 to 0. When kernel execution is complete then trap again generated and change the mode bit from 0 to 1. Explanation given under with diagram,
Note: Windows and Linux OS are used in user and kernel modes.
Real Life example
- If we go for back to withdraw money. Then the staff of bank is kernel and our command in user mode is withdraw the money. This request is executed by bank staff to withdraw the money. So, without kernel, user can’t proceed.
- In a cricket match all players are in user mode when they go for any decisions to umpires then the Umpire is kernel mode and decision is system call
Now lets look at comparisons of Trap and Interrupt
Trap VS Interrupt
Trap | Interrupt |
it is generated by an instruction in the user program so they called software interrupts. | it is generated by hardware device so they are called hardware interrupt. |
Trap can arrive after the execution of any instruction | It can occur at the execution time of any instruction |
It raised from user program and indicates the OS to perform some functionality immediately. | A signal to processor emitted by hardware indicating an event that needs immediate attention. |
It transfers the Control to trap handler. | Control transfer to corresponding interrupt handler routine. |
For example, if we
need to write a program code to read data from one file, copy that data into
another file. The first information that the program requires is the name of
the two files, the input and output files.
In an interactive
system, this type of program execution requires some system calls by OS.
·
First call is to write
a prompting message on the screen
·
Second, to read from
the keyboard, the characters which define the two files.
As you can see in the
above-given diagram.
Step 1) The processes executed in the user mode
till the time a system call interrupts it.
Step 2) After that, the system call is executed in the
kernel-mode on a priority basis.
Step 3) Once system call execution is over,
control returns to the user mode.,
Step 4) The execution of user processes resumed in
Kernel-mode.
Why do you need System
Calls in OS?
Following are
situations which need system calls in OS:
- Reading and writing from files demand system calls.
- If a file system wants to create or delete files,
system calls are required.
- System calls are used for the creation and management
of new processes.
- Network connections need system calls for sending and
receiving packets.
- Access to hardware devices like scanner, printer, need
a system call.
Types of System calls
Here are the five
types of system calls used in OS:
- Process Control
- File Management
- Device Management
- Information Maintenance
- Communications
Rules for passing Parameters for System Call
Here are general common
rules for passing parameters to the System Call:
- Parameters
should be pushed on or popped off the stack by the operating system.
- Parameters
can be passed in registers.
- When there
are more parameters than registers, it should be stored in a block, and
the block address should be passed as a parameter to a register.
0 Comments