CMPSC 311, Introduction to Systems Programming

The POSIX Standard
The Single UNIX Specification



Reading


The Posix Standard describes the behavior of a computer system as seen through a particular set of data types, function interfaces and system utilities.
The official designation of the current standard is
Why do we need a standard for an operating system?
Do I actually need to read the Posix Standard?
POSIX or Posix?
UNIX or Unix?



See APUE Sections 2.2, 2.3, 2.4 for
There are four volumes in the Posix Standard:
To access the standard ...


Some older designations that you may see mentioned in various places,
There has also been a series of IEEE Posix standards for interfaces to the Ada and Fortran programming languages, and there is a current project (P1003.27) to produce Posix interfaces for C++.  There are related standards for Realtime and Embedded Application Support (1003.13-2003) and Device Control Application Program Interface (1003.26-2003).

Of historical interest,
Of current interest,



Version Test Macros

How can you tell which version of the Posix interface you have?
These use the symbolic constants #define'd by <unistd.h>.  There are many more of these which describe the various options of the standard.

Here is an example from the Posix Standard, which uses a compile-time version test

#if _POSIX_VERSION >= 200112L
  /* Use the newer function that copes with large files. */
  off_t pos=ftello(fp);
#else
  /* Either this is an old version of POSIX, or _POSIX_VERSION is
   * not even defined, so use the traditional function.
   */

  long pos=ftell(fp);
#endif

This example uses a runtime version test,

if (sysconf(_SC_VERSION) < 200112L) {
  fprintf(stderr, "POSIX.1-2001 system required, terminating.\n");
  exit(1);
}




Posix Vocabulary

Some features are marked legacy or obsolescent.  These are allowed by the current standard, but will probably be withdrawn in future versions.  You should avoid using these features in new programs, and if you find them in old programs (that you have the authority to rewrite) then you should replace them.
The term implementation-defined is used in the same way as in the C Standard.  In the case of some possibly-defined constants, the constant might not be defined at all, but in any case the documentation should clearly state what has been implemented or not.

The terms undefined and unspecified relate to values or behaviors associated with invalid or valid programs or data, respectively.  Portable programs should not depend on undefined or unspecified features.

There are various levels of requirements placed by the standard, depending on the verb that is used (some of this comes directly from the ISO/IEC requirements).  "Can" refers to optional application behavior.  "May" refers to optional implementation behavior.
A conforming implementation shall meet all of the following criteria:  (lightly edited from the Standard)
A conforming POSIX application must be written in C, or use only the standard utilities (it could be a shell script).

A strictly conforming POSIX application
A strictly conforming XSI application would also define _XOPEN_SOURCE to be 700 before any header is included.



Feature Test Macros

To ensure that your code uses only the standard features from Posix or XSI, compile it on Solaris as
c99 -D_POSIX_C_SOURCE=200112L prog.c
or
c99 -D_XOPEN_SOURCE=600 prog.c

The first version excludes implementation-defined definitions or limits.  The second version includes all XSI features, and also sets _POSIX_C_SOURCE to 200112L.  Use gcc -std=c99 instead of c99 on Linux or Mac OS X.

Using these two macros would be an easy way to assess how close to "maximally portable" your program might be.



Headers and data types defined in the Posix Standard

The headers marked C99 in the Option column are adopted by Posix from the C Standard, either directly or with extensions.  If an entry in the Option column is blank, that header is required by Posix but not by C99.  Refer to APUE, Fig. 2.5 for a more complete list of option codes, and which ones are required for the Single UNIX Specification; of those in this table, they are THR (threads) and XSI (X/Open System Interface).  See also, Posix Option Codes for the full list.

Header
Posix Standard description
Option
APUE
<aio.h> asynchronous input and output
AIO

<arpa/inet.h> definitions for internet operations

16.
<assert.h> verify program assertion
C99

<complex.h> complex arithmetic
C99

<cpio.h> cpio archive values
XSI

<ctype.h> character types
C99

<dirent.h> format of directory entries

4.21
<dlfcn.h> dynamic linking
XSI

<errno.h> system error numbers
C99

<fcntl.h> file control options

