Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does a system call work [duplicate]

Tags:

How does system calls work ?
What are the operations happen during system call?
There are various system call like open , read, write, socket etc. I would like to know how do they work in general ?

like image 696
Rocoder Avatar asked Jun 04 '14 11:06

Rocoder


People also ask

What does dup system call do?

dup() and dup2() Linux system call. The dup() system call creates a copy of a file descriptor. It uses the lowest-numbered unused descriptor for the new descriptor. If the copy is successfully created, then the original and copy file descriptors may be used interchangeably.

How does a system call works?

When a user program invokes a system call, a system call instruction is executed, which causes the processor to begin executing the system call handler in the kernel protection domain. This system call handler performs the following actions: Sets the ut_error field in the uthread structure to 0.

Why do we need duplicate system call ?/?

dup() system call is used to duplicate a file descriptor. It creates a copy of the old file descriptor to a new file descriptor. The new file descriptor can be system-generated or a number of your choice depending upon either you use dup() or dup2().

What is the difference between dup and dup2?

The difference between dup and dup2 is that dup assigns the lowest available file descriptor number, while dup2 lets you choose the file descriptor number that will be assigned and atomically closes and replaces it if it's already taken.


1 Answers

In short, here's how a system call works:

  • First, the user application program sets up the arguments for the system call.
  • After the arguments are all set up, the program executes the "system call" instruction.
  • This instruction causes an exception: an event that causes the processor to jump to a new address and start executing the code there.

  • The instructions at the new address save your user program's state, figure out what system call you want, call the function in the kernel that implements that system call, restores your user program state, and returns control back to the user program.

A visual explanation of a user application invoking the open() system call:

enter image description here

It should be noted that the system call interface (it serves as the link to system calls made available by the operating system) invokes intended system call in OS kernel and returns status of the system call and any return values. The caller need know nothing about how the system call is implemented or what it does during execution.
Another example: A C program invoking printf() library call, which calls write() system call

enter image description here

For more detailed explanation read section 1.5.1 in CH-1 and Section 2.3 in CH-2 from Operating System Concepts.

like image 84
haccks Avatar answered Oct 21 '22 18:10

haccks