04 System Calls Event Driven Kernel
04 System Calls Event Driven Kernel
Abhijit A. M.
[email protected]
(C) Abhijit A.M.
Available under Creative Commons Attribution-ShareAlike License V3.0+
https://linuxhint.com/list_of_linux_syscalls/
int main() {
int a = 2;
}
printf("hi\n"); Code schematic
-----
C Library -------user-kernel-mode-
----- boundary----
int printf("void *a, ...) {
...
//OS code
write(1, a, ...); int sys_write(int fd, char *, int
} len) {
int write(int fd, char *, int len) {
int ret;
figure out location on disk
... where to do the write and
mov $5, %eax,
carry out the operation,
mov ... %ebx,
mov ..., %ecx etc.
int $0x80
}
__asm__("movl %eax, -4(%ebp)");
# -4ebp is ret
return ret;
}
Two important system calls
Related to processes
Absolutely No!
Let’s understand
What kind of
Hardware, OS interplay
makes
Multitasking possible
Two types of CPU instructions
and two modes of CPU operation
CPU instructions can be divided into two types
Normal instructions
mov, jmp, add, etc.
Privileged instructions
Normally related to hardware devices
E.g.
IN, OUT # write to I/O memory locations
INTR # software interrupt, etc.
Two types of CPU instructions
and two modes of CPU operation
CPUs have a mode bit (can be 0 or 1)
The two values of the mode bit are called: User mode and
Kernel mode
If the bit is in user mode
Only the normal instructions can be executed by CPU
If the bit is in kernel mode
Both the normal and privileges instructions can be executed by CPU
If the CPU is “made” to execute privileged instruction when
the mode bit is in “User mode”
It results in a illegal instruction execution
Again running OS code!
Two types of CPU instructions
and two modes of CPU operation
The opearting system code runs in kernel mode.
How? Wait for that!
The application code runs in user mode
How? Wait !
So application code can not run privileged hardware
instructions
Transition from user mode to kernel mode and vice-
versa
Special instruction called “software interrupt” instructions
E.g. INT instruction on x86
Software interrupt instruction
E.g. INT on x86 processors
Does two things at the same time!
Changes mode from user mode to kernel mode in CPU
+ Jumps to a pre-defined location! Basically changes
PC to a pre-defined value.
Close to the way a hardware interrupt works. Isn’t it?
Why two things together?
What’s there are the pre-defined location?
Obviously, OS code. OS occupied these locations in Memory,
at Boot time.
Software interrupt instruction
What’s the use of these type of instructions?
An application code running INT 0x80 on x86 will now
cause
Change of mode
Jump into OS code
Effectively a request by application code to OS to do a
particular task!
E.g. read from keyboard or write to screen !
OS providing hardware services to applications !
A proper argument to INT 0x80 specified in a proper
register indicates different possible request
Software interrupt instruction
How does application code run INT
instruction?
C library functions like printf(), scanf() which do
I/O requests contain the INT instruction!
Control flow
Application code -> printf -> INT -> OS code -> back
to printf code -> back to application code
Example: C program
int main() {
int i, j, k;
k = 20;
scanf("%d", &i); // This jumps into OS and returns back
j = k + i;
printf("%d\n", j); // This jumps into OS and returns back
return 0;
}
Interrupt driven OS code
OS code is sitting in memory , and runs
intermittantly . When?
On a software or hardware interrupt or
exception!
Event/Interrupt driven OS!
Hardware interrupts and exceptions occur
asynchronously (un-predictedly) while software
interrupts are caused by application code
Interrupt driven OS code
Timer interrupt and multitasking OS
Setting timer register is a privileged instruction.
After setting a value in it, the timer keeps ticking down and on
becoing zero the timer interrupt is raised again (in hardware
automatically)
OS sets timer interrupt and “passes control” over to some
application code. Now only application code running on CPU !
When time allocated to process is over, the timer interrupt
occurs and control jumps back to OS (hardware interrupt
mechanism)
The OS code that runs here (the ISR) is called “scheduler”
OS can now schedule another program
What runs on the processor ?
4 possibilities.