-3

I have been given this question:

Is open a function or a system call? (select all valid answers)

a) it’s a function provided by the kernel

b) it’s a function

c) it’s a library call

d) it’s a system call

e) it’s a kernel routine

I have selected options a) and d) but the answer is still wrong. Any insight, please.

ad absurdum
  • 19,498
  • 5
  • 37
  • 60
Joe
  • 11
  • 1
  • Type `man open` at a linux command prompt (or on google) and find it yourself. Hint: it's on man section #2 so take your conclusions. – DDS Aug 07 '23 at 09:51
  • Also take a look here: https://stackoverflow.com/questions/2668747/system-call-vs-function-call – DDS Aug 07 '23 at 09:58
  • a) false. the kernel function is sys_open. – stark Aug 07 '23 at 10:12
  • Why does it matter if `open()` is a "function" or a "system call"? if someone stridently insists that, "`open()` is a system call!" you need to ask them, "Why isn't `fopen()` also a "system call" then?" Make sure you have a stopwatch to time how long they have their mouth open, speechless. Both `open()` and `fopen()` are **functions** that will wind up making the `openat()` system call - note that's **not** the `open()` system call - `openat()` has been the POSIX standard for almost two decades now. The "function" vs "system call" distinction is artificial, useless, and irrelevant. – Andrew Henle Aug 07 '23 at 11:40
  • open is a variable. open() is a function. – Chenmunka Aug 07 '23 at 15:18
  • @Chenmunka -- a peculiar objection. In C `open()` may be a function _call_ if `open` takes no arguments, and `open` may be a function designator. But you seem to be commenting on a typographical convention (not part of the C language) used by some, but certainly not by everyone, to indicate that an identifier refers to a function. – ad absurdum Aug 07 '23 at 17:05
  • This was helpful! – Joe Aug 08 '23 at 08:48

2 Answers2

1

Just a little bit of non-conformism here.

Calling open on any Unix-like does end in a system call using exactly the passed parameter. For that reason it is said to be a system call.

But with some nifty-picking, we can say that it can be a tiny function wrapping the actual system call.

The man page for syscall is explicit about that point:

syscall() is a small library function that invokes the system call...

But in the context of the question (yes/no type question), this understanding is not the expected answer, even if it is perfectly correct...

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
1

The answer is not so simple as the question. It is open to interpretation.

open is a function [in glibc], but it is also the name of the corresponding syscall.

This seems like an interview related question. Or, some certification test question. Or, a test question from some programming course.

But, IMO, it's poorly worded without more (such) context, and the answer depends upon some interpretation of how sophisticated an answer is required.

The TL;DR is probably: B, C, and D.

But, A and E are also possible in some contexts ... ;-)

So, we need the context: The mayonaise you like is the mayonaise you grew up with ... :-)


b) it’s a function

Yes, it's a function that is linked from glibc


c) it’s a library call

Yes, because it is a function in glibc

However, if the definition of "library call" is only a function in glibc that does not invoke a syscall, then the answer is No.


a) it’s a function provided by the kernel

For many syscalls, (e.g. open, close, read, write) the kernel does not provide a function to userspace, so the answer is No.

However, for some kernel syscalls, the kernel does inject a function into userspace via the VDSO mechanism. See: https://man7.org/linux/man-pages/man7/vdso.7.html and https://en.wikipedia.org/wiki/VDSO

An example of this is clock_gettime. Under a simple implementation, the function invokes a syscall. But, for speed, the kernel injects code into the app, and the library function knows this, and just invokes the injected code which has special access to some portions of kernel memory that are mapped (R/O) into userspace.

So, the answer is: It depends


d) it’s a system call

In most cases, the TL;DR answer is Yes, it's a system call.

But, the library function is considered to be a [thin] "wrapper" function around the kernel's syscall.

Here, the underlying implementation would be an internal version of:

syscall(SYS_open,...)

So, the "syscall" is SYS_open

So, the answer could be: No, open is just a [glibc] library function [that invokes the SYS_open syscall].


e) it’s a kernel routine

The answer is No.

But, because when the kernel receives an int 0x80, syscall, or sysenter, it dispatches to a given handler function in the kernel.

The convention for a given syscall (e.g.) whatever is sys_whatever. So, the kernel [internally] calls sys_open, which probably calls do_open.

So, under a loose interpretation, the kernel does have a function for this. So, the answer could be Yes


So, the question was probably posed for the "simple" interpretation:

B, C, D

(and sometimes A)

Craig Estey
  • 30,627
  • 4
  • 24
  • 48