linux的sleep()和usleep()的使用和區別__linux

來源:互聯網
上載者:User
函數名: sleep
標頭檔: #include <windows.h> // 在VC中使用帶上標頭檔
        #include <unistd.h>  // 在gcc編譯器中,使用的標頭檔因gcc版本的不同而不同
功  能: 執行掛起指定的秒數
語  法: unsigned sleep(unsigned seconds);


樣本:
#include <windows.h>
#include<stdio.h>
int main()
{
  int a;
  a=1;
  printf("hello");
  sleep(a);        /* VC 使用Sleep*/
  printf("world"); 
  return 0;
}


函數名: usleep
標頭檔: #include <unistd.h>
功  能: usleep功能把進程掛起一段時間, 單位是微秒(百萬分之一秒);
語  法: void usleep(int micro_seconds);
傳回值: 無
內容說明:本函數可暫時使程式停止執行。參數 micro_seconds 為要暫停微秒數(us)。
注 意:
這個函數不能工作在windows 作業系統中。用在Linux的測試環境下面。
參 見:usleep() 與sleep()類似,用於延遲掛起進程。進程被掛起放到reday queue。
是一般情況下,延遲時間數量級是秒的時候,儘可能使用sleep()函數。
如果延遲時間為幾十毫秒(1ms = 1000us),或者更小,儘可能使用usleep()函數。這樣才能最佳的利用CPU時間




時鐘換算:
微秒,時間單位,符號us(英語:microsecond ).
1微秒等於百萬分之一秒(10的負6 次方秒)
0.000 001 微秒 = 1皮秒
0.001 微秒 = 1納秒
1,000 微秒 = 1毫秒
1,000,000 微秒 = 1秒
1s = 1000ms
1ms = 1000μs
1μs = 1000ns
1ns = 1000ps
1秒(s) = 1000 毫秒(ms) = 1,000,000 微秒(μs) = 1,000,000,000 納秒(ns) = 1,000,000,000,000 皮秒(ps)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.