3.14
<fenv.h> floating-point environment
C99

<float.h> floating types
C99

<fmtmsg.h> message display structures
XSI

<fnmatch.h> filename-matching types


<ftw.h> file tree traversal
XSI
4.21
<glob.h> pathname pattern-matching types


<grp.h> group structure

6.4
<iconv.h> codeset conversion facility
XSI

<inttypes.h> fixed size integer types
C99

<iso646.h> alternative spellings
C99

<langinfo.h> language information constants
XSI

<libgen.h> definitions for pattern matching functions
XSI

<limits.h> implementation-defined constants
C99

<locale.h> category macros
C99

<math.h> mathematical declarations
C99

<monetary.h> monetary types
XSI

<mqueue.h> message queues
MSG

<ndbm.h> definitions for ndbm database operations
XSI

<net/if.h> sockets local interfaces

16.
<netdb.h> definitions for network database operations


<netinet/in.h> Internet address family

16.3
<netinet/tcp.h> definitions for the Internet Transmission Control Protocol (TCP)


<nl_types.h> data types
XSI

<poll.h> definitions for the poll( ) function
XSI
14.5.2
<pthread.h> threads
THR
11., 12.
<pwd.h> password structure

6.2
<regex.h> regular expression matching types


<sched.h> execution scheduling
PS

<search.h> search tables
XSI

<semaphore.h> semaphores
SEM

<setjmp.h> stack environment declarations
C99

<signal.h> signals
C99

<spawn.h> spawn
SPN

<stdarg.h> handle variable argument list
C99

<stdbool.h> boolean type and values
C99

<stddef.h> standard type definitions
C99

<stdint.h> integer types
C99

<stdio.h> standard buffered input/output
C99

<stdlib.h> standard library definitions
C99

<string.h> string operations
C99

<strings.h> string operations
XSI

<stropts.h> STREAMS interface
XSR
14.4
<sys/ipc.h> XSI interprocess communication access structure
XSI
15.6
<sys/mman.h> memory management declarations


<sys/msg.h> memory management declarations
XSI
15.7
<sys/resource.h> definitions for XSI resource operations
XSI
7.11
<sys/select.h> select types

14.5.1
<sys/sem.h> XSI semaphore facility
XSI
15.8
<sys/shm.h> XSI shared memory facility
XSI
15.9
<sys/socket.h> main sockets header

16.
<sys/stat.h> data returned by the stat( ) function

4.
<sys/statvfs.h> VFS File System information structure
XSI

<sys/time.h> time types
XSI

<sys/timeb.h> additional definitions for date and time
XSI

<sys/times.h> file access and modification times structure

8.16
<sys/types.h> data types

2.8
<sys/uio.h> definitions for vector I/O operations
XSI
14.7
<sys/un.h> definitions for UNIX domain sockets

17.3
<sys/utsname.h> system name structure

6.9
<sys/wait.h> declarations for waiting

8.6
<syslog.h> definitions for system error logging
XSI
13.4
<tar.h> extended tar definitions


<termios.h> define values for termios (terminal I/O)

18.
<tgmath.h> type-generic macros
C99

<time.h> time types
C99

<trace.h> tracing
TRC

<ucontext.h> user context
XSI

<ulimit.h> ulimit commands
XSI

<unistd.h> standard symbolic constants and types

various
<utime.h> access and modification times structure

4.19
<utmpx.h> user accounting database definitions
XSI

<wchar.h> wide-character handling
C99

<wctype.h> wide-character classification and mapping utilities
C99

<wordexp.h> word-expansion types





The following table is derived from the POSIX Standard, System Interfaces, Sec. 2.12, Data Types.  It extends APUE Fig. 2.20, but it is still not a complete list of all types defined in the Standard (we will enlarge the table as time permits).

Again, recall the rule that the standard is an interface, not an implementation.  The appropriate header is required to have a typedef, or struct or union declaration, which actually implements the type, and which follows rules set by the standard, but the implementation itself is not given by the standard.

Some types appear in more than one header, and most headers include other headers.  Each entry in this table lists only one header, for convenience.  For example, the pthread_t type is defined in <sys/types.h>, which is included by the more obvious choice <pthread.h>.


