1

The following link says that read is a syscall: What is the difference between read() and fread()?

Now, I am trying to understand what makes read a system call.

For example:
I use Nuttx OS and registered a device structure flash_dev (path '/dev/flash0') with open, close and ioctl methods. This is added as a inode in pesudo file system with semaphore support for mutual exclusion.

Now, from application I open ('/dev/flash0') and do read & ioctls.
Now, which part in the above process makes read a syscall?

Community
  • 1
  • 1
RRON
  • 1,037
  • 3
  • 12
  • 32

2 Answers2

2

The read() function is a thin wrapper around whatever instructions are necessary to call into the system, IOW, to make a system call. When you call read() (and fread() call it as well), the relevant kernel/driver code gets invoked and does whatever is necessary to read from a file.

Alexey Frunze
  • 61,140
  • 12
  • 83
  • 180
  • The important difference is that `fread` _may_ not involve a system call. If it can satisfy the request out of the user space buffers, no system call occurs. `read`, being a thin wrapper, always performs the system call, which can be expensive if you're doing a lot of small `read`s (for large `read`s, the system call overhead is swamped by I/O overhead and `fread` would have needed to perform the same system call anyway). – ShadowRanger Nov 10 '15 at 19:08
2

A system call is a call whose functionality lives almost entirely in the kernel rather than in user space. Traditionally, open(), read(), write(), etc, are in the kernel whereas fread(), fwrite(), etc, have code that runs in user space that calls into the kernel as needed.

For example, in Linux when you call read() the standard library your application linked against might do the following:

   mov eax, 3          ;3 -> read
   mov ebx, 2          ;file id
   mov ecx, buffer  
   mov edx, 5          ;5 bytes 
   int 80h

That's it - it simply takes the parameters you passed in and invokes the kernel via the int 80h (interrupt) instruction. As an application programmer, it's not usually important whether the call runs in user space, in the kernel, or both. It can be important for debugging or performance reasons, but for simple applications it really doesn't matter much.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
EricS
  • 9,650
  • 2
  • 38
  • 34