System call
Системный вызов
A program uses this mechanism to ask the operating system kernel to do something on its behalf, such as open a file, read from a network socket, or start a process.
Also known as: syscall
What crosses the boundary?
Most application code runs in user mode. It can manipulate its own data, but it cannot directly take control of a disk or access a file just because it knows the filename. The kernel manages those resources and enforces access rules. A system call, or syscall, is a controlled entry point into the kernel. On Linux, for example, openat asks the kernel to open a file. If it succeeds, the kernel returns a file descriptor: a number the program can pass to read or close.
What happens during a call?
Applications usually go through a library or language runtime rather than issuing a CPU instruction themselves. On Linux, a wrapper places the syscall number and arguments where the platform's calling convention expects them. On x86-64, the syscall instruction transfers control to a kernel entry point and changes the CPU's privilege level. The kernel dispatches the request, checks arguments and permissions as appropriate, and normally returns a result to user mode. A C library wrapper such as openat() reports failure as -1 and sets errno to indicate why the operation failed.
Example: diagnose a file-open failure
When a service says it cannot load its config, the error message alone may not tell you which path it tried. On Linux, strace shows the calls the program made and their results. Try it with a small command:
strace -e trace=openat,read,close cat ./config.txt
Look for the line containing ./config.txt; there may also be calls that open shared libraries. If openat returns ENOENT, a path component could not be found. If it returns EACCES, access was denied, which can happen because of the file's permissions or because the process cannot traverse a parent directory. This gives you a concrete next step instead of guessing from the application's log message.
Common interview questions
Is every library call a syscall? No. printf formats text in user space and may leave the result in a buffer. Several calls to printf can lead to just one write syscall when the buffer is flushed. Conversely, a single library operation can make several syscalls.
Does a syscall always cause a context switch? Entering kernel mode is a privilege change, not necessarily a switch to another thread. If the requested bytes are ready, read can return without the calling thread blocking. If a blocking socket read has to wait for data, the scheduler can run another thread in the meantime.
Does reading a file always hit the disk? No. An ordinary read still enters the kernel, but the kernel may already have the requested file data in its page cache.