编程语言
首页 > 编程语言> > UNIX环境高级编程-第二章习题

UNIX环境高级编程-第二章习题

作者:互联网

1,一些基本系统数据类型可以在多个头文件中定义。例如,在FreeBSD8.0中,size_t在29个不同的头文件中都有定义,由于一个程序可能包含这29个不同的头文件,但是ISO C却不允许对同一个名字进行多次typedef,那么如何编写这些头文件呢?

  答:采取下面的方式

  

1 #ifndef __XXX_t_defined
2 typedef __XXX_t XXX;
3 #dedine __XXX_t_defined
4 #endif

 

  这样如果有头文件已经定义了这个名字,那就不会冲突了。

2,检查系统的头文件,列出实现基本系统数据类型所用到的实际数据类型。

  答:ubuntu18系统。如下图,还有查看/usr/include/sys/types.h和/usr/include/bits/types.h文件可知

dog@dog:/usr/include/sys$ cat ../bits/typesizes.h
#ifndef _BITS_TYPES_H
# error "Never include <bits/typesizes.h> directly; use <sys/types.h> instead."
#endif

#ifndef _BITS_TYPESIZES_H
#define _BITS_TYPESIZES_H       1

/* See <bits/types.h> for the meaning of these macros.  This file exists so
   that <bits/types.h> need not vary across different GNU platforms.  */

/* X32 kernel interface is 64-bit.  */
#if defined __x86_64__ && defined __ILP32__
# define __SYSCALL_SLONG_TYPE   __SQUAD_TYPE
# define __SYSCALL_ULONG_TYPE   __UQUAD_TYPE
#else
# define __SYSCALL_SLONG_TYPE   __SLONGWORD_TYPE
# define __SYSCALL_ULONG_TYPE   __ULONGWORD_TYPE
#endif

#define __DEV_T_TYPE            __UQUAD_TYPE
#define __UID_T_TYPE            __U32_TYPE
#define __GID_T_TYPE            __U32_TYPE
#define __INO_T_TYPE            __SYSCALL_ULONG_TYPE
#define __INO64_T_TYPE          __UQUAD_TYPE
#define __MODE_T_TYPE           __U32_TYPE
#ifdef __x86_64__
# define __NLINK_T_TYPE         __SYSCALL_ULONG_TYPE
# define __FSWORD_T_TYPE        __SYSCALL_SLONG_TYPE
#else
# define __NLINK_T_TYPE         __UWORD_TYPE
# define __FSWORD_T_TYPE        __SWORD_TYPE
#endif
#define __OFF_T_TYPE            __SYSCALL_SLONG_TYPE
#define __OFF64_T_TYPE          __SQUAD_TYPE
#define __PID_T_TYPE            __S32_TYPE
#define __RLIM_T_TYPE           __SYSCALL_ULONG_TYPE
#define __RLIM64_T_TYPE         __UQUAD_TYPE
#define __BLKCNT_T_TYPE         __SYSCALL_SLONG_TYPE
#define __BLKCNT64_T_TYPE       __SQUAD_TYPE
#define __FSBLKCNT_T_TYPE       __SYSCALL_ULONG_TYPE
#define __FSBLKCNT64_T_TYPE     __UQUAD_TYPE
#define __FSFILCNT_T_TYPE       __SYSCALL_ULONG_TYPE
#define __FSFILCNT64_T_TYPE     __UQUAD_TYPE
#define __ID_T_TYPE             __U32_TYPE
#define __CLOCK_T_TYPE          __SYSCALL_SLONG_TYPE
#define __TIME_T_TYPE           __SYSCALL_SLONG_TYPE
#define __USECONDS_T_TYPE       __U32_TYPE
#define __SUSECONDS_T_TYPE      __SYSCALL_SLONG_TYPE
#define __DADDR_T_TYPE          __S32_TYPE
#define __KEY_T_TYPE            __S32_TYPE
#define __CLOCKID_T_TYPE        __S32_TYPE
#define __TIMER_T_TYPE          void *
#define __BLKSIZE_T_TYPE        __SYSCALL_SLONG_TYPE
#define __FSID_T_TYPE           struct { int __val[2]; }
#define __SSIZE_T_TYPE          __SWORD_TYPE
#define __CPU_MASK_TYPE         __SYSCALL_ULONG_TYPE

#ifdef __x86_64__
/* Tell the libc code that off_t and off64_t are actually the same type
   for all ABI purposes, even if possibly expressed as different base types
   for C type-checking purposes.  */
# define __OFF_T_MATCHES_OFF64_T        1

/* Same for ino_t and ino64_t.  */
# define __INO_T_MATCHES_INO64_T        1

/* And for __rlim_t and __rlim64_t.  */
# define __RLIM_T_MATCHES_RLIM64_T      1
#else
# define __RLIM_T_MATCHES_RLIM64_T      0
#endif

/* Number of descriptors that can fit in an `fd_set'.  */
#define __FD_SETSIZE            1024


#endif /* bits/typesizes.h */

可知pid_t的实际数据类型是有符号32位整数,同理可知其他。

3,改写一下程序,使其在sysyconf为OPEN_MAX限制返回LONG_MAX时,避免进行不必要的处理。

#include "apue.h"
#include <errno.h>
#include <limits.h>

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

/*
 * If OPEN_MAX is indeterminate, this might be inadequate.
 */
#define    OPEN_MAX_GUESS    256

long
open_max(void)
{
    if (openmax == 0) {        /* first time through */
        errno = 0;
        if ((openmax = sysconf(_SC_OPEN_MAX)) < 0) {
            if (errno == 0)
                openmax = OPEN_MAX_GUESS;    /* it's indeterminate */
            else
                err_sys("sysconf error for _SC_OPEN_MAX");
        }
    }
    return(openmax);
}

 

   答:如果OPEN_MAX是未确定的或大得出奇(即等于LONG_MAX,效果等同于无限制),那个可以使用getrlimit得到每个进程的最大打开文件描述符数。因为可以修改对每个进程的限制,所以我们不能将前一个调用得到的值高速缓存起来,所以要把openmax改成局部变量。

#include <stdio.h>
#include <errno.h>
#include <limits.h>
#include <unistd.h>
#include <sys/resource.h>
/*
 * If OPEN_MAX is indeterminate, this might be inadequate.
 */
#define    OPEN_MAX_GUESS    256

long
open_max(void)
{
    long openmax;
    struct rlimit rl;

    if ((openmax = sysconf(_SC_OPEN_MAX)) < 0 || openmax == LONG_MAX) 
    {
        if (getrlimit(RLIMIT_NOFILE, &rl) < 0)
            perror("can't get file limit");
        if (rl.rlim_max == RLIM_INFINITY)
            openmax = OPEN_MAX_GUESS;
        else
            openmax = rl.rlim_max;
    }
    return(openmax);
}

int main(int argc, char *argv[])
{
    printf("openmax is %ld\n", open_max());
    
}

 

标签:__,SYSCALL,MAX,编程,UNIX,openmax,习题,TYPE,define
来源: https://www.cnblogs.com/mingyunrangwozoudaoxianzai/p/12357483.html