Reputation: 5360
In Advanced Programing in the UNIX Environment 2nd Edition, chapter 01, section 11, there is a sentence:
For example, the printf function may use the write system call to output a string, but the strcpy (copy a string) and atoi (convert ASCII to integer) functions don't involve the kernel at all.
What I don't understand is that, why it is possible that there is no involvement of kernel at all. Aren't all routines require kernel support?
Also, I may figure out that atoi
only requires CPU computation. But as strcpy
need the memory operation, why it doesn't involve kernel support either?
Generally, how can I recognize that a function doesn't involve the kernel?
Upvotes: 0
Views: 578
Reputation: 171471
What I don't understand is that, why it is possible that there is no involvement of kernel at all. Aren't all routines require kernel support?
At the very lowest level, yes, because a process wouldn't even exist without the kernel starting it, but that doesn't mean the kernel is involved in every operation. You wouldn't exist without your parents, but that doesn't mean you can't get dressed "without the involvement of your parents".
If you write a simple function in C that simply dereferences its argument, that is a routine, and it doesn't require support from the kernel:
int func(int* p) { return *p; }
Some library functions, such as atoi
and strcpy
are like this, they don't invoke any system calls to make use of services provided by the kernel.
Also, I may figure out that
atoi
only requires CPU computation. But asstrcpy
need the memory operation, why it doesn't involve kernel support either?
The kernel isn't used to copy bytes from one place to another within the same address space. The virtual memory subsystem might be involved at a very low level, to map virtual addresses to physical addresses, but that's true for almost every operation, even (if it's not in a register) reading or setting the value of an int
!
Anyway, atoi
has to read memory, why do you not consider that to "need the memory operation" too?
Generally, how can I recognize that a function doesn't involve the kernel?
You can't in general, without reading the source of the function. However, if a function can be implemented in pure C, without interacting with hardware or services provided by the kernel (such as forking new processes, or communicating with other processes) then it's likely it doesn't use system calls. Some operating systems provide ways to find out which system calls a process uses, e.g. the strace
utility, which will tell you whenever a process invokes a system call. You can also trace the process or intercept system calls by interposition, but that is quite complicated and out of scope for this answer.
Upvotes: 3
Reputation: 4332
It seems to mean that the function doesn't lead to system calls by saying 'not involve kernel", from the context of that sentence.
Upvotes: 5