0

Considering Linux environment, what is the difference between them?

How is a system call different from a normal function call?

Krishna
  • 484
  • 7
  • 22
  • Did you check out this question: http://stackoverflow.com/questions/3149175/what-is-the-difference-between-trap-and-interrupt/37558741#37558741 Maybe you'll find the answer there? – Jolta Mar 09 '17 at 12:05
  • One more thing, it looks like you've put two questions into this question. Perhaps you should post them separately. https://meta.stackexchange.com/questions/222735/can-i-ask-only-one-question-per-post – Jolta Mar 09 '17 at 12:06
  • The tag [tag:exception-handling] is generally for C++ type exceptions and not OS "exceptions/traps/faults", etc. It is common to call them exceptions; but the terminology often depends on who designed the CPU. Linux itself tries to be fairly CPU agnostic in the core sections and there are often configurations that will select different core mechanisms to handle exceptions depending on the CPU. For instance, the mm (memory management) layer is often tied to the 'page fault' handler. The fault can be accessing memory that is swapped or non-existent (bus error). – artless noise Mar 09 '17 at 14:34

1 Answers1

2

According to wikipedia, a TRAP is an exception. Exceptions are defined differently depending on who you talk to. In a generic form, interrupts are exceptions. Exceptions could be a page fault (code or data), an alignment, an undefined instruction, divide by zero, etc.

Generally, they are all very similar. They will switch context to the OS to handle the issue resulting in registers saves (a user-space to OS context switch) and a possible process context switch depending on the request or circumstance. When you transition to the OS, different MMU protection (the CPU view of memory) are in effect and a different stack is used. In most cases, the instruction that caused the fault is the one that was executing when the switch happens.

The interrupt is different in that any user-space instruction could be interrupted. For most others, there are only specific classes of instructions that may cause a fault. This has ramification for compilers and libraries that need to do things atomically (to the thread, process or to the system globally). More details really depend on the CPU in use.


Considering Linux environment, what is the difference between them?

This is almost unanswerable in a definite way. Linux versions, CPU versions and your definition of what these are would influence the answer. However, I think the above is a good conceptual guide.

How is a system call different from a normal function call?

A normal function call doesn't transition to 'kernel space'. Many access permissions change when entering kernel space. Usually this has some physical hard wiring into the CPU. However the Linux 'mm' and 'io' layers are most definitely different and code maybe required to make it so. It can also depend on what the 'system call' does. In some cases, Linux has been optimize so the system call isn't needed (from one version to the next). See for example the vdso man page. In other cases, the C libraries or other mechanism might avoid the system call; for instance DNS name caching, etc.

Community
  • 1
  • 1
artless noise
  • 21,212
  • 6
  • 68
  • 105
  • If you meant for the question to be x86 specific, then this is a duplicate of [Difference between trap and interrupt](http://stackoverflow.com/questions/3149175/what-is-the-difference-between-trap-and-interrupt/37558741#37558741) and if not, the questions are very similar so it is a good idea to look there. – artless noise Mar 09 '17 at 16:42