1. 程式人生 > >time, ctime, sleep, exit等Linux系統呼叫使用方法

time, ctime, sleep, exit等Linux系統呼叫使用方法

作業系統實驗時候整理的一些知識點。有小錯請見諒哦。

(1)Linux中time命令是用來計算某個程式的執行耗時(real),使用者態cpu耗時(user),系統態cpu耗時(sys)。

(2)time命令最常用的使用方式就是在其後面直接跟上命令和引數:time <command> [<arguments...>]。

 (3)函式:ctime ;功能:把日期和時間轉換為字串;用法: char *ctime(const time_t *time)。

(4)sleep:將程序掛起,不再佔用CPU(自動放棄CPU)。在linux裡,sleep 的單位是秒,即sleep 1000就是睡眠了1000 秒,而在windows中,如果呼叫sleep函式單位是毫秒,sleep(1000)則是睡眠了1秒。

(5)exit:exit命令同於退出shell,並返回給定值。在shell指令碼中可以終止當前指令碼執行。執行exit可使shell以指定的狀態值退出。若不設定狀態值引數,則shell以預設值退出。狀態值0代表執行成功,其他值代表執行失敗。語法:exit(引數)。

·除錯方法描述:(各種呼叫的除錯原始碼)

 time:

#include<stdio.h>

#include<unistd.h>

#include<time.h>

#include<iostream>

using namespace std;

int main()

{

 while(1)

  {

   char timebuf[100];

   time_t t;

   time(&t);

   strftime(timebuf,sizeof(timebuf),"%Y年%m月 %d 日 %H:%M:%S",localtime(&t));

   cout<<timebuf<<endl;

   fflush(stdout);

   sleep(1);

  }

  return 0;

}

② ctime:

#include<time.h>

#include<stdio.h>

void main()

{

  time_t timep;

  time (&timep);

  printf("%s",ctime(&timep));

}

sleep:

#include<unistd.h>

#include<stdio.h>

int main()

{

 int a;

 a = 1;

 printf("Hello");

 sleep(a);

 printf("  world\n");

 return 0;

}

④ exit:

#include<stdio.h>

#include<stdlib.h>

int main()

{

 printf("Hello\n");

 exit(0);

}