Differences between library functions and system calls in Linux C

Source: Internet
Author: User

Library functions are high-level and fully run in the user spaceProgramProvides a more convenient interface for calling the system that actually completes the actual transaction in the background.System calls run in kernel mode and are provided by the kernel itself.Standard C library functionsPrintf ()It can be seen as a general output statement, but what it actually does is to convert the data into a formatted string and call the system callWrite ()Output these strings.

Do you want to have a look?Printf ()What system calls are actually used? This is easy to compile the followingCode.

# Include <stdio. h>
Int main (void)
{
Printf ("hello ");
Return 0;
}

compile with the command gcc-wall-O hello. C . use the strace Hello command to trace the executable file . Are you surprised? Each line corresponds to a system call. Strace is a very useful program that tells you which system calls the program uses and the parameters and return values of these system calls. This is a very valuable tool for viewing what the program is doing. At the end of the output, you should see a line like this write (1, "hello", 5 Hello) . This is what we are looking. The real face of the hidden mask printf () . Since most people use library functions to operate on file I/O (such as fopen, fputs, and fclose ). You can view the second part of man's description using the command MAN 2 Write . The second part of man's description specifically describes system calls (for example, kill () and Read () ). The third part of man's description specifically introduces library functions that you may be more familiar with (such as cosh () and random () ).

you can even write code to overwrite system calls, as we will do soon. Hackers often do this to install backdoors or Trojans for the system. But you can use it to do something more useful, like letting the kernel output " Tee hee, that tickles! "Information

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.