C 标准库与 C POSIX 库的区别

我对“ C 标准库”和“ C POSIX 库”有点困惑,因为我发现,在“ C POSIX 库”中定义的许多头文件也是“ C 标准库”的一部分。

因此,我假设“ C 标准库”是由 ANSI C 组织定义的库,并且在不同的平台(类 Unix 的 Win32)上有不同的实现,而“ C POSIX 库”仅仅是类 Unix 操作系统上“ C 标准库”的实现,对吗?

但是“ C POSIX lib”包含一些没有在“ C 标准 lib”中指定的头,例如 <sys/types.h><sys/wait.h><pthread.h>

<pthread.h>为例,我假设它的“ C 标准库”对应物是 <threads.h>,那么如果我想在 Linux 上编写一个多线程程序,我应该包含哪个头文件,<pthread.h>还是 <threads.h>

39182 次浏览

The C POSIX library is a specification of a C standard library for POSIX systems. It was developed at the same time as the ANSI C standard. Some effort was made to make POSIX compatible with standard C; POSIX includes additional functions to those introduced in standard C.

POSIX is a superset of the standard C library, and it's important to note that it defers to it. If C and POSIX is ever in conflict, C wins.

Sockets, file descriptors, shared memory etc. are all part of POSIX, but do not exist in the C library.

pthread.h is used for POSIX threads and threads.h is a new header for C11 and is part of the C library. Perhaps pthreads will be deprecated sometime in the future in favor of the C ones, however you probably can't count on C11 to have widespread deployment yet. Therefore if you want portability you should prefer pthreads for now. If portability is not a concern, and you have C11 threads available, you should probably use those.

POSIX 7 quote

http://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap01.html#tag_14_01

1.1 Relationship to Other Formal Standards

Great care has been taken to ensure that this volume of POSIX.1-2008 is fully aligned with the following standards:

ISO C (1999) ISO/IEC 9899:1999, Programming Languages - C, including ISO/IEC 9899:1999/Cor.1:2001(E), ISO/IEC 9899:1999/Cor.2:2004(E), and ISO/IEC 9899:1999/Cor.3.

Parts of the ISO/IEC 9899:1999 standard (hereinafter referred to as the ISO C standard) are referenced to describe requirements also mandated by this volume of POSIX.1-2008. Some functions and headers included within this volume of POSIX.1-2008 have a version in the ISO C standard; in this case CX markings are added as appropriate to show where the ISO C standard has been extended (see Codes). Any conflict between this volume of POSIX.1-2008 and the ISO C standard is unintentional.

I have listed some major API extensions at: I never really understood: what is POSIX?

ANSI C is still alive, I think: ANSI C is inherited and extended by ISO C, Cxx. POSIX have been obeying ANSI C absolutely."

We can write ANSI C on Windows, Unix-Like, embedded device easily; but Cxx, or POSIX may have issue.