张芷铭的个人博客

POSIX(Portable Operating System Interface)是 IEEE 制定的类 Unix 操作系统标准,实现软件源码级可移植。

三层含义

系统标准层

规定操作系统必须提供的统一接口:

接口类型示例
系统调用fork、open、read、write
进程/线程模型pthread_create、pthread_join
IPC 机制信号、管道、套接字
文件系统权限模型、用户组

POSIX-compliant 系统:Linux、macOS、FreeBSD、Solaris、AIX

Windows 兼容层:WSL、MinGW、Cygwin

路径格式层

Python pathlib.Path.as_posix() 对应此层:

特性POSIXWindows
路径分隔符/\
根路径/C:\
盘符
大小写敏感

示例

  • POSIX:/home/user/data.txt
  • Windows:C:\Users\data.txt

Shell 与工具层

规定标准 Shell 和命令行工具行为:

  • Shell 语法(Bash 基本兼容 POSIX sh)
  • 标准工具:ls、cp、mv、grep、sed、awk

Python 应用

1
2
3
4
5
from pathlib import Path

# 转换为 POSIX 格式路径字符串
path = Path("C:\\Users\\data.txt")
path.as_posix()  # 'C:/Users/data.txt'

跨平台代码优先使用 POSIX 路径格式,避免平台差异问题。

Comments