C 标准库和 C POSIX 库的区别

发布于 2025-01-07 06:06:10 字数 604 浏览 5 评论 0原文

我对“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

和我恋爱吧 2025-01-14 06:06:10

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

清眉祭 2025-01-14 06:06:10

POSIX 7 引用

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

1.1 与其他正式标准的关系

我们非常谨慎地确保本卷 POSIX.1-2008 完全符合以下标准:

ISO C (1999)
ISO/IEC 9899:1999,编程语言 - C,包括 ISO/IEC 9899:1999/Cor.1:2001(E)、ISO/IEC 9899:1999/Cor.2:2004(E) 和 ISO/IEC 9899 :1999/Cor.3。

引用了 ISO/IEC 9899:1999 标准(以下简称 ISO C 标准)的部分内容来描述 POSIX.1-2008 本卷规定的要求。 POSIX.1-2008 本卷中包含的一些函数和标头在 ISO C 标准中有一个版本;在这种情况下,会酌情添加 CX 标记,以显示 ISO C 标准已扩展的位置(请参阅代码)。本卷 POSIX.1-2008 与 ISO C 标准之间的任何冲突都是无意的。

我在以下位置列出了一些主要的 API 扩展:我从来没有真正理解过:什么是 POSIX?

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?

网名女生简单气质 2025-01-14 06:06:10

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文