第2章——《Unix标准及实现》

2018-06-25  本文已影响0人  alex_man

实验环境介绍

引言

Unix标准化

Unix系统实现

标准与实现的差异

限制

unix限制

后面说明

ISO C限制
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>

int
main(int argc, char *argv[])
{
    printf("CHAR_BIT: %d\n", CHAR_BIT);
    printf("CHAR_MAX: %d\n", CHAR_MAX);
    printf("CHAR_MIN: %d\n", CHAR_MIN);
    printf("INT_MAX: %d\n", INT_MAX);
    printf("INT_MIN: %d\n", INT_MIN);

    exit(EXIT_SUCCESS);
}

result:
CHAR_BIT: 8
CHAR_MAX: 127
CHAR_MIN: -128
INT_MAX: 2147483647
INT_MIN: -2147483648
POSIX限制

某些变量可能没有在<limits.h>中,所以需要使用sysconf、pathconf、fpathconf来获取

XSI限制
函数sysconf、pathconf和fpathconf
#include <limits.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#define ONE_MB  (1024 * 1024)

int
main(int argc, char *argv[])
{
    // test sysconf
    printf("The number of processors configured is :%ld\n",
        sysconf(_SC_NPROCESSORS_CONF));
    printf("The number of processors currently online (available) is :%ld\n",
        sysconf(_SC_NPROCESSORS_ONLN));
    printf ("The pagesize: %ld\n", sysconf(_SC_PAGESIZE));
    printf ("The number of pages: %ld\n", sysconf(_SC_PHYS_PAGES));
    printf ("The number of available pages: %ld\n", sysconf(_SC_AVPHYS_PAGES));
    printf ("The memory size: %lld MB\n",
        (long long)sysconf(_SC_PAGESIZE) * (long long)sysconf(_SC_PHYS_PAGES) / ONE_MB );
    printf ("The number of files max opened:: %ld\n", sysconf(_SC_OPEN_MAX));
    printf("The number of ticks per second: %ld\n", sysconf(_SC_CLK_TCK));
    printf ("The max length of host name: %ld\n", sysconf(_SC_HOST_NAME_MAX));
    printf ("The max length of login name: %ld\n", sysconf(_SC_LOGIN_NAME_MAX));

    // test
    char pathname[] = "/home";
    printf("NAME_MAX = %ld\n", pathconf(pathname, _PC_NAME_MAX));
    printf("PATH_MAX = %ld\n", pathconf(pathname, _PC_PATH_MAX));
    printf("LINK_MAX = %ld\n", pathconf(pathname, _PC_LINK_MAX));
    printf("MAX_INPUT = %ld\n", pathconf(pathname, _PC_MAX_INPUT));
    printf("MAX_CANON = %ld\n", pathconf(pathname, _PC_MAX_CANON));
    printf("PIPE_BUF = %ld\n", pathconf(pathname, _PC_PIPE_BUF));

    // test fpathconf
    int  files = open(pathname, O_RDONLY);
    printf("NAME_MAX = %ld\n", fpathconf(files, _PC_NAME_MAX));
    printf("PATH_MAX = %ld\n", fpathconf(files, _PC_PATH_MAX));
    printf("LINK_MAX = %ld\n", fpathconf(files, _PC_LINK_MAX));
    printf("MAX_INPUT = %ld\n", fpathconf(files, _PC_MAX_INPUT));
    printf("MAX_CANON = %ld\n", fpathconf(files, _PC_MAX_CANON));
    printf("PIPE_BUF = %ld\n", fpathconf(files, _PC_PIPE_BUF));
    exit(EXIT_SUCCESS);
}

result:
The number of processors configured is :2
The number of processors currently online (available) is :2
The pagesize: 4096
The number of pages: 249657
The number of available pages: 35592
The memory size: 975 MB
The number of files max opened:: 1024
The number of ticks per second: 100
The max length of host name: 64
The max length of login name: 256
NAME_MAX = 255
PATH_MAX = 4096
LINK_MAX = 2147483647
MAX_INPUT = 255
MAX_CANON = 255
PIPE_BUF = 4096
NAME_MAX = 255
PATH_MAX = 4096
LINK_MAX = 2147483647
MAX_INPUT = 255
MAX_CANON = 255
PIPE_BUF = 4096
不确定的运行时限制
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <limits.h>
#include <unistd.h>

#ifdef PATH_MAX
static long pathmax = PATH_MAX;
#else
static long pathmax = 0;
#endif

static long posix_version = 0;
static long xsi_version = 0;


#define PATH_MAX_GUESS 1024

char *
path_alloc(size_t *sizep)
{
    char *ptr;
    size_t size;

    if (!posix_version)
        posix_version = sysconf(_SC_VERSION);

    if (!xsi_version)
        xsi_version = sysconf(_SC_XOPEN_VERSION);

    if (!pathmax) {
        errno = 0;
        if ((pathmax = pathconf("/", _PC_PATH_MAX)) < 0) {
            int errno_save = errno;
            if (errno_save == 0)
                pathmax = PATH_MAX_GUESS;
            else
                perror("pathconf error for _PC_PATH_MAX:");
        } else
            pathmax++;
    }

    if ((posix_version < 200112L) && (xsi_version < 4))
        size = pathmax + 1;
    else
        size = pathmax;

    if ((ptr = malloc(size)) == NULL)
        perror("malloc error for pathname");

    if (sizep)
        *sizep = size;

    return (ptr);
}

int
main(int argc, char *argv[])
{
    size_t size;
    char *ptr = path_alloc(&size);
    if (ptr)
        printf("alloc size = %zu successfully\n", size);
    else
        printf("alloc failed\n");

    exit(EXIT_SUCCESS);
}

result:
[root@localhost part_2]# ./2_16 
alloc size = 4096 successfully
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <limits.h>
#include <unistd.h>

#ifdef OPEN_MAX
static long openmax = OPEN_MAX;
#else
static long openmax = 0;
#endif

#define OPEN_MAX_GUESS 256

long
open_max(void)
{
    if (!openmax) {
        errno = 0;
        if ((openmax = sysconf(_SC_OPEN_MAX)) < 0) {
            if (!errno)
                openmax = OPEN_MAX_GUESS;
            else
                perror("sysconf error for _SC_OPEN_MAX");
        }
    }

    return (openmax);
}

int
main(int argc, char *argv[])
{
    long openmax = open_max();
    if (openmax < 0)
        printf("open_max get error\n");
    else
        printf("openmax is: %ld\n", openmax);
    exit(EXIT_SUCCESS);
}

result:
[root@localhost part_2]# ./2_17 
openmax is: 1024

选项

功能测试宏

基本系统数据类型

标准之间的冲突

上一篇下一篇

猜你喜欢

热点阅读