Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System call vs Function call

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

like image 303
Ankur Avatar asked Apr 19 '10 15:04

Ankur


People also ask

Is system call a function?

A system call is a request from computer software to an operating system's kernel. The Application Program Interface (API) connects the operating system's functions to user programs. It acts as a link between the operating system and a process, allowing user-level programs to request operating system services.

What is difference between system call and system program?

System programs are executable files while system calls are C routines which interact with operating system features and can be compiled into system programs.

How is system call different from subroutine?

System call is a call to a subroutine built in to the system, while a function call is a call to a subroutine within the program. Unlike function calls, system calls are used when a program needs to perform some task, which it does not have privilege for.


2 Answers

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.

like image 122
Thomas Avatar answered Sep 20 '22 12:09

Thomas


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.

like image 21
Tom Avatar answered Sep 20 '22 12:09

Tom