协慌网

登录 贡献 社区

“POSIX” 是什么意思?

什么是 POSIX?我已经阅读了维基百科的文章 ,每次遇到这个词时我都会阅读它。事实是,我从来没有真正理解它是什么。

任何人都可以通过解释 “对 POSIX 的需求” 向我解释一下吗?

答案

POSIX是由IEEE指定的一系列标准,用于澄清和统一 Unix-y 操作系统提供的应用程序编程接口(以及辅助问题,如命令行 shell 实用程序)。当您编写程序以依赖 POSIX 标准时,您可以非常肯定能够轻松地将它们移植到大量的 Unix 衍生产品系列中(包括 Linux,但不仅限于它!); 如果你使用的某些 Linux API 没有标准化为 Posix 的一部分,那么如果你希望将来将该程序或库移植到其他 Unix-y 系统(例如 MacOSX),你将会遇到更难的时间。

Most important things POSIX 7 defines

  1. C API

    Greatly extends ANSI C with things like:

    • more file operations: mkdir, dirname, symlink, readlink, link (hardlinks), poll(), stat, sync, nftw()
    • process and threads: fork, execl, pipe, semaphors sem_*, shared memory (shm_*), kill, scheduling parameters (nice, sched_*), sleep, mkfifo, setpgid()
    • networking: socket()
    • memory management: mmap, mlock, mprotect, madvise, brk()
    • utilities: regular expressions (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.

  2. CLI utilities

    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.

  3. Shell language

    E.g., a=b; echo "$a"

    Major Linux desktop implementation: GNU Bash.

  4. Environment variables

    E.g.: HOME, PATH.

  5. Program exit status

    ANSI C says 0 or EXIT_SUCCESS for success, EXIT_FAILURE for failure, and leaves the rest implementation defined.

    POSIX adds:

  6. 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, 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.

  7. Directory struture

    E.g.: /dev/null, /tmp

    The Linux FHS greatly extends POSIX.

  8. Filenames

    • / is the path separator
    • NUL cannot be used
    • . is cwd, .. parent
    • portable filenames
      • use at most max 14 chars and 256 for the full path
      • can only contain: a-zA-Z0-9._-

    See also: what is posix compliance for filesystem?

  9. 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:

  • 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. 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 的POSIXAndroid POSIX 兼容吗?

奖金水平

Linux 标准库进一步扩展了 POSIX。

使用非帧索引,它们更具可读性和可搜索性: http//pubs.opengroup.org/onlinepubs/9699919799/nfindex.html

获取用于 grepping 的 HTML 页面的完整压缩版本: POSIX C API 函数列表在哪里?

POSIX是:

POSIX(发音为 / pɒzɪks/)或 “便携式操作系统接口 [用于 Unix]” 1是 IEEE 指定的一系列相关标准的名称,用于定义应用程序编程接口(API),以及用于软件的 shell 和实用程序接口虽然该标准适用于任何操作系统,但与 Unix 操作系统的变体兼容。

基本上,它是一组措施,通过(大多数)常见的 API 和实用程序来缓解开发和使用不同版本 UNIX 的痛苦。有限的 POSIX 合规性也扩展到各种版本的 Windows。