现在的位置: 首页 > 综合 > 正文

CTDP linux 程序员手册 C和C++编程(4)Linux POSIX 系统的兼容型

2014年01月02日 ⁄ 综合 ⁄ 共 8444字 ⁄ 字号 评论关闭

这一章将介绍如何去确定您的系统都支持POSIX的哪些部分。最容易的办法是察看系统中的文件/usr/include/unistd.h 。这个文件将定义_POSIX_SOURCE 版本。它也可能包括其他的POSIX 定义头文件。其中的一个是/usr/include/bits/posix_opt.h ,里边应该由更多的POSIX定义。下面的所有参数都列在那里。当然,最好的方法是使用下面的程序。因为在你得系统中可能没有/usr/include/inistd.h 文件。同时你也会关心你的系统不支持哪些功能。它将依赖与编译它的库和编译时使用的选项。内核对于支持POSIX.4的功能将有较大的影响,因为它控制内存的分配和处理,包括它们之间的信号。

测试程序

下面的程序将输出你得系统都支持哪些特性。我称它为 "posixtst.c". 这是一个 tar 和 gz 源代码和二进制程序版本,并且带有一个README 文件和安装命令。 posixtst-1.0.0

#define _POSIX_SOURCE
#define _POSIX_C_SOURCE 199309
#include <unistd.h>
 
long val;
int errno;
 
