I never really understood: what is POSIX?
What is POSIX? I have read the Wikipedia article and I read it every time I encounter the term. Fact is that I never really understood what it is.
Can anyone please explain it to me by explaining "the need for POSIX" too?
POSIX is a family of standards, specified by the IEEE, to clarify and make uniform the application programming interfaces (and ancillary issues, such as commandline shell utilities) provided by Unix-y operating systems. When you write your programs to rely on POSIX standards, you can be pretty sure to be able to port them easily among a large family of Unix derivatives (including Linux, but not limited to it!); if and when you use some Linux API that's not standardized as part of Posix, you will have a harder time if and when you want to port that program or library to other Unix-y systems (eg, MacOSX) in the future.
Most important things POSIX 7 defines
C API
Greatly extends ANSI C with things like:
mkdir
, dirname
, symlink
, readlink
, link
(hardlinks), poll
, sync
fork
, execl
, pipe
, semaphors sem_*
, shared memory ( shm_*
), kill
, scheduling parameters ( nice
, sched_
), sleep
mmap
, mlock
, mprotect
, madvise
Those APIs also determine underlying system concepts on which they depend, eg fork
requires a concept of a process.
Many Linux system calls exist to implement a specific POSIX C API function and make Linux compliant, eg sys_write
, sys_read
, ...
Major Linux desktop implementation: glibc, which in many cases just provides a shallow wrapper to system calls.
CLI utilities
Eg: cd
, ls
, echo
, ...
Many utilities are direct shell front ends for a corresponding C API function, eg mkdir
.
Major Linux desktop implementation: GNU Coreutils for the small ones, separate GNU projects for the big ones: sed
, grep
, awk
, ... Some CLI utilities are implemented by Bash as built-ins.
Shell language
Eg, a=b; echo "$a"
a=b; echo "$a"
Major Linux desktop implementation: GNU Bash.
Environment variables
Eg: HOME
, PATH
.
Program exit status
ANSI C says 0
or EXIT_SUCCESS
for success, EXIT_FAILURE
for failure, and leaves the rest implementation defined.
POSIX adds:
126
: command found but not executable.
127
: command not found.
> 128
: terminated by a signal.
But POSIX does not seem to specify the 128 + SIGNAL_ID
rule used by Bash: https://unix.stackexchange.com/questions/99112/default-exit-code-when-process-is-terminated
Regular expression
There are two types: BRE (Basic) and ERE (Extended). Basic is deprecated and only kept to not break APIs.
Those are implemented by C API functions, and used throughout CLI utilities, eg grep
accepts BREs by default, and EREs with -E
.
Eg: echo 'a.1' | grep -E 'a.[[:digit:]]'
echo 'a.1' | grep -E 'a.[[:digit:]]'
Major Linux implementation: glibc implements the functions under regex.h which programs like grep
can use as backend.
Directory struture
Eg: /dev/null
, /tmp
The Linux FHS greatly extends POSIX.
Filenames
/
is the path separator NUL
cannot be used .
is cwd
, ..
parent a-zA-Z0-9._-
See also: what is posix compliance for filesystem?
Command line utility API conventions
Not mandatory, used by POSIX, but almost nowhere else, notably not in GNU. But true, it is too restrictive, eg single letter flags only (eg -a
), no double hyphen long versions (eg --all
).
A few widely used conventions:
-
means stdin where a file is expected --
terminates flags, eg ls -- -l
to list a directory named -l
See also: Are there standards for Linux command line switches and arguments?
Who conforms to POSIX?
Many systems follow POSIX closely, but few are actually certified by the Open Group which maintains the standard. Notable certified ones include:
Most Linux distros are very compliant, but not certified because they don't want to pay the compliance check.
See the wiki page.
Windows
Windows implemented POSIX on some of its professional distributions.
Since it was an optional feature, programmers could not rely on it for most end user applications.
Support was deprecated in Windows 8:
In 2016 a new official Linux-like API called "Windows Subsystem for Linux" was announced. It includes Linux system calls, ELF running, parts of the /proc
filesystem, Bash, GCC, (TODO likely glibc?), apt-get
and more: https://channel9.msdn.com/Events/Build/2016/P488 so I believe that it will allow Windows to run much, if not all, of POSIX. However, it is focused on developers / deployment instead of end users. In particular, there were no plans to allow access to the Windows GUI.
Historical overview of the official Microsoft POSIX compatibility: http://brianreiter.org/2010/08/24/the-sad-history-of-the-microsoft-posix-subsystem/
Cygwin is a well known GPL third-party project for that "provides substantial POSIX API functionality" for Windows, but requires that you "rebuild your application from source if you want it to run on Windows". MSYS2 is a related project that seems to add more functionality on top of Cygwin.
Android
Android has its own C library (Bionic) which does not fully support POSIX as of Android O: Is Android POSIX-compatible?
Bonus level
The Linux Standard Base further extends POSIX.
Use the non-frames indexes, they are much more readable and searchable: http://pubs.opengroup.org/onlinepubs/9699919799/nfindex.html
Get a full zipped version of the HTML pages for grepping: Is there a listing of the POSIX API / functions?
POSIX is:
POSIX (pronounced /ˈpɒzɪks/) or "Portable Operating System Interface [for Unix]"1 is the name of a family of related standards specified by the IEEE to define the application programming interface (API), along with shell and utilities interfaces for software compatible with variants of the Unix operating system, although the standard can apply to any operating system.
Basically it was a set of measures to ease the pain of development and usage of different flavours of UNIX by having a (mostly) common API and utilities. Limited POSIX compliance also extended to various versions of Windows.
链接地址: http://www.djcxy.com/p/22910.html上一篇: “下游”和“上游”的定义
下一篇: 我从来没有真正理解:什么是POSIX?