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系統程式設計 第二版
196
|
第六章
發生錯誤時,
nice()
會傳回
-1
。然而,因為
nice()
會傳回新的友善值,
-1
也是執行成
功時的傳回值。為了有所區別,調用
nice()
之前,你可以先把
errno
清為零,並在事後
檢查它的值。例如:
int ret;
errno = 0;
ret = nice (10); /* 把我們的友善值調高 10 */
if (ret ==
1 && errno != 0)
perror ("nice");
else
printf ("nice value is now %d\n", ret);
發生錯誤時,Linux 只會傳回單一錯誤碼:
EPERM
,這表示進行調用之行程調高了它的優
先權(經由一個負的
inc
值),但是它並不具備
CAP_SYS_NICE
的能力。當放入
inc
的友善
值逾越有效值的範圍時,有的系統還會傳回
EINVAL
,但是 Linux 不會。事實上,有需要
時,Linux 會把無效的
inc
值自動調整至此限制所允許的範圍內。
只需要把
inc
設為
0
,就可以輕易取得當前的友善值:
printf ("nice value is currently %d\n", nice (0));
往往,一個行程會想要設定一個絕對的友善值,而不是設定一個相對增加的友善值。如
下的程式碼可以完成此事:
int ret, val;
/* 取得當前的友善值 */
val = nice (0);
/* 我們想要設定 10 的友善值 */
val = 10 - val;
errno = 0;
ret = nice (val);
if ...
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