1.原始问题：I never really understood: what is POSIX?
What is POSIX? I read the Wikipedia article and I read it ever 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 (e.g., MacOSX) in the future.
Most important things POSIX 7 defines
Greatly extends ANSI C with things like:
- more file operations:
- process and threads:
sem_*, shared memory (
kill, scheduling parameters (
- regular expressions
- advanced memory management:
Those APIs also determine underlying system concepts on which they depend, e.g.
forkrequires a concept of a process.
Many Linux system calls exist to implement a specific POSIX C API function and make Linux compliant, e.g.
Major Linux desktop implementation: glibc, which in many cases just provides a shallow wrapper to system calls.
- more file operations:
Many utilities are direct shell front ends for a corresponding C API function, e.g.
Major Linux desktop implementation: GNU Coreutils for the small ones, separate GNU projects for the big ones:
awk, … Some CLI utilities are implemented by Bash as built-ins.
a=b; echo "$a"
Major Linux desktop implementation: GNU Bash.
ANSI C says
EXIT_FAILUREfor failure, and leaves the rest implementation defined.
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_IDrule used by Bash: https://unix.stackexchange.com/questions/99112/default-exit-code-when-process-is-terminated
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, e.g.
grepaccepts BREs by default, and EREs with
echo 'a.1' | grep -E 'a.[[:digit:]]'
Major Linux implementation: glibc implements the functions under regex.h which programs like
grepcan use as backend.
The Linux FHS greatly extends POSIX.
/is the path separator
NULcannot be used
- portable filenames
- use at most max 14 chars and 256 for the full path
- can only contain:
See also: what is posix compliance for filesystem?
Not mandatory, used by POSIX, but almost nowhere else, notably not in GNU. But true, it is too restrictive, e.g. single letter flags only (e.g.
-a), no double hyphen long versions (e.g.
A few widely used conventions:
-means stdin where a file is expected
--terminates flags, e.g.
ls -- -lto list a directory named
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:
- OS X (Apple) X stands for both 10 and UNIX. Was the first Apple POSIX system, released circa 2001. See also: Is OSX a POSIX OS?
- AIX (IBM)
- HP-UX (HP)
- Solaris (Oracle)
Most Linux distros are very compliant, but not certified because they don’t want to pay the compliance check.
See the wiki page.
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:
- Where does Microsoft Windows' 7 POSIX implementation currently stand?
- Feature request: https://windows.uservoice.com/forums/265757-windows-feature-suggestions/suggestions/6573649-full-posix-support
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.
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 more as an OS, it is an “OS standard”. You can imagine it as an imaginary OS, which actually doesn’t exist but it has a documentation. These papers are the “posix standard”, defined by the IEEE, which is the big standard organization of the USA.
The OSes implementing this specification are “Posix-compliant”.
Government regulations prefer Posix-compliant solutions in their investments, thus being Posix-compliant has a significant financial advantage, particularly for the big IT companies of the USA.
The reward for an OS being fully posix compliant, that it is a guarantee that it will compile and run all Posix-compliant applications seamlessly.
Linux is the most well-known one. OSX, Solaris, NetBSD and Windows NT play here as well. Free- and OpenBSD are only “nearly” Posix-compliant. The posix-compliance of the WinNT is only a pseudo-solution to avoid this government regulation above.
Posix governs interoperability, portability, and in other areas such as the usage and mechanism of fork, permissions
and filesystem standards such as /etc, /var, /usr and so on . Hence, when developers write a program under a Posix compliant system such as for example Linux, it is generally, not always, guaranteed to run on another posix compliant system such as IBM’s AIX system or other commercial variants of Unix. Posix is a good thing to have as such it eases the software development for maximum portability which it strives for. Hope this answer makes sense.
Thanks to Jed Smith & Tinkertim for pointing out my error – my bad!!! 🙁
POSIX defines set of standards for an operating system or a program.
The goal is to write new software that is compatible with UNIX-like systems.
For example a program runs on Linux is also can be compile and run on other UNIX-like systems like Solaris, HP-UX, and AIX etc..
The most popular examples are
GNU Bash which is 100% POSIX compliance and