Getting started with POSIX

Download posix eBook

Versions

VersionStandardRelease Year
POSIX.1IEEE Std 1003.1-19881988-01-01
POSIX.1bIEEE Std 1003.1b-19931993-01-01
POSIX.1cIEEE Std 1003.1c-19951995-01-01
POSIX.2IEEE Std 1003.2-19921992-01-01
POSIX.1-2001IEEE Std 1003.1-20012001-12-06
POSIX.1-2004IEEE Std 1003.1-20042004-01-01
POSIX.1-2008IEEE Std 1003.1-2008 (aka "Base Specifications, Issue 7")2008-12-01
POSIX.1-2013IEEE Std 1003.1-20132013-04-19
POSIX.1-2016IEEE Std 1003.1-20162016-09-30

What is POSIX?

POSIX stands for "Portable Operating System Interface" and defines a set of standards to provide compatibility between different computing platforms. The current version of the standard is IEEE 1003.1 2016 and can be accessed from the OpenGroup POSIX specification. Previous versions include POSIX 2004 and POSIX 1997. The POSIX 2016 edition is essentially POSIX 2008 plus errata (there was a POSIX 2013 release too).

POSIX defines various tools interfaces, commands and APIs for UNIX-like operating systems and others.

The following are considered to be within the scope of POSIX standardization:

  • System interface (functions, macros and external variables)
  • Command interpreter, or Shell (the sh utility)
  • Utilities (such as more, cat, ls)

Outside of POSIX scope:

  • DBMS Interfaces
  • Graphical Interfaces
  • Binary code portability

Hello World

A simple Hello, World program without error checking:

#include <unistd.h> /* For write() and STDOUT_FILENO */
#include <stdlib.h> /* For EXIT_SUCCESS and EXIT_FAILURE */

int main(void) {
        char hello[] = "Hello, World\n";
        
        /* Attempt to write `hello` to standard output file */
        write(STDOUT_FILENO, hello, sizeof(hello) - 1);

        return EXIT_SUCCESS; 
}
 

And with error checking:

#include <unistd.h> /* For write() and STDOUT_FILENO */
#include <stdlib.h> /* For EXIT_SUCCESS and EXIT_FAILURE */

int main(void) {
        char hello[] = "Hello, World\n";
        ssize_t ret = 0;
        
        /* Attempt to write `hello` to standard output file */
        ret = write(STDOUT_FILENO, hello, sizeof(hello) - 1);

        if (ret == -1) {
                /* write() failed. */
                return EXIT_FAILURE;
        } else if (ret != sizeof(hello) - 1) {
                /* Not all bytes of `hello` were written. */
                return EXIT_FAILURE;
        }

        return EXIT_SUCCESS; 
}
 

Compiling and running

If the code shown above (either version) is stored in file hello.c , then you can compile the code into a program hello using either c99 or make . For example, in a strictly POSIX compliant mode, you might in theory compile and run the program using:

$ make hello
c99 -o hello hello.c
$ ./hello
Hello, World
$
 

Most actual make implementations will use a different C compiler (perhaps cc , perhaps gcc , clang , xlc or some other name), and many will use more options to the compiler. Clearly, you could simply type the command that make executes directly on the command line.

Stats

87 Contributors: 7
Tuesday, March 14, 2017
Licensed under: CC-BY-SA

Not affiliated with Stack Overflow
Rip Tutorial: info@zzzprojects.com

Download eBook