Linux programming knowledge

Source: Internet
Author: User
Tags mathematical constants
Linux programming knowledge-general Linux technology-Linux programming and kernel information. The following is a detailed description. 1.4.1 standard (ansi c, POSIX, SVID, XPG ,...)
Ansi c: This is the C language standard set by ANSI (US National Standards Agency) in 1989. It was later accepted as a standard by ISO (International Organization for Standardization) and therefore known as iso c.
Ansi c aims to provide portability guarantee for C Programs on various operating systems, not limited to UNIX. This standard not only defines the language and semantics of the C programming language, but also defines a standard library. This library can be divided into 15 parts based on the header file, including: character type ( ), Error code ( ), Floating point constant ( ), Mathematical constants ( ), Standard definition ( ), Standard I/O ( ), Tool functions ( ), String operations ( ), Time, and date ( ), Variable parameter table ( ), Signal ( ), Non-local jump ( ), Local information ( ), Program assertions () and so on.
POSIX: The standard family originally developed by IEEE, which has been accepted by ISO as an international standard. For details about the standard, see 1.1.3. POSIX.1 and POSIX.2 define POSIX-Compatible C language system interfaces and shell and tool standards. These two standards are commonly mentioned.
SVID: System V interface description. The System V interface description (SVID) is a document describing the AT & T Unix System V operating System. It is an extended superset of POSIX standards.
XPG: X/Open portability guide. The X/Open portability Guide (published by X/Open Company, Ltd.) is a more general standard than POSIX. X/Open has Unix copyrights, while XPG specifies that the Unix operating system must meet the requirements.
1.4.2 function libraries and system calls
1. glibc
As we all know, C language does not provide built-in support for common operations, such as input/output, memory management, and string operations. On the contrary, these functions are generally provided by the standard "function library. The gnu c function library, glibc, is the most important function library in Linux. It defines all library functions specified by the iso c standard, additional features specified by POSIX or other unix OS Variants include GNU system-related extensions. Currently, glibc 2.0 and later are popular Linux systems. Glibc is based on the following standards:

