Re: library function vs system calls



sophia.agnes@xxxxxxxxx wrote:

Dear all,

I was going through peter van der linden's book expert C programming
there i found the following:-

Library call
------------

*call to a routine in a library
*linked with user program
*executes in user address space
*counts as a part of user time
*has the lower head overhead of procedure call.

system call
-----------
* call to the kernel for a service
* is an entry point to the OS
* executes in the kernel address space
* has high overhead switch to kernel and back

then the author says that , remember many routines in the C library do
their work by making system calls and system() call is
actually a library call.

then how valid is the above explanation on the differences between
library call versus system call ?

why system() call is treated as a library call ?

No. He simply means that many library calls like printf() end up calling
one or more system level routines to do their job. This is because
modern operating systems forbid user level programs from interfacing
hardware directly. Usually they must use the API provided by the OS to
do this, as well as many other things.

For example a printf() call might do a lot work like scanning the format
string and converting the corresponding arguments and building the
final output string without a single call to the system, but finally,
it is unable to actually write to a system device like the display or a
file on the hard disk because the OS forbids such interactions with the
hardware to stop programs from running wild on a multitasking system.
Therefore to output the final string it will typically call a system
level routine like write() on UNIX or WriteFile() under Windows.

Many library functions like those in string.h and many in math.h do all
their work without calling system but functions that need to do I/O or
change system settings or communicate with other processes need to go
through the system.

Under some modern architectures, the "system call" has been optimised to
be much faster than what it was before. Also some operating systems run
under a single privilege level only, and so, under such systems
a "system call" may be no more slower than an ordinary function call.

These details are all very much dependent on the exact configuration of
your hardware and system level software. Please ask in a operating
system group like <news:alt.os.development> for more details.

For a Standard C program, a "system call" does not exist. A POSIX
program can use the "syscall()" macro do call system entry points. Also
note that POSIX calls like write() and read() are most often
implemented as ordinary library calls that, under UNIX systems,
actually call a identically named system call. Thus the POSIX read()
function may itself call the 'read' system call to do it's work.
Because POSIX was standardised based on the then existing UNIX
interface, many of it's routines are simply wrappers to correspondingly
named UNIX system calls.

<http://en.wikipedia.org/wiki/System_call>
<http://www.unix.org/single_unix_specification/>
<http://www.developertutorials.com/tutorials/linux/linux-command-line-utility-050430/page8.html>

.



Relevant Pages

  • Re: Getting the file name from a FILE *
    ... string -- which might perhaps include the filename openned with, ... but as there can be considerable differences in parameter ... passing between non-varargs routines and varargs routines, ... I expect that that recompile / relink step would not be considered ...
    (comp.lang.c)
  • Re: Trying to execute something stored in variable
    ... Public Function ExecuteMethods(ByVal prmMethodName As String, ... Dim objMethodInfo As MethodInfo ... Public Sub SubRoutine1() ... Please note that I cannot take full credit for these routines. ...
    (microsoft.public.dotnet.languages.vb)
  • Writing an Adventure game with HLA
    ... For an "adventure" style game, ... These two routines lexically scan13 a string and break it up into ... The create procedure is used to initialize a lookup table. ...
    (alt.lang.asm)
  • Re: Trying to execute something stored in variable
    ... Public Function ExecuteMethods(ByVal prmMethodName As String, ... Dim objMethodInfo As MethodInfo ... Public Sub SubRoutine1() ... Please note that I cannot take full credit for these routines. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Safer and Better C
    ... > which causes memory limit to be inadequate for the desired string ... Unfortunately the world is full of macho programmers who seem to ... providing routines that can be controlled is not such an avoidance. ... fiber to be a Red Sox fan" ...
    (comp.lang.c)