Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between system call and library call?

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

like image 706
drdot Avatar asked Apr 23 '15 07:04

drdot


People also ask

Is write a system call or library call?

System calls are functions that transfer control from the user process to the operating system kernel. Functions such as read() and write() are system calls.

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.

Which library function is known as system call?

The functions which are a part of standard C library are known as Library functions. For example the standard string manipulation functions like strcmp(), strlen() etc are all library functions. The functions which change the execution mode of the program from user mode to kernel mode are known as system calls.

What is the difference between a system call and a kernel call?

The big different is what the system calls are for - let's take the file system as an example. In a monolithic kernel most services are implemented inside the kernel, including the file system. So to open a file, do operations on a file, a system call is required.


2 Answers

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

like image 150
Rahul Tripathi Avatar answered Oct 14 '22 08:10

Rahul Tripathi


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.

like image 38
user3344003 Avatar answered Oct 14 '22 06:10

user3344003