C 标准库和 C POSIX 库的区别
我对“C standard lib”和“C POSIX lib”有点困惑,因为我发现,“C POSIX lib”中定义的许多头文件也是“C standard lib”的一部分。
所以,我假设,“C 标准库”是由 ANSI C 组织定义的库,并且在不同平台(Win32/Unix-like)上有不同的实现,而“C POSIX lib”只是“C 标准”的实现lib”在类 Unix 操作系统上,对吗?
但是“C POSIX lib”包含一些“C 标准lib”中未指定的头文件,例如
、
> 和
。
以
为例,我假设它的“C standard lib”对应项是
,那么如果我想写一个多线程- Linux 上的线程程序,我应该包含哪个头文件,
还是
?
I'm a little confused by "C standard lib" and "C POSIX lib", because I found that, many header files defined in "C POSIX lib" are also part of "C standard lib".
So, I assume that, "C standard lib" is a lib defined by ANSI C organization, and there are different implementation on different platforms (Win32/Unix-like), and "C POSIX lib" is just a implementation for "C standard lib" on Unix-like OSes, right?
But "C POSIX lib" contains some headers not specified in "C standard lib", such as <sys/types.h>
, <sys/wait.h>
, and <pthread.h>
.
Take <pthread.h>
as an example, I presume its "C standard lib" counterpart is <threads.h>
, then if I want to write a multi-threaded program on Linux, which header file should I include, <pthread.h>
or <threads.h>
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
POSIX 是标准 C 库的超集,值得注意的是它遵循标准 C 库。如果 C 和 POSIX 发生冲突,C 获胜。
套接字、文件描述符、共享内存等都是 POSIX 的一部分,但 C 库中不存在。
pthread.h
用于 POSIX 线程,threads.h
是 C11 的新头文件,并且是 C 库的一部分。也许将来某个时候 pthread 会被弃用,取而代之的是 C 线程,但是您可能还不能指望 C11 得到广泛部署。因此,如果您想要可移植性,您现在应该更喜欢 pthreads。如果可移植性不是问题,并且您有可用的 C11 线程,那么您可能应该使用它们。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 andthreads.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 引用
http://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap01.html#tag_14_01
我在以下位置列出了一些主要的 API 扩展:我从来没有真正理解过:什么是 POSIX?
POSIX 7 quote
http://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap01.html#tag_14_01
I have listed some major API extensions at: I never really understood: what is POSIX?
ANSI C还活着,我想:ANSI C是由ISO C、Cxx继承和扩展的。 POSIX 绝对遵守 ANSI C。”
我们可以轻松地在 Windows、类 Unix、嵌入式设备上编写 ANSI C;但是 Cxx 或 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.