Header
Type
Description
<termios.h>
cc_t Type used for terminal special characters.
<sys/types.h> clock_t Integer or real-floating type used for processor times, as defined in the ISO C standard.
<sys/types.h> clockid_t Used for clock ID type in some timer functions.
<sys/types.h> dev_t Arithmetic type used for device numbers.
<dirent.h> DIR Type representing a directory stream.
<stdlib.h> div_t Structure type returned by the div() function.
<stdio.h> FILE Structure containing information about a file.
<glob.h> glob_t Structure type used in pathname pattern matching.
<stdio.h> fpos_t Type containing all information needed to specify uniquely every position within a file.
<sys/types.h> gid_t Integer type used for group IDs.
<ioconv.h> iconv_t Type used for conversion descriptors.
<sys/types.h> id_t Integer type used as a general identifier; can be used to contain at least the largest of a pid_t, uid_t, or gid_t.
<sys/types.h> ino_t Unsigned integer type used for file serial numbers.
<sys/types.h> key_t Arithmetic type used for XSI interprocess communication.
<stdlib.h> ldiv_t Structure type returned by the ldiv() function.
<sys/types.h> mode_t Integer type used for file attributes.
<mqueue.h> mqd_t Used for message queue descriptors.
<poll.h> nfds_t Integer type used for the number of file descriptors.
<sys/types.h> nlink_t Integer type used for link counts.
<sys/types.h> off_t Signed integer type used for file sizes.
<sys/types.h> pid_t Signed integer type used for process and process group IDs.
<sys/types.h> pthread_attr_t Used to identify a thread attribute object.
<sys/types.h> pthread_cond_t Used for condition variables.
<sys/types.h> pthread_condattr_t Used to identify a condition attribute object.
<sys/types.h> pthread_key_t Used for thread-specific data keys.
<sys/types.h> pthread_mutex_t Used for mutexes.
<sys/types.h> pthread_mutexattr_t Used to identify a mutex attribute object.
<sys/types.h> pthread_once_t Used for dynamic package initialization.
<sys/types.h> pthread_rwlock_t Used for read-write locks.
<sys/types.h> pthread_rwlockattr_t Used for read-write lock attributes.
<sys/types.h> pthread_t Used to identify a thread.
<stddef.h> ptrdiff_t Signed integer type of the result of subtracting two pointers.
<regex.h> regex_t Structure type used in regular expression matching.
<regex.h> regmatch_t Structure type used in regular expression matching.
<sys/resource.h> rlim_t Unsigned integer type used for limit values, to which objects of type int and off_t can be cast without loss of value.
<semaphore.h> sem_t Type used in performing semaphore operations.
<signal.h> sig_atomic_t Integer type of an object that can be accessed as an atomic entity, even in the presence of asynchronous interrupts.
<signal.h> sigset_t Integer or structure type of an object used to represent sets of signals.
<stddef.h> size_t Unsigned integer type used for size of objects.
<termios.h> speed_t Type used for terminal baud rates.
<sys/types.h> ssize_t Signed integer type used for a count of bytes or an error indication.
<sys/types.h> suseconds_t Signed integer type used for time in microseconds.
<termios.h> tcflag_t Type used for terminal modes.
<sys/types.h> time_t Integer or real-floating type used for time in seconds, as defined in the ISO C standard.
<sys/types.h> timer_t Used for timer ID returned by the timer_create() function.
<sys/types.h> uid_t Integer type used for user IDs.
<sys/types.h> useconds_t Unsigned integer type used for time in microseconds.
<stdarg.h> va_list Type used for traversing variable argument lists.
<stddef.h> wchar_t Integer type whose range of values can represent distinct codes for all members of the largest extended character set specified by the supported locales.
<wctype.h> wctype_t Scalar type which represents a character class descriptor.
<wchar.h> wint_t Integer type capable of storing any valid value of wchar_t or WEOF.
<wordexp.h> wordexp_t Structure type used in word expansion.







References, not previously mentioned
Linux



Last revised, 22 Jan. 2013