The book 'Understanding Linux Kernel' says that 'for something abstract such as math functions, there may be no reason to make system calls'. Can any one please explain how is a system call not required in math functions? Isn't the CPU involved in mathematical operations? Can anyone give an example of such a program?
I don't have that book to check, but I assuming its using the normal meaning of system calls, then a system call is a call into the kernel to perform some operation the hardware considers privileged, or is unaware of. This is used to enforce permissions, etc. on the system. So you need to make a system call to (among many other things):
- read from a file (the kernel must check that the permissions allow you to read from said file, and then the kernel carries out the actual instructions to the disk to read the file)
- signal a process (processes do not exist as far as the hardware is concerned, they are an abstraction provided by the kernel, etc.)
- obtain additional memory (the kernel must make sure you don't exceed the ulimit, make sure two processes don't claim the same RAM, etc.)
Math is not one of those things. Typically, it requires no intervention from the kernel.
A "system call" is a call to a kernel function. This is needed for functionality managed by the kernel, like accessing devices. For "normal" operation like adding numbers no help from the kernel is needed. Therefore calling a library which is only computing stuff, no call to kernel space is needed, too.
You can use
strace to show all the system calls of a given program.
Executing a process executes CPU instructions by definition. There are a few CPU instructions that can only be executed in a privileged mode, mostly instructions related to accessing hardware outside the CPU including the RAM or to modifying some configurations. The kernel is executed in privileged mode, ordinary processes are executed in unprivileged mode. The details of what “privileged mode” means depends on the CPU architecture.
Purely computational instructions such as integer operations and floating point operations do not require any privileged access, so they can be executed by ordinary processes.
When a process wants to perform an operation that requires privileges, it has to invoke some kernel code to do it. There is a special CPU instruction that simultaneously switches the CPU to privileged mode and branches to a predefined address in memory. In a unix-like operating system (and in most OSes in general), this action is called a system call.
Unix provides isolation between processes, so it sets up the processor (specifically, the MMU) in such a way that each process can only access its own memory, and not the memory of other processes or the kernel. (Almost) any action that could affect other processes or that involves access to hardware outside the main CPU goes through the kernel and hence requires a system call. This includes access to files and hardware devices, networking, inter-process communication, privilege and memory management, etc.
You can see the documentation for the system calls on your system in section 2 of the manual. You can see what system calls a process makes by launching it via
truss on Solaris and FreeBSD,
strace on Linux, or equivalents under other unix variants.