如何使CPU占用率为50%
生活随笔
收集整理的這篇文章主要介紹了
如何使CPU占用率为50%
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在Linux下,CPU的狀態分為系統態,用戶態和空閑態,分別指系統內核執行時間,處于用戶態的時間和空閑系統進程執行的時間。三者之和就是CPU的總時間。
CPU的利用率就是非空閑進程占用時間的比例。
?
100%:
1 #include <stdlib.h> 2 int main(){ 3 while(1){ 4 for(int i = 0 ;i<100;i++); 5 } 6 return 0; 7 }?
50%:
1 #include <unistd.h> 2 #include <stdlib.h> 3 #include <stdio.h> 4 #include <time.h> 5 int main(){ 6 int time_start; 7 int fulltime = 100;//總時間 8 int runtime = 50;//運行時間 9 while(1){ 10 time_start = clock(); 11 while((clock()-time_start)<runtime){} 12 usleep(fulltime-runtime); 13 } 14 return 0; 15 }?
轉載于:https://www.cnblogs.com/Kiven5197/p/8979240.html
總結
以上是生活随笔為你收集整理的如何使CPU占用率为50%的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 小米7的传闻:这是我们迄今为止知道的
- 下一篇: 无法找到“XXX.exe”的调试信息,或