Skip to Content
LINUX系統程式設計 第二版
book

LINUX系統程式設計 第二版

by Robert Love
December 2013
Intermediate to advanced
496 pages
8h 57m
Chinese
GoTop Information, Inc.
Content preview from LINUX系統程式設計 第二版
介紹與基本概念
|
25
對某些函式而言,整個系列的傳回型別都是合法的傳回值。因此,調用此類函式之前,
必須把
errno
的值設為零,並於事後進行檢查(此類函式只會在實際發生錯誤時傳回非
零值的
error
)。例如:
errno = 0;
arg = strtoul (buf, NULL, 0);
if (errno)
perror ("strtoul");
檢查
errno
時常犯的錯誤,就是忘記任何程式庫或系統呼叫都可以改變它。例如,下面
這段有瑕疵的程式碼:
if (fsync (fd) ==
1) {
fprintf (stderr, "fsync failed!\n");
if (errno == EIO)
fprintf (stderr, "I/O error on %d!\n", fd);
}
要讓
errno
的值得以跨越函式調用,你必須保存它:
if (fsync (fd) ==
1) {
const int err = errno;
fprintf (stderr, "fsync failed: %s\n", strerror (errno));
if (err == EIO) {
/* 如果錯誤與 I/O 有關,棄船 */
fprintf (stderr, "I/O error on %d!\n", fd);
exit (EXIT_FAILURE);
}
}
在單執行緒的程式中,
errno
是個全域變數,正如本節稍早所述。然而,在多執行緒的
程式中,
errno
會按每個執行緒來儲存,因此具執行緒安全性。 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

優雅的SciPy|Python科學研究的美學

優雅的SciPy|Python科學研究的美學

Juan Nunez-Iglesias, Stéfan van der Walt, Harriet Dashnow
C++语言导学(原书第2版)

C++语言导学(原书第2版)

本贾尼 斯特劳斯特鲁普

Publisher Resources

ISBN: 9789862769812