What is the difference between a system call and a function call? Is fopen() a system call or a function call?
相关问题
- Multiple sockets for clients to connect to
- What is the best way to do a search in a large fil
- glDrawElements only draws half a quad
- Index of single bit in long integer (in C) [duplic
- Equivalent of std::pair in C
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.
fopen
is a function call, but it may sometimes be referred to as a system call because it is ultimately handled by the "system" (the OS).fopen
is built into the C runtime library.Just to complete the picture presented by the others,
fopen
is commonly implemented as a wrapper aroundopen
, which is also a user-accessible function.fopen
is, in a sense, higher-level thanopen
since theFILE*
structure it returns encapsulates stuff for the user. Some users useopen
directly for special needs. Therefore it wouldn't be right to callfopen
a "system call" in any way. Nor does it execute system calls directly, sinceopen
is also a function callable by the user.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:
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.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.