18

Can someone explain the differences for these two in linux? Please go as deep as possible into each step the operating system takes.

1
  • 3
    @MartinJames, this is a curious question. I wish the professor gave this homework when I was in the OS class.
    – drdot
    Apr 27, 2015 at 16:14

4 Answers 4

18

Low level kernel calls which are handled by the kernel are the system calls.

The man page says:

2 System calls (functions provided by the kernel)

3 Library calls (functions within program libraries)

A pictorial image can make it clear:

enter image description here

and

enter image description here

5
  • this is too high level. I am looking for each small steps.
    – drdot
    Apr 23, 2015 at 7:44
  • @dannycrane:- You can check the wiki: en.wikipedia.org/wiki/System_call to go in more detail. Apr 23, 2015 at 7:45
  • 1
    @dannycrane:- I can give you a small example like fopen() which is a Library call, and open() which is a System call. Now lets say you call fopen() from header file in your C program. Now the programming environment will call system call open() from kernel and perform its file opening task. Again after executing, control flow return to user mode. You can check FOPEN(3) and OPEN(2) of man page Apr 23, 2015 at 7:48
  • The library call diagram is wonderful. Do you have something similar for syscall?
    – drdot
    Apr 23, 2015 at 8:01
  • 2
    @dannycrane, In second diagram, 4th step is system call.And for detail info you can read this nice article... thegeekstuff.com/2012/07/system-calls-library-functions Apr 23, 2015 at 9:41
12

Looking at the comment, let me try this. A system service is a procedure that executes with elevated privilege (usually kernel mode). Everything else is a library call.

The underlying hardware provides a gate for user applications to enter kernel mode. The operating system guards this gate for basic system security.

Doing a little bit of simplification here --- a common method used by processors is for the process to explicitly trigger an exception (Intel has an alternate SYSCALL method).

A system will have a set of interrupt/exception vectors (pointer to handler procedures) for responding to exception or interrupts (e.g. page fault, divide by zero). The system will define some set of vectors (usually low numbered ones) for hardware exceptions and interrupts. However, they usually leave slots for the operating system to use.

An instruction something like:

   INT #12

Will explicitly trigger an exception and invoke the 12th procedure in the vector. A system might allow you to simulate a divide by zero exception doing this

Let us assume that the operating system uses vector 123 for system services.

   INT #123

would call a system service. A system might reserve a separate vector for each system service or it could use one and dispatch.

So you would do something like this:

  MOVL  #23, R0
  INT  #123

The value 23 in Register 0 tells the interrupt handler to invoke system service #23.

So you can see that this all takes assembly language. What every operating system does is create wrapper that can be called like functions from high level languages.

This is then the sequence of what happens:

  1. A user calls the named wrapper with normal parameters. The wrapper sets up the registers and stack for the system service.

  2. The wrapper triggers the exception that dispatches to the system service.

  3. The system service then has to check ALL the parameters. This is one of the reasons system services have high overhead. Exceptions in kernel mode cause blue screens of death. If the system service needs to write to a buffer supplied by the user, it needs to ensure that every byte it writes to is writeable memory.

  4. The system service does whatever it needs to do.

  5. The system service executes a hardware instruction to return from an exception or interrupt. That returns to user mode and back to the wrapper function.

  6. The wrapper may unpack parameters returned in registers.

  7. The wrapper returns to the caller.

0
6

System calls and library calls are similar in that they are provided to application by the environment. The difference between the two is that system calls are implemented in kernel, whereas library calls are implemented in user space. To make a system call, application has to execute special hardware- and system- dependent instruction (usually an interrupt or special SYSENTER call) instruction, which triggers context switch and forwards control to he kernel. Library calls are, on the other hand, no different from regular function calls (the only difference is that code does not reside in main executable but in shared libraries).

2
  • Simply Library calls are somewhat (not in all case) are wrapper to system calls. Apr 23, 2015 at 9:30
  • 2
    Not at all, eg. strcmp is a library call implemented fully in user-space, not calling any kernel code. On the other hand, system library provides convienience wrapper functions for most system calls. Apr 23, 2015 at 9:35
0

Library Functions is an ordinary function that is placed in the collection of functions called the library .

System calls is the request to the OS for service.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.