UNIX Environment Advanced Programming (4): UNIX Standardization and Implementation (2)

Source: Internet
Author: User

Indeterminate run-time limitations:

If some of the throttling values are not defined in <limits.h>, they are not available at compile time, and they are undefined even for run-time constraints if their values are indeterminate.

The following program is used to dynamically allocate storage for pathname (in general, many programs allocate storage for them at compile time, and different programs use different magic numbers, such as 256,512,1024 or standard I/O constants Bufsiz, but are rarely correct).

/* Copyright (C) [email protected] */#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <limits.h> #include <errno.h> #define SUSV3 200112l#define path_max_guess 1024#ifdef path_ maxstatic int path_max = Path_max; #else static int path_max = 0; #endifstatic long posix_version = 0;char *path_alloc (int * SIZEP); char *path_alloc (int *sizep) {int Size;char *ptr;if (posix_version = = 0) {posix_version = sysconf (_sc_version);} if (Path_max = = 0) {errno = 0;if ((Path_max = pathconf ("/", _pc_path_max) < 0)) {if (errno = = 0) {Path_max = Path_max_ GUESS;} else {printf ("pathconf Errnor for _pc_path_max\n");}} else {path_max++;}} if (Posix_version < SUSV3) {size = Path_max + 1;} else {size = Path_max;} if (ptr = malloc (size) = = NULL) {printf ("malloc error\n");} if (Sizep! = NULL) {*sizep = size;} return ptr;} Intmain () {int Size;char *ptr;ptr = Path_alloc (&size);p rintf ("Path_max =%d\n", size); if (ptr! = NULL) {free (PTR);} Exit (0);}
The following are some notes on the procedure:

(1) The PATHCONF function returns the maximum length based on the relative pathname of the working directory, and the working directory is its first parameter. Therefore, the root directory is used as a parameter to call, and the resulting return value of 1, you can get the maximum length of the path name.

(2) before SUS v3, it is unclear whether Path_max already contains a null character at the end of the pathname, and if the operating system follows the previous standard version, add 1 to the number of storage allocated for the pathname.

(3) If Path_max is uncertain, we can only guess one value.

Maximum number of open files:

a common code sequence in a daemon (daemon, which is a process that runs in the background and is not connected to a terminal, also known as a daemon or background process), is to close all open files. we can use the Open_max in posix.1 to determine the maximum number of open files to improve the portability of the code.

The following program gets the maximum number of open files, and if Open_max is undefined, only one limit value can be guessed. Although this does not guarantee proper action in all cases, this is the best method we can choose.

/* Copyright (C) [email protected] */#include <stdio.h> #include <stdlib.h> #include <limits.h># Include <unistd.h> #include <errno.h> #ifdef open_maxstatic long OpenMAX = Open_max; #elsestatic long OpenMAX = 0; #endif # define open_max_guess 256long Open_max (void), Long Open_max (void) {if (OpenMAX = = 0) {errno = 0;if (OpenMAX = sys Conf (_sc_open_max)) < 0) {if (errno = = 0) {OpenMAX = open_max_guess;} else {printf ("Sysconf error for _sc_open_max\n"); }}}return OpenMAX;} int main () {printf ("Oepnmax =%d\n", Open_max ()); exit (0);}

The running results of the program are as follows:

This is consistent with the results of Ulimt-n:

The above program also has a problem, because Linux can call the Ulimit command at any time to change the maximum number of files can be opened, and the above program only call the Open_max function when the sysconf function is called, so in order to deal with this situation, the above procedure should be modified so that each call to open The sysconf function is called when _max.

The system that supports the xsi extension of single UNIX specification provides the Getrlimit (2) function, which can be used to return the maximum number of descriptors that a process can open at the same time.

Options:

If we want to write some portable applications first, and these programs are related to the supported options, then a portable method is required to determine whether a given option is supported.

As with restrictions, SUS defines three processing methods:

    • Compile-time options are defined in <unistd.h>;
    • Options unrelated to the file or directory can be determined using the SYSCONF function;
    • Options related to files or directories can be determined by pathconf or fpathconf functions;
If the symbol constants are undefined, you must use sysconf, pathconf, or fpathconf to determine whether the option is supported. If the platform defines symbolic constants, the following three possibilities are available:
    • If the defined value of the symbolic constant is-1, then the platform does not support the corresponding option;
    • If the defined value of the symbolic constant is greater than 0, then the platform supports the appropriate option;
    • If the symbolic constant has a defined value of 0, you must call sysconf, pathconf to determine whether the appropriate option is supported;

Functional Test Macros:

Most implementations, in addition to defining many POSIX and xsi symbols in the header file, add their own definitions to the header files. If you are compiling a program that only wants to use the POSIX definition without using any implementation-defined restrictions, you need to define the constant _posix_c_source, which is used by all POSIX header files. When you define this constant, you can exclude any implementation-specific definitions.

Constants _posix_c_source and _xopen_source are called functional test macros.

Another functional test macro is: __stdc__, which is automatically defined by the C compiler that complies with the ISO C standard. This allows us to write programs that both the ISO C compiler and the non-ISO C compiler can compile. Although most compilers support the ISO C standard, the __stdc__ feature is still used in many header files to test macros.

Basic System data type:

The header file <sys/types.h> defines some implementation-related data types, which are referred to as the basic system data type (primitive). There are many more of these data types defined in other header files. In the header file, these data types are defined with the TypeDef functionality of C, and most of them end with _t.

Once these data types have been defined in this way, it is no longer necessary to consider the system-specific implementation details.

Conflicts between the standards:

On the whole, these different standards are very well matched. However, there are some differences between them, for example, there are some differences between ISO C and POSIX.1 standards, and SUS v3 itself is a superset of posix.1.



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

UNIX Environment Advanced Programming (4): UNIX Standardization and Implementation (2)

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.