main()
{
#ifndef _POSIX_VERSION
   printf("No support for POSIX on this system!/n");
#else     //see what POSIX version
   printf("POSIX_VERSION = %d./n",_POSIX_VERSION);
   printf("POSIX2_VERSION = %d./n",_POSIX2_VERSION);
   printf("POSIX2_C_VERSION = %d./n",_POSIX2_C_VERSION);
   #if _POSIX_VERSION == 199009
      printf("This system supports POSIX.1 without support for POSIX.4./n");
   #else
      #if _POSIX_VERSION >= 199309
         printf ("This system supports POSIX.1 and POSIX.4. /n");
      #else
         printf("This system supports a strange POSIX version between 199009 and 199309./n");
      #endif
   //DO POSIX.4 tests
      printf ("POSIX.4 Options Test Results:/n");
      #ifdef _POSIX_REALTIME_SIGNALS
         printf("   POSIX.4 Additional real time signals are supported./n");
         #ifdef _POSIX_RTSIG_MAX
            printf("     _POSIX_RTSIG_MAX=%d Max real time signals./n",_POSIX_RTSIG_MAX);
         #else
            printf("     No _POSIX_RTSIG_MAX value exists./n");
         #endif
         #ifdef _POSIX_SIGQUEUE_MAX
            printf("     _POSIX_SIGQUEUE_MAX=%d Max real time signals at once per process./n",_POSIX_RTQUEUE_MAX);
         #else
            printf("     No _POSIX_SIGQUEUE_MAX value exists./n");
         #endif
      #else
         printf("   POSIX.4 Additional real time signals are not supported./n");
      #endif
      #ifdef _POSIX_PRIORITY_SCHEDULING
         printf("   POSIX.4 Priority scheduling is supported./n");
      #else
         printf("   POSIX.4 Priority scheduling is not supported./n");
      #endif
      #ifdef _POSIX_TIMERS
         printf("   POSIX.4 Clocks and timers are supported./n");
         #ifdef _POSIX_TIMER_MAX
            printf("     _POSIX_TIMER_MAX=%d Max number of concurrent timers a process can have./n",_POSIX_TIMER_MAX);
         #else
            printf("     No _POSIX_TIMER_MAX value exists./n");
         #endif
         #ifdef _POSIX_DELAYTIMER_MAX
            printf("     _POSIX_DELAYTIMER_MAX=%d Max number of times a timer can have a detectable overrun./n",_POSIX_DELAYTIMER_MAX);
         #else
            printf("     No _POSIX_DELAYTIMER_MAX value exists./n");
         #endif
      #else
         printf("   POSIX.4 Clocks and timers are not supported./n");
      #endif
      #ifdef _POSIX_ASYNCHRONOUS_IO
         printf("   POSIX.4 Asynchronous I/O is supported./n");
         #ifdef _POSIX_AIO_LISTIO_MAX
            printf("     _POSIX_AIO_LISTIO_MAX=%d Max operations in one call/n",_POSIX_AIO_LISTIO_MAX);
         #else
            printf("     No _POSIX_AIO_LISTIO_MAX value exists./n");
         #endif
         #ifdef _POSIX_AIO_MAX
            printf("     _POSIX_AIO_MAX=%d Max concurrent Async I/Os/n",_POSIX_AIO_MAX);
         #else
            printf("     No _POSIX_AIO_MAX value exists./n");
         #endif
      #else
         printf("   POSIX.4 Asynchronous I/O is not supported./n");
      #endif
      #ifdef _POSIX_SYNCHRONIZED_IO  //Only supported if Asynchronous I/O is supported
         printf("   POSIX.4 Synchronized I/O is supported./n");
      #else
         printf("   POSIX.4 Synchronized I/O is not supported./n");
      #endif
      #ifdef _POSIX_PRIORITIZED_IO
         printf("   POSIX.4 Prioritized asynchronous I/O is supported./n");
         #ifdef _POSIX_AIO_PRIO_DELTA_MAX
            printf("     _POSIX_AIO_PRIO_DELTA_MAX=%d Max amount AIO priority can be decreased./n",_POSIX_AIO_PRIO_DELTA_MAX);
         #else
            printf("     No _POSIX_AIO_PRIO_DELTA_MAX value exists./n");
         #endif
      #else
         printf("   POSIX.4 Prioritized asynchronous I/O is not supported./n");
      #endif
      #ifdef _POSIX_FSYNC
         printf("   POSIX.4 The fsync function is supported./n");
      #else
         printf("   POSIX.4 The fsync function is not supported./n");
      #endif
      #ifdef _POSIX_MAPPED_FILES
         printf("   POSIX.4 Mapping files as memory is supported./n");
      #else
         printf("   POSIX.4 Mapping files as memory is not supported./n");
      #endif
      #ifdef _POSIX_MEMLOCK
         printf("   POSIX.4 Locking memory is supported./n");
      #else
         printf("   POSIX.4 Locking memory is not supported./n");
      #endif
      #ifdef _POSIX_MEMLOCK_RANGE
         printf("   POSIX.4 Locking memory ranges is supported./n");
      #else
         printf("   POSIX.4 Locking memory ranges is not supported./n");
      #endif
      #ifdef _POSIX_MEMORY_PROTECTION
         printf("   POSIX.4 Setting memory protection is supported./n");
      #else
         printf("   POSIX.4 Setting memory protection is not supported./n");
      #endif
      #ifdef _POSIX_MESSAGE_PASSING
         printf("   POSIX.4 Message Queues are supported./n");
         #ifdef _POSIX_MQ_OPEN_MAX
            printf("     _POSIX_MQ_OPEN_MAX=%d Max # of message queues per process./n",_POSIX_MQ_OPEN_MAX);
         #else
            printf("     No _POSIX_MQ_OPEN_MAX value exists./n");
         #endif
         #ifdef _POSIX_MQ_PRIO_MAX
            printf("     _POSIX_MQ_PRIO_MAX=%d Max # of message priorities./n",_POSIX_MQ_PRIO_MAX);
         #else
            printf("     No _POSIX_MQ_PRIO_MAX value exists./n");
         #endif
      #else
         printf("   POSIX.4 Message Queues are not supported./n");
      #endif
      #ifdef _POSIX_SEMAPHORES
         printf("   POSIX.4 Semaphores are supported./n");
         #ifdef _POSIX_SEM_NSEMS_MAX
            printf("     _POSIX_SEM_NSEMS_MAX=%d Max # of open semaphores per process./n",_POSIX_SEM_NSEMS_MAX);
         #else
            printf("     No _POSIX_SEM_NSEMS_MAX value exists./n");
         #endif
         #ifdef _POSIX_SEM_VALUE_MAX
            printf("     _POSIX_SEM_VALUE_MAX=%d Maximum semaphore value./n",_POSIX_SEM_VALUE_MAX);
         #else
            printf("     No _POSIX_SEM_VALUE_MAX value exists./n");
         #endif
      #else
         printf("   POSIX.4 Semaphores are not supported./n");
      #endif
      #ifdef _POSIX_SHARED_MEMORY_OBJECTS
         printf("   POSIX.4 Shared memory objects are supported./n");
      #else
         printf("   POSIX.4 Shared memory objects are not supported./n");
      #endif
 
      #ifdef _POSIX_THREADS
         printf("   POSIX.1c pthreads are supported./n");
      #else
         printf("   POSIX.1c pthreads are not supported./n");
      #endif
      #ifdef _POSIX_THREAD_ATTR_STACKADDRTHREAD_ATTR_STACKADDR
         printf("   POSIX.4 Thread stack address attribute option is supported./n");
      #else
         printf("   POSIX.4 Thread stack address attribute option is not supported./n");
      #endif
      #ifdef _POSIX_THREAD_ATTR_STACKSIZE
         printf("   POSIX.4 Thread stack size attribute option is supported./n");
      #else
         printf("   POSIX.4 Thread stack size attribute option is not supported./n");
      #endif
      #ifdef _POSIX_THREAD_SAFE_FUNCTIONS
         printf("   POSIX.4 Thread-safe functions are supported./n");
      #else
         printf("   POSIX.4 Thread-safe functions are not supported./n");
      #endif
      #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
         printf("      POSIX.1c thread execution scheduling is supported./n");
      #else
         printf("      POSIX.1c thread execution scheduling is not supported./n");
      #endif
      #ifdef _POSIX_THREAD_PRIO_INHERIT
         printf("   POSIX.4 Thread priority inheritance option is supported./n");
      #else
         printf("   POSIX.4 Thread priority inheritance option is not supported./n");
      #endif
      #ifdef _POSIX_THREAD_PROCESS_SHARED
         printf("   POSIX.4 Process-shared synchronization is supported./n");
      #else
         printf("   POSIX.4 Process-shared synchronization is not supported./n");
      #endif
      #ifdef _POSIX_POSIX_PII
         printf("   Protocol-independent interfaces are supported./n");
      #else
         printf("   Protocol-independent interfaces are not supported./n");
      #endif
      #ifdef _POSIX_PII_XTI
         printf("   XTI protocol-indep. interfaces are supported./n");
      #else
         printf("   XTI protocol-indep. interfaces are not supported./n");
      #endif
      #ifdef _POSIX_PII_SOCKET
         printf("   Socket protocol-indep. interfaces are supported./n");
      #else
         printf("   Socket protocol-indep. interfaces are not supported./n");
      #endif
      #ifdef _POSIX_PII_INTERNET
         printf("   Internet family of protocols is supported./n");
      #else
         printf("   Internet family of protocols is not supported./n");
      #endif
      #ifdef _POSIX_PII_INTERNET_STREAM
         printf("   Connection-mode Internet protocol is supported./n");
      #else
         printf("   Connection-mode Internet protocol is not supported./n");
      #endif
      #ifdef _POSIX_PII_INTERNET_DGRAM
         printf("   Connectionless Internet protocol is supported./n");

抱歉!评论已关闭.