POSIX

共7篇文章。

errno的实现

Kernel

IEEE Std 1003.1-2024 (POSIX)中对于errno的定义如下:

The lvalue to which the macro errno expands is used by many functions to return error values.

在更早期的POSIX(Issue 5及以前)以及X/Open文档中,曾经规定errno是一个外部变量(extern int errno),但这使得errno无法实现线程安全,因为所有线程共享同一个全局变量,一个线程的系统调用返回的错误码会覆盖另一个线程的值。因此,POSIX Issue 6(即SUSv3 / IEEE Std 1003.1-2001)将这一要求删除,改为现在的定义:只要求errno是一个展开为int类型的可修改左值(modifiable lvalue)的宏。这为实现者提供了足够的自由度,以支持线程安全的errno。

ISO C标准在C90/C89时期已经不再要求errno是外部变量。

阅读全文…:errno的实现(本文约1842字,阅读大致需要4分钟)

Moving toward a standarized qsort_r(3)

Development

History of qsort_r(3)

First introduced in Research Unix V3 and eventually standardized as part of C Standard Library as of ANSI/ISO C89, qsort(3) provided an abstract interface where programmers can perform sort operation over an array of objects, by supplying a pointer to that array, the total number of the member objects, the size of individual member object,as well as a compare function. The compare function is expected to take two parameters, both pointers to a member object, and it shall return a negative number, 0, or a positive number, if the first object is considered smaller, equal, or greater respectively.

阅读全文…:Moving toward a standarized qsort_r(3)(本文约2240字,阅读大致需要11分钟)

关于闰秒

Kernel

phk老大如此吐槽:

来自X(Twitter)

背景

关于时间,A core几年前写过一个「时间是什么?」的系列:序言、续一、续二、续三。

为了兼顾天文观测获得的时间UT1(更符合普通人的需要)和由铯-133原子震荡获得的时间TAI(更稳定),将两者综合一下就获得了协调世界时UTC。UTC和UT1之间的差距永远保持在1秒以内。UTC中的一秒和TAI相同,但UTC和UT1保持最多一秒的差距,这是通过IERS在UTC事件中人为插入或减少闰秒这样的人为调整来实现的。从1972年这套系统投入运营到今天为止,总共插入了35个闰秒(2015年6月30日还将再插入一个闰秒)。

闰秒只在每年的6月30日和12月31日的UTC时间23:59:59之后插入,表示为当天的23:59:60。

阅读全文…:关于闰秒(本文约1611字,阅读大致需要4分钟)

const char ** vs char * const * vs const char * const *

Development

因为有人问到这个问题,解答一下:

  • 对于const char **p:p[i] 的值可变,而p[i][j] 的值不可变。
  • 对于char * const *p:p[i] 的值不可变,而p[i][j] 的值可变。
  • 对于const char * const *p:p[i] 的值不可变,p[i][j] 的值也不可变。

由是可见,第三种对于接收参数的一方而言,是最为严厉的限制。在设计接口时,这类限制有助于减少程序中的潜在问题,但却会使使用现有接口的一些程序出现错误。许多公开的标准,特别是POSIX,往往会出于这种考虑而选择char * const *。在设计程序时,对于这种char * const *,应当作const char * const * 看待,而不应abuse其p[i][j] 可写的特性。

参与评论:const char ** vs char * const * vs const char * const *

Linux threading bug

Kernel

A collegue of mine has reported a FreeBSD “bug” that, when the main thread exits, the whole process is terminated. He complained that pthread_detach() would not cause the thread to run without the main thread, while Linux did.

With some experiments we have figured out that this is actually a Linux bug, which violates POSIX. Interestingly, Solaris has the same behavior that FreeBSD have.

The POSIX pthread_create() said:

Note that the thread in which main() was originally invokeddiffers from this. When it returns from main(), the effectshall be as if there was an implicit call to exit() usingthe return value of main() as the exit status."

阅读全文…:Linux threading bug(本文约161字,阅读大致需要1分钟)
搜索文章…(按ESC退出)