Note: This article is reproduced in the network
The man in Linux is very powerful, and the manual is divided into sections, which can be used to specify a different section to navigate, with each section having the following meanings:
1-commands
2-system calls
3-library calls
4-special files
5-file formats and Convertions
6-games for Linux
7-macro Packages and conventions
8-system Management Commands
9-Other
Explain,
1 is a normal command.
2 is a system call, such as open,write (through this, at least can be easily found to call this function, need to add what header file)
3 is a library function, such as Printf,fread
4 is a special file, that is,/dev under the various device files
5 refers to the format of the file, such as passwd, will explain the meaning of the various fields in this file
6 is reserved for the game, defined by each game
7 is the attachment there are also some variables, such as to the Environ this global variable is described here
8 is a System Management command that can only be used by root, such as Ifconfig
To specify a section, add a number directly behind the man, such as:
Man 1 ls
Mans 3 printf
Wait a minute
For both command and system calls like Open,kill, man Open shows open (1), which starts with the first section, and if you want to see the open system call, Man 2 open
About library functions and system calls
Library functions are high-level, fully running in user space, providing programmers with a more convenient interface to invoke real-world system calls that do real-world transactions behind the scenes. System calls run in the kernel state and are provided by the kernel itself. The standard C library function printf()
can be seen as a generic output statement, but what it actually does is convert the data into a conforming string and invoke the system call to write()
output the strings.
Do you want to see printf()
what system calls are being used? It's easy to compile the code below.
#i nclude <stdio.h>int Main (void) {printf ("hello"); return 0;} |
Use the command Gcc-wall-o Hello hello.c compilation. Use the command strace Hello to trace the executable file. Are you surprised? Each row corresponds to a system call. Strace is a very useful program that can tell you which system calls the program uses and the parameters of these system calls, and the return value. This is a very valuable tool to see what the program is doing. At the end of the output, you should see a line like this write(1, "hello", 5hello)
. That's what we're looking for. Hidden in printf()
the true face of the mask. Since most people use library functions to manipulate file I/O (like fopen, Fputs, fclose). You can view the second part of the man description using the command man 2 write . The second part of the man description is devoted to system invocation (like kill()
and read()
). The third part of the man description specifically describes the library functions (like and) that you might be more familiar with cosh()
random()
.
You can even write code to overwrite system calls, as we will soon do. Hackers often do this to install a backdoor or Trojan for the system. But you can use it to do something more rewarding, like letting the kernel output " Tee hee, that tickles! " information every time someone deletes a file.
Self-Summary:
From the function of the program, the functions provided by the library are usually services that do not require an operating system, and functions are performed within the user space, unless the function involves I/O operations and so on, and generally does not cut into the kernel mentality.
function library, especially those related to input and output, are mostly done through Linux system calls. So we can think of function library functions as an intermediate layer between the Application designer and the system Invoker, through which we can safely invoke the system calls with a consistent interface. This allows programmers to write code once to be able to use the backlog between different versions of Linux systems to implement completely different system calls. As for how to implement compatibility issues with different system calls, this is a concern for library developers.
from the point of view of program execution efficiency, the execution efficiency of system calls is much higher than that of function, especially the function of processing input and output. When the amount of data processed is relatively small, the function library's function execution may be more efficient, because the function library's approach is to buffer the data that will be processed, wait until the buffer is full, and then write or read the data once. This approach is relatively efficient when dealing with small amounts of data, but when making system calls, because the user process enters the system core mode from user mode, involves a lot of additional task switching, called context switching, which can affect the efficiency of the system execution. However, when the amount of data to be processed is large, for example, when the amount of data input and output exceeds the size of the file system definition, the system call can be used to achieve higher efficiency.
from the point of view of program portability, the standard library of C (ANSI c) has a high portability compared to system calls, and in different system environments, as long as there are few modifications, Typically, there is no need to modify it.