CodePark/ questions / I/O models, file descriptors & syscalls
START PRACTICINGPRACTICE
Backend question · OS, networking & Linux › I/O models, file descriptors & syscalls

Why does a program make a system call such as read() instead of talking to the disk or network card directly?

User-mode code can't access hardware or kernel memory, so it asks the kernel, running in privileged mode, to check permissions and do the work

Applications run in an unprivileged CPU mode (user mode). Only the kernel runs in privileged mode, and only it can touch devices and all of memory. A system call is a controlled way to enter the kernel. The kernel checks the request and its permissions, then does the work.

"System calls run inside the program's own address space without any mode switch" is wrong: a system call always switches the CPU into kernel mode. That switch has a cost. This is why a few large reads and writes are faster than many tiny ones.