System call vs Function call

2019-01-13 03:25发布

What is the difference between a system call and a function call? Is fopen() a system call or a function call?

10条回答
倾城 Initia
2楼-- · 2019-01-13 04:09

If you're using Linux you can monitor system calls performed by an application:

strace appname ...

Its output might give you a good insight on what's going on within libc, and which functions are actually system calls.

查看更多
相关推荐>>
3楼-- · 2019-01-13 04:17

A point of view to add to this discussion is that a function call generally in the most optimistic case has overhead of a a few 8-bit instructions (4-10 on average)in x86.

A system call has the following properties.

  1. It Executes far more instructions, it has to freeze a process instead of simply the stack state.
  2. The timing involved is mostly non-deterministic.
  3. It is often a scheduling spot, and the scheduler may choose to reschedule.

For these three primitive reasons (there are probably more), one should reduce the amount of system calls where possible -- e.g., networked system software keeps socket handles (and other application specific internal data structures used by a connection) around to assign to new connection, why bother the kernel ?

Remember that software is built like a upside down pyramid. System calls are at the base.

查看更多
在下西门庆
4楼-- · 2019-01-13 04:17

The question has excellent answers already, but I think I can add something (one segment from ostepthat isn't already in other answers

Sometimes system call and function call have the same signature, for example, open():

open()-system call

--- ~/Documents » man open(2)

OPEN(2)                 Linux Programmer's Manual           OPEN(2)

int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);
...

open()-function call

$ man open(3)

--- ~/Documents » 
OPEN(3P)                        POSIX Programmer's Manual          OPEN(3P)
...

int open(const char *path, int oflag, ...);

...

Quoting form OSTEP

You may wonder why a call to a system call, such as open() or read(), looks exactly like a typical procedure call in C; that is, if it looks just like a procedure call, how does the system know it’s a system call, and do all the right stuff? The simple reason: it is a procedure call, but hidden inside that procedure call is the famous trap instruction. More specifically, when you call open() (for example), you are executing a procedure call into the C library. Therein, whether for open() or any of the other system calls provided, the library uses an agreed-upon calling convention with the kernel to put the arguments to open in well-known locations(e.g., on the stack, or in specific registers), puts the system-call number into a well-known location as well (again, onto the stack or a register), and then executes the aforementioned trap instruction. The code in the library after the trap unpacks return values and returns control to the program that issued the system call. Thus, the parts of the C library that make system calls are hand-coded in assembly, as they need to carefully follow convention in order to process arguments and return values correctly, as well as execute the hardware-specific trap instruction. And now you know why you personally don’t have to write assembly code to trap into an OS; somebody has already written that assembly for you.

查看更多
Juvenile、少年°
5楼-- · 2019-01-13 04:17

System call is executed at kernet level and not in user spce because it requires some prievilege to access the hardware.

Therfore when programming in user space and making some ordinary function call like fopen in C language the libc generally wrap this function to specific code code where an interrupt is generated to switch from user space to kernel space , then in kernel space the required system call to perform the functionality of the function call at hardware level will be executed in kernel space .

查看更多
登录 后发表回答