What is the difference between a system call and a function call? Is fopen() a system call or a function call?
A system call is a request from computer software to an operating system's kernel. The Application Program Interface (API) connects the operating system's functions to user programs. It acts as a link between the operating system and a process, allowing user-level programs to request operating system services.
System programs are executable files while system calls are C routines which interact with operating system features and can be compiled into system programs.
System call is a call to a subroutine built in to the system, while a function call is a call to a subroutine within the program. Unlike function calls, system calls are used when a program needs to perform some task, which it does not have privilege for.
A system call is a call into kernel code, typically performed by executing an interrupt. The interrupt causes the kernel to take over and perform the requested action, then hands control back to the application. This mode switching is the reason that system calls are slower to execute than an equivalent application-level function.
fopen
is a function from the C library that, internally, performs one or more system calls. Generally, as a C programmer, you rarely need to use system calls because the C library wraps them for you.
fopen is a function call.
A system call interacts with the underlying OS, which manages resources. Its orders of magnitud more expensive than a function call, because many steps have to be taken to preserve the state of the process that made the syscall.
On *nix systems, fopen wraps open, which makes the system call (open is the C - wrapper for the syscall). The same happens with fread /read, fwrite / write , etc..
Here there's a nice description of the tasks executed by a unix syscall.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With