什么是 POSIX?我已经阅读了维基百科的文章 ,每次遇到这个词时我都会阅读它。事实是,我从来没有真正理解它是什么。
任何人都可以通过解释 “对 POSIX 的需求” 向我解释一下吗?
Most important things POSIX 7 defines
Greatly extends ANSI C with things like:
mkdir
, dirname
, symlink
, readlink
, link
(hardlinks), poll()
, stat
, sync
, nftw()
fork
, execl
, pipe
, semaphors sem_*
, shared memory (shm_*
), kill
, scheduling parameters (nice
, sched_*
), sleep
, mkfifo
, setpgid()
socket()
mmap
, mlock
, mprotect
, madvise
, brk()
reg*
)Those APIs also determine underlying system concepts on which they depend, e.g. fork
requires a concept of a process.
Many Linux system calls exist to implement a specific POSIX C API function and make Linux compliant, e.g. sys_write
, sys_read
, ... Many of those syscalls also have Linux-specific extensions however.
Major Linux desktop implementation: glibc, which in many cases just provides a shallow wrapper to system calls.
E.g.: cd
, ls
, echo
, ...
Many utilities are direct shell front ends for a corresponding C API function, e.g. 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.
E.g., a=b; echo "$a"
Major Linux desktop implementation: GNU Bash.
E.g.: HOME
, PATH
.
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
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. grep
accepts BREs by default, and EREs with -E
.
E.g.: 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.
E.g.: /dev/null
, /tmp
The Linux FHS greatly extends POSIX.
/
is the path separatorNUL
cannot be used.
is cwd
, ..
parenta-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, e.g. single letter flags only (e.g. -a
), no double hyphen long versions (e.g. --all
).
A few widely used conventions:
-
means stdin where a file is expected--
terminates flags, e.g. 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. Inspur's K-UX and Huawei's EulerOS are two certified examples.
官方认证系统列表可在以下网址找到: https : //www.opengroup.org/openbrand/register/ ,也可在维基页面找到 。
视窗
Windows 在其某些专业发行版上实现了 POSIX。
由于它是一个可选功能,程序员不能依赖它来支持大多数最终用户应用程序。
Windows 8 中不支持支持:
2016 年,宣布了一个名为 “Linux 子系统 Linux” 的新的类似 Linux 的官方 API。它包括 Linux 系统调用,ELF 运行,部分/proc
文件系统,Bash,GCC,(TODO 可能是 glibc?), apt-get
等等: https : //channel9.msdn.com/Events/Build/2016/P488所以我相信它将允许 Windows 运行很多(如果不是全部)POSIX。但是,它专注于开发人员 / 部署而非最终用户。特别是,没有计划允许访问 Windows GUI。
官方 Microsoft POSIX 兼容性的历史概述: http : //brianreiter.org/2010/08/24/the-sad-history-of-the-microsoft-posix-subsystem/
Cygwin是一个众所周知的 GPL 第三方项目,为 Windows“提供了大量的 POSIX API 功能”,但要求您 “从源代码重建应用程序,如果您希望它在 Windows 上运行”。 MSYS2是一个相关项目,似乎在 Cygwin 之上添加了更多功能。
Android 的
Android 拥有自己的 C 库(Bionic),它不支持 Android O 的POSIX : Android POSIX 兼容吗?
奖金水平
Linux 标准库进一步扩展了 POSIX。
使用非帧索引,它们更具可读性和可搜索性: http : //pubs.opengroup.org/onlinepubs/9699919799/nfindex.html
获取用于 grepping 的 HTML 页面的完整压缩版本: POSIX C API 函数列表在哪里?