System call vs Function call
Solution 1
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.
Solution 2
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.
Solution 3
Actually, the system call is not related to function call. The only common of these two mechanism is that they both provides services to the caller.
-
From view of thread execution to see system call:
A system call is function for application mode program to request services provided by underline OS. The system call will bring the running thread from user mode into kernel mode, execute the system call handler function, then return back to user mode.
-
Syscall Parameters:
The parameter of a system call is (syscall number, params...). The meaning and format of params depends on syscall number.
-
From view of syscall library provided to userland program:
The user mode program usually calls glibc's library to call system call. For example, the open() function in glibc:
- put system call number SYS_OPEN in eax register
- request system call by calling a software interrupt or sys_enter instruction
Solution 4
System call actually calls out to an API executed by the kernel space. With all the associated costs this assumes (see Wiki, or this link for details)
A function call is a call to a piece of code in user space.
However, please note that a function call MIGHT be to a function which in the process of its execution does system calls - "fopen" being one of such examples. So while the call to fopen itself is a call to a function, doesn't mean that the system call will not happen to handle the actual IO.
Solution 5
If you're using Linux you can monitor system calls performed by an application via strace:
strace /path/to/app
Its output might give you a good insight on what's going on within libc, and which functions are actually system calls.
Ankur
Working in Card Processing Industry in C and UNIX/LINUX
Updated on February 06, 2021Comments
-
Ankur over 3 years
What is the difference between a system call and a function call? Is fopen() a system call or a function call?
-
Adam Rosenfield about 14 yearsIt wraps
open
on *nix systems, but it wraps different system calls on other OSs (such asCreateFile
on Windows). -
Tom about 14 yearsYes, I have something agains C or C++ running on windows, so I always fail to consider it. (completely dogmatic, I know)
-
Hassan Syed about 14 yearsThat is ridiculous considering windows has as much C/C++ (considering professional CAD/office software and video games), if not more, software on it. Visual Studio isn't that bad either for C/C++.
-
Tom about 14 years@Hassan I know, i dont pretend to justify it, I just say I dont like it.
-
el.pescado - нет войне about 14 yearsIt's worth noting that on UNIX open() is a system call, whereas on Windows open() is a library function wrapping some native kernel interface, much like fopen().
-
Tomas Kubes over 8 yearsMoreover Isn't the system call mapping virtual memory to physical memory?
-
Utku about 8 years
This mode switching is the reason that system calls are slower to execute than an equivalent application-level function.
This does not mean thatfopen()
is faster than performing the same operation using the relavant system call right? Because iffopen()
is already performing system calls to do its job, then using the relavant system call must be, in the worst case, at the same speed withfopen()
right? -
Keyur Padalia about 8 yearsRight.
fopen()
might be ever so slightly slower than doing the system call yourself, but by usingfopen()
you gain portability, readability and maintainability. -
adityah almost 7 yearsIsn't caching and buffering the main reason why a function call is faster. I am really a beginner with limited knowledge. What are your thoughts about this ?
-
Keyur Padalia almost 7 years@adityah Yes, in case of calls like
fwrite
, buffering can make the C library calls faster than using system calls directly. In case offopen
this is not applicable though. -
Yann TM over 6 yearsThanks for sharing that link to the course on *Nix calls, really nice overview.