0

In C programming language, fopen(), open() I can use both for file operations.

is fopen() is a system call or not? if no why?

is open() is a system call or not? if yes why?

Mahendra M
  • 23
  • 3

3 Answers3

2

General Information

  • A system call is basically an interface of your application to the OS kernel.
  • System calls are usually wrapped by the wrapper function provided by the platform's standard library.

Sometimes the system call name and the wrapper function names are same, sometimes they are not.

Usually, the wrapper functions provided by the standard library provides some extra feature and/or sanity checking/ error checking for the underlying system call.

system calls are the bare minimum calls that is passed to the kernel, which executes in kernel-mode and returns the required result to the user-space application.

For each kernel versions, there are defined set of system calls available and are usually represented by a number associated with each call. Ready referecnce : arch/i386/kernel/entry.S file (look for sys_call_table)

OTOH, the library functions, provided by standard [or non-standard] libraries are simple APIs which are used to provide access to various functions , including system calls in a bit easy way for the developer. These are used to hide some complex parts, provide some additional checks, some error handling and finally calls the internal function, mostly underlying system calls.


Answer to your question

open() is a system call.

fopen() is a wrapper provided by the standard library [linux glibc, for example].

Check more details in syscalls manual page.

Sourav Ghosh
  • 127,934
  • 16
  • 167
  • 234
  • calling fopen() a wrapper of open() may not be appropriate. The main purpose of fopen() is implement the standard C lib function, not to wrap open(). A wrapper should expose as much function as the wrapped one. The real wrapper is mostly a assembly code like :ENTRY(read) movl $__NR_read, %eax syscall... ret END(read) – jw_ Dec 07 '19 at 02:19
0

open() is a system call. man 2 open gives you the details, and system calls are in section 2.

System calls are the lowest-level functionality of an application. They tend to be a no-frills API, providing the bare minimum to accomplish a task. A system call is a request from your application to the kernel to interact with something, like a filesystem, or a network socket.

The main distinguishing feature of a system call is that you leave the user-mode context where your application code is running, and transition to kernel-mode, where the kernel code executes to handle that request for you.

There is a lot of information available on the topic. I suggest you start with Google.


fopen() is a library call, provided by your libc. man 3 fopen gives you the details. Many of these library calls serve as wrappers to the system calls. fopen makes an open() call under the hood, but also sets up the FILE stream - an abstraction that makes your life as a developer easier.

For example, with the write system call, you pass it a pointer to some data, and a length - that's all. With the FILE streams, you can fwrite, fprintf, etc. These streams also provide buffering which improves the performance of your application. System calls incur a a fair amount of overhead, to make the user-kernel mode switch; this buffering basically combines multiple fwrite / fprintf calls to speed things up.

See also:

Community
  • 1
  • 1
Jonathon Reinhart
  • 116,671
  • 27
  • 221
  • 298
0

open() is a system call and fopen() is a library function.

For more details see the man page of open() and man page of fopen() and here for difference between the library and system call. The latter link says:

Well, the answer to this is the fact that fopen() is a library function which provides buffered I/O services for opening a file while open() is a system call that provides non-buffered I/O services. Though open() function is also available for applications to use but application should avoid using it directly.

glglgl
  • 81,640
  • 11
  • 130
  • 202
Karthikeyan.R.S
  • 3,921
  • 1
  • 17
  • 31