[Linux Internals] System Calls
System calls provide transition from ‘user space’ to ‘kernel space’; These are not just a routine calls.
In newer systems, kernel code for system calls do exist inside the address space of the process itself ( top of the address space stack). process cant access them in general, but only via special instructions i.e system calls. It uses stack space once its invoked and it executes within process context like any other instruction. This avoids Context Switch , which means we don't need to swap out memory tables of the current process and we can leave as it is !
Another advantage is, it allows system calls to be naturally interrupted /suspended/resumed etc. i.e when we suspend a process it does not matter whether its running a kernel code or user space code at that time. Otherwise it would have been tricky for the operating system to suspend the system calls.
- System call to read the file may block the process. Why they block?
Generally reading from hard drive for a file is slower than CPU. So there is nothing much to do for a process until data is ready. So system call tells the operating system to block the process during the file read and operating system will unblock once its done.
- System calls can be used for all below :
(1) Processes: fork(), exit(), exec()
(2) Files : open(), read(), write(), close()
(3) Networking Sockets : pipe(), shmget()
(4) Signals : signal()
(5) Inter Process Communication
(8) I/O Devices : ioctl(), read(), write()