How does a system call work [duplicate]
Asked Answered
H

1

24

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 ?

Healing answered 4/6, 2014 at 11:29 Comment(4)
blog.csdn.net/harhy/article/details/11766721Flugelhorn
Tending to close this as being a duplicate to: https://mcmap.net/q/377620/-how-do-system-calls-work/694576Sherl
Another potenial duplicate: https://mcmap.net/q/324918/-how-is-the-system-call-in-linux-implemented/694576Sherl
@Sherl i tried initially bt didnt get any question. thanks this will really help me to understand better.Healing
V
48

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.

Verditer answered 4/6, 2014 at 11:37 Comment(2)
this link also helpsHealing
This is a very good and accurate explanation. I would only add that the CPU instructions for an interrupt are "int 0x80" and "syscall". The second one being more modern and faster. Also the reason that the CPU knows where to jump (that new address) is because the OS previously had set it up (told the CPU beforehand). Also after the interrupt instruction, the CPU switches mode from User Mode to Kernel Mode because in User Mode the CPU will not allow you to touch hardware directly.Impercipient

© 2022 - 2024 — McMap. All rights reserved.