Iso c: International Standard for the C programming language (ansi c.
POSIX: the gnu c function library implements all the functions specified by ISO/IEC 9945-1: 1996 (POSIX System Application Programming Interface, POSIX.1. This standard is an extension of iso c, including the original language of the file system interface, terminal control functions related to the device, and process control functions. At the same time, the gun c function library also supports some functions specified by ISO/IEC 9945-2: 1993 (POSIX Shell and tool standard, namely POSIX.2, these include functions used to process regular expressions and pattern matching.
Berkeley Unix: BSD and SunOS. The gnu c function library defines unstandardized functions in some UNIX versions, especially 4.2 BSD, 4.3 BSD, 4.4 BSD Unix systems (I .e. "Berkeley Unix ") and "SunOS" (Popular 4.2 BSD variants, including some Unix System V functions ). BSD functions include symbolic links, select functions, BSD signal processing functions, and sockets.
SVID: System V interface description. The gnu c function library defines most functions specified by SVID but not specified by iso c and POSIX. Supported functions from System V include inter-process communication and shared memory, hsearch and drand48 Function Families, fmtmsg, and some mathematical functions.
XPG: X/Open portability guide. The gnu c function library complies with the X/Open portability Guide (Issue 4.2) and all XSI (X/Open system interfaces) compatible with system extensions, all X/Open Unix extensions are also followed.
2. Other important function libraries
In addition to glibc, popular Linux distributions also contain some other function libraries that play an important role, such:

GNU Libtool: GNU Libtool is actually a script generation tool that provides general shared library support for software package developers.
In the past, if the developers of source code packages wanted to take advantage of the shared library, they had to write customized support code for the platforms supported by each software package. You also need to design the configuration interface so that the installation program of the software package can correctly select the library type to be created. Using GNU Libtool can simplify the developer's work. It encapsulates platform-related dependencies and user interfaces in a separate script. GNU Libtool allows the complete functions of each host type to be obtained through general interfaces, while hiding the particularity of the host for programmers. The GNU Libtool consistency interface is reliable. You do not have to read obscure documents to create a shared library on each platform. They only need to run the configuration script of the software package, and libtool does complicated work.
CrackLib: CrackLib provides a C-language function interface, which can be used to prevent users from selecting a password that is easy to crack. This function library can be used in programs similar to passwd.
LibGTop: LibGTop is a function library that obtains process information and system running information, including: system general information, sys v ipc restrictions, process list, process information, process ing, and file system usage information.
Image File Operation function library: including libungif, libtiff, libpng, Imlib, and libjpeg, which can be used to operate GIF, TIFF, PNG, JPEG, and other format image files respectively.
3. System Call
A system call is an interface provided by the operating system to external programs. In C language, system calls of the operating system are usually completed through function calls because these functions encapsulate the details of system calls, the system calls the entry, parameter, and return value in the C language function call process. In Linux, system call functions are defined in glibc.

When talking about system calling, pay attention to the following points:

A system call function usually returns 0 when the call succeeds, and a non-zero value when the call fails. If you want to check the cause of failure, you must determine the value of the global variable errno. errno contains error code.
The returned data of many system calls is usually transmitted through reference parameters. In this case, you need to pass a buffer address in the function parameter, and the returned data is saved in the buffer.
It cannot be considered that the system calls functions more efficiently than other functions. Note that system calling is a very time-consuming process.
System calls will be detailed later.

1.4.3 online documentation (man, info, HOW-,...)
1. man
Man, or manunal, is an electronic version of the UNIX System Manual. According to the habit, UNIX system manuals are usually divided into different parts (or sections), each section describes different system content. The current sections are divided as follows:

Command: Common USER command
System Call: kernel interface
Function library call: Functions in the common function library
Special files: special files in the/dev directory
File Format and conventions:/etc/passwd and other file formats
Game.
Miscellaneous and conventions: Miscellaneous content such as standard file system layout and manual page Structure
System Management commands.
Kernel routine: Non-Standard Manual section. It facilitates Linux kernel development and includes
Other manual sections:

L: PostgreSQL database commands
N: TCL/TK command
The manual page is generally stored in the/usr/man directory. Each subdirectory (such as man1, man2,..., manl, mann) contains different manual sections. Run the man command to view the manual page.

Man command line:
Man [-acdfFhkKtwW] [-m system] [-p string] [-C config_file] [-M path] [-P pager] [-S section_list] [section] name

Common command lines:
$ Man open
$ Man 7 man
$ Man./myman.3


2. info
In Linux, most software development tools come from the GNU project of the Free Software Foundation. The online documents of these tools and software are in the form of info files. The info program is a GNU hypertext help system.

The info document is generally stored in the/usr/info directory. Run the info command to view the info document.

To run info, enter info at the shell prompt or Press Esc-x and info in GNU emacs.

Info helps the system to display a topic directory on the initial screen. You can move the cursor over the topic menu with * and press Enter.
Enter the topic, or enter m, followed by the name of the topic menu. For example, you can type m and then type gcc.
Go to the gcc topic.
If you want to jump between topics, you must remember the following command keys:
* N: jump to the next node of the node;
* P: Jump to the previous node of the node;
* M: Specify the menu name and select another node;
* F: Enter the cross-reference topic;
* L: Enter the last node in the window;
* TAB: jump to the next hypertext link in the window;
* RET: hypertext link at the cursor;
* U: Go to the previous topic;
* D: Return to the initial node directory of info;
* H: Call up the info tutorial;
* Q: Exit info.

# DEMO #

3. HOW-
Another form of online documentation for your reference is HOWTO files, which are located in the/usr/doc/HOWTO directory of the system. The file names of HOWTO files all have a-HOWTO suffix and are all text files.

Each HOWTO file contains information about one aspect of Linux, such as the hardware it supports or how to create a boot disk.

To view these files, go to the/usr/doc/HOWTO directory and run the more command in the following format:
$ Cd/usr/doc/HOWTO; more topic-name-HOWTO

In addition, HOWTO documents also contain files in other formats, such as HTML and PS, stored in/usr/doc/HOWTO/other-formats.

4. Miscellaneous
Linux Kernel Documentation is generally included in the kernel source code, the directory is as follows:/usr/src/linux-2.x.x/Documentation

The/usr/doc directory contains a large number of Descriptive documents related to specific software or function libraries.

1.4.4 C programming style
The purpose of writing this section is to remind everyone to pay attention to the programming style during the programming process. If you are writing some small exercise programs with only 100 or 200 presidents, the programming style may not matter. However, if you work with many developers or want to understand your programs correctly after a while, you must develop good programming habits. Among many programming habits, programming style is the most important.

A good programming style can help developers in many aspects. If you have read the Linux kernel source code, you may be overwhelmed by beautiful program orchestration. A good programming style can increase the readability of the code and help you clarify your clues. If the program is messy, you may be confused at a glance. The programming style best reflects the overall quality of a programmer.

Many readers may be familiar with the Hungary naming method promoted by Windows. This method defines very complex naming methods for functions, variables, and types. A typical naming method is case-insensitive. For variable names, you can add a prefix to indicate its type. For example:
Char szBuffer [20];
Int nCount;
Use sz and n to represent strings and integers respectively. To represent a variable name, it is possible to use the following variable name:
Int iThisIsAVeryLongVariable;

In Linux, we often see simple function interfaces and variable names. In the source code of the Linux kernel, you can see the encoding style (/Documentation/CodingStyle) of the source code of the Linux kernel ). One feature of UNIX systems is that they are well-designed and follow the building principle. The C language was originally from a UNIX operating system. Like the design principles of UNIX, one important reason why C language is widely recognized and used is its flexibility and simplicity. Therefore, when using the C language to write a program, it should always comply with its concise design principles, instead of using a very complex variable naming method. Linus defines the C language encoding style for the Linux kernel as follows:

The Tab key with a length of 8 characters is used for indentation. If the program indentation exceeds three levels, you should consider re-designing the program.
The location of the braces. In addition to the definition of the function, the left braces should be placed at the end of the row, and the right braces should be placed at the beginning of the row. The Function Definition body should place the Left and Right braces at the beginning of the line. As follows:
Int function (int x, int y)
{
If (x = y ){
...
} Else if (x> y ){
...
} Else {
...
}

Return 0;
}

A simple naming method should be used. For variable names, we do not agree to use case-insensitive statements, but encourage descriptive names. Do not use global variables as much as possible; do not use the Hungarian name method to represent the type of variables; use a short and concise name to represent local variables. Keep the function short to avoid using too many local variables.
Keep the function short and concise.
The role of annotations should not be overly emphasized. A good coding style should be used as much as possible instead of adding too many annotations.
1.4.5 storage location of library and header files
1. function library
/Lib: required shared library
/Usr/lib: Standard shared library and static library
/Usr/i486-linux-libc5/lib: libc5 compatibility function library
/Usr/X11R6/lib: X11R6 function library
/Usr/local/lib: local function library
2. header files
/Usr/include: System header file
/Usr/local/include: local header file
1.4.6 shared library and related configurations
/Etc/ld. so. conf: Location of the Shared Library
Ldconfig: Shared library management tool. Generally, you need to run this command after updating the shared library.
Ldd: allows you to view the shared libraries used by executable files.

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.