Abstract


Important

Control is passed back to the Kernel when a system call is made by the Process (进程). Kernel uses this opportunity to perform its Process Management. If the process hogs to the CPU and doesn’t make any system call, we have Preemptive Scheduling to handle this.

User program isn't communicating with syscall directly!

The System call is placed behind an Abstraction Barrier created with the Library Call. So user programs like OS System Program is triggering system call that requests privileged services from the kernel via the library call.

Best Practice

Program should always check the results of System Call to see if an error has occurred.

Better Security

Process (进程) in User Space to can only use computer hardware to complete its job with kernel’s security implementation. This prevents programs from doing malicious stuff.

How is a system call triggered?


  1. Step 1-3: Calling Process (进程) pushes the arguments for the parameters of the system call to its Stack Segment
  2. Step 4(where the actual Library Call is happening): An Instruction is triggered to trigger the corresponding Library Call, the same instruction is used to trigger other library calls
  3. Step 5 Library Call puts Syscall Interrupt Number in a place where Kernel expects it, such as a Register
  4. Step 6: Execute Trap Interrupt (陷入) to enter the Kernel Mode
  5. Step 7: The trap interrupt examines Syscall Interrupt Number, dispatch the correct Interrupt Handler via Interrupt Vector Table
  6. Step 8: The desired Interrupt Handler starts running
  7. Step 9: After Interrupt Handler finishes, control maybe returned to the User Space at the Instruction following the Trap Interrupt (陷入)

Control MAYBE returned to user-space

  • The System Call (系统调用) may block the caller (in this case Library Call), preventing it from continuing
  • For example, the system call for keyboard input. When system call tries to read but nothing has been typed yet, the caller has to be blocked
  1. Step 10: Then, library call returns, and we are back to the user program
  2. Step 11: To finish the job, the process needs remove the system call related data like the arguments we pushed to the stack segment from its stack segment by incrementing the Stack Pointer

Highly CPU dependent


Abstraction comes to rescue

We have Abstraction Barrier on top of these System Call (系统调用) in the form of Library Call that follows a standardised interface like POSIX by wrapping the Assembly Instruction of different Instruction Set Architecture (ISA). Specific Instruction of Instruction Set Architecture (ISA) is generated automatically during Compilation.

Unix-like systems use libc and Windows uses ntdll.dll.

Examples


Linux System Calls

Windows System Calls