2017-03-31 91 views
0

我在做嵌入式系統編程。 默認情況下,我們的進程被設置爲更高的優先級,但是對於像調用shell命令,寫入文件等操作。我正在考慮降低優先級,然後重新開始。所以它就像一對函數調用:「setdefaultpriority」和「提高優先級」。會改變進程優先級經常有副作用

在我們的過程中有很多shell命令調用。在一個文件中,我可能需要調用幾十對「setdefault ...」和「improve ..」

我的問題,在一個進程中有那麼多優先級操作會有什麼不好的影響?

+0

有**測量結果**表示存在性能問題?如果不是,我不會打擾。 – Jens

回答

0

setpriority在非根進程中只能上升(降低優先級),永不停止。

你可以做的是在執行shell命令之前減少子進程中的進程優先級。

//errror checks ommited 
#include <sys/resource.h> 
#include <sys/time.h> 
#include <stdio.h> 
#include <unistd.h> 
#include <assert.h> 
#include <sys/wait.h> 

int main() 
{ 
    pid_t pid; 
    pid=fork(); 
    assert(pid>=0); 
    if (!pid){ 
     execlp("nice", "nice", (char*)0); 
     _exit(1); 
    } 
    wait(0); 
    pid=fork(); 
    if (!pid){ 
     setpriority(PRIO_PROCESS, 0, 10); 
     execlp("nice", "nice", (char*)0); 
     _exit(1); 
    } 

} 
/* should print: 
    0 
    10 
*/ 

系統調用爲setpriority簡單相比,forkexec*成本可以忽略不計的性能開銷。