C standard library and system calls

Is there a way to find out what functions from the standard C library system calls perform? Some of them, such as open , close and malloc , are obvious, but is there any list you can look at? How, for example, does strcpy make a system call or any of the functions from time.h ?

+4
source share
2 answers

I assume that you are asking in terms of performance, since in many cases the execution time is completely dependent on system calls.

Now, although it depends on the implementation (it even depends on the implementation, regardless of whether "system calls" exist), a good way to reason and make an educated guess is to use a "system call" as meaning "user-kernel-to-user" since this is really a system call.

Consider the strcpy example. It copies a string from one array (in your process space) to another array (also in your memory space). None of this makes any data other than these lines, so it would be pretty pathological to introduce kernel space as part of this operation.

On the other hand, consider the time function from time.h It returns the current system time. This is a shared system resource, so you can expect it to need to enter kernel space to read the current time kernel record. And traditionally, you would be right. However, modern versions of Linux, at least on some arches, display the kernel memory page in each user process as read-only - a page containing certain information that only the kernel can update, for example, system time, but all processes can read freely from user space without leakage of any sensitive data. Therefore, this question is more ambiguous.

Finally, you have operations, such as open or unlink , which must include switching from user to kernel to user, since they deal with shared resources (file system) on which permissions must be respected.

If you need a more empirical approach to answering your question, you can simply write a short program to call each function you are interested in and run it under strace .

+7
source

A system call is made when your program needs to interact with the kernel. It would be extremely inefficient for strcpy to make a system call, since this requires interupt; your code stops working, there is a context switch, the CPU runtime changes (to have more privileges), the interupt handler will work, and your code will continue. It is assumed that the OS will not decide to start another process after you mix it up or even execute your code at all (for example, while waiting to read).

You can largely answer the question yourself by asking yourself: can I write this function? (Maybe like in: is it physically possible?) You can write your own strcpy, but not open or read it. You can write your own fopen, but it would have to be open.

Wikipedia has a good list of categories for system calls . This will give you an idea of ​​when a system call is required.

This shows how to call interupt in an assembly (this is the only place you can reach the code that does this).

+1
source

Source: https://habr.com/ru/post/1395334/


All Articles