Posix
Posix
POSIX
2 .POSIX (Portable Operating System Interface)
2.1. What Is POSIX?
POSIX stands for Portable Operating System Interface. It’s a family of standards specified by
IEEE for maintaining compatibility among operating systems. Therefore, any software that
conforms to POSIX standards should be compatible with other operating systems that
adhere to the POSIX standards.
For that reason, most of the tools we use on Linux and Unix-like operating systems behave
almost the same. For example, if we use the ps command, it should behave the same under
OpenBSD, Debian, and macOS.
To overcome this problem, POSIX was born. POSIX is the standardization of the original
UNIX, which came back in 1988 to resolve issues not only between different UNIX
variants but also Non-UNIX operating systems as well.
The work on POSIX began in the early 1980s to standardized the rapidly developing UNIX
system interface. It covers a variety of systems in concise terms. The important POSIX.1
standard became the internationally accepted standard ISO/IEC 9945-1:1990 while the
POSIX.2 standard was internationally accepted as IEEE Std 1003.2-1992.
POSIX 1003.1 is the base standard upon which the POSIX family of standards has been
developed, and there are currently more than 20 standards and drafts under the POSIX
umbrella.
POSIX.1 defines application portability, as well as the C interface and the behavior of system
services for fundamental tasks such as process creation and termination, process
environment manipulation, file and directory access, and simple I/O.
On the other hand, POSIX.2 describes the command interpreter, portable shell
programming, user environment, and related utilities. Both standards are strongly influenced
by existing UNIX practice and experience.
The POSIX C API adds more functions on top of the ANSI C Standard for a number of
aspects:
File operations
Processes, threads, shared memory, and scheduling parameters
Networking
Memory management
Regular expressions
As an example, let’s suppose we want to output a string that contains today’s date. We’ll use
the printf utility because it follows the POSIX file format standard:
$ printf "Today's Date: %d %s, %d" 18 September 2021
Today's Date: 18 September, 2021
The format specifies three conversion specifications: %d, %s, and %d. The printf utility
processes these conversion specifications and substitutes them with the arguments.
For each environment variable, the value should only be a string of characters as
defined in the portable character set. For instance, we can define the environment
variable for our base user directory in the form of:
XDG_BASE_DIRECTORY="/home/user/"
We can name the environment variables however we like, although we should avoid
defining our own environment variables with names that conflict with the environment
variables of standard utilities. Additionally, our conforming implementation should be case-
sensitive to environment variable names. For instance, the names home and Home are two
different environment variables.
2.3.5. Locale
A locale defines the language and cultural convention that is used in the user
environment. Each locale consists of categories that define the behavior of software
components, such as date-time formatting, monetary formatting, and numeric formatting.
A program implementation shall conform to the POSIX locale, which is identical to the C
locale. The program implementation should make use of the currently defined locale
environment variables to retain coherence. If there is no locale set, then the
implementation should specify its POSIX compliant locale.
The POSIX standard defines the following environment variables for each category:
POSIX recommends that our implementation should contain at least one character set and
a portable character set. The first eight entries in the character set shall be control
characters. The POSIX locale should include at least 256 characters from both portable and
non-portable character sets.
POSIX conforming implementations can make use of Basic Regular Expressions (BRE)
or Extended Regular Expressions (ERE). BRE provides basic notations for searching
text, while ERE supports more notations. Most POSIX conforming utilities rely heavily on
BRE, although advanced text manipulation utilities also support ERE.
BRE and ERE should operate on a string of characters that ends with a NUL character.
The literal escape sequence and newline character produce an undefined result.
Therefore, our programs should treat them as ordinary characters.
POSIX does not permit the use of an explicit NUL character in the REs or the text to be
matched.
Our implementation should be able to perform a case-insensitive search by default.
The length of our REs should not exceed 256 bytes in length.
2.3.8. Directory Structure
Most major Linux distributions conform to the Filesystem Hierarchy Standard (FHS). FHS
defines a configurable tree-like directory structure. The first directory in the hierarchy is
the root directory, and all the other directories, files, and special files branch out from it.
The tree utility can give us a better look at the directory structure:
$ tree / -d -L 1
/
├── bin -> usr/bin
├── boot
├── dev
├── etc
├── home
├── lib64 -> usr/lib
├── mnt
├── opt
├── proc
├── root
├── run
├── sbin -> usr/bin
├── sys
├── usr
└── var
While the hierarchy is configurable, our program shouldn’t create files or directories
under the root and /dev directories. However, it allows for creating files and directories
under any user directory, such as the XDG Base Directory. Additionally, we can also use
the /tmp directory to create temporary files and directories.
2.3.9. Utilities
When we get familiar with the utilities in the UNIX/Linux environment, we can see that most
utilities behave the same. For instance, we know that the -h option prints a help text for
almost every UNIX/Linux utility. This consistency owes to the conventions described by
POSIX. POSIX defines several conventions for programmers about how we should
implement our utility programs.
POSIX recommends that we implement the following argument syntax in our utility
programs:
Additionally, we can omit the brackets for the options that are required by the utility. As for
complex utilities with a lot of options, we can group the options:
Apart from the utility syntax, POSIX encourages us to use a set of guidelines when
implementing utilities.
2.4. Linux
It’s certainly possible to create a Linux-based operating system that is entirely POSIX
compliant. EulerOS is a good example of that. However, most modern programs,
especially closed-source software, conform to the standard either partially or not at
all.
As an example, the bash shell used to be completely POSIX compliant. The recent versions
of bash, however, don’t conform to the POSIX standard by default. So, one can say that
most Linux distributions are partially POSIX-compliant.