1. 程式人生 > >linux系統程式設計(一)--時間與錯誤處理

linux系統程式設計(一)--時間與錯誤處理

#inlcude<error.h>

 errno   

perror();  //列印錯誤
strerror   //列印錯誤

#inlcude<time.h>


//使用CLOCKS_PER_SEC  //常量可以得到秒數

clock_t  \\ typedef long clock_t;

clock_t  clock(void) //只是檢測處理器的時間,如果睡眠不算。即程序佔用cpu時間。    


clock_t clk_start ,clk_end;

clk_start = clock();

.........
clk_end = clock();

int clk = clk_end - clk_start;

---------------------------------------------------------------------
time_t  //日曆時間   typedef long  time_t

time_t time(time_t *tloc);//引數可以為NULL,     srand(time(NULL)); //設定隨機因子 rand();
time_t t1,t2;
t1 = time (&t2);
t1 = t2;

-----------------------------------
char* ctime(&t1);             //把time_t轉為一個字串


int stime(time_t *t);
  
t1 =time(NULL);   //獲取時間
t1 +=(24*60*60)UL; //增加時間

stime(&t1);    //設定時間

printf...   ctime(&t1);  //列印時間

struct tm  //分解時間


           struct tm {
               int tm_sec;    /* Seconds (0-60) */
               int tm_min;    /* Minutes (0-59) */
               int tm_hour;   /* Hours (0-23) */
               int tm_mday;   /* Day of the month (1-31) */
               int tm_mon;    /* Month (0-11) */
               int tm_year;   /* Year - 1900 */
               int tm_wday;   /* Day of the week (0-6, Sunday = 0) */
               int tm_yday;   /* Day in the year (0-365, 1 Jan = 0) */
               int tm_isdst;  /* Daylight saving time */
           };



//------------------------核心 定時器也有個時間結構體   struct timer_list  
//定一個定時器
struct timer_list my_timer;

//初始化定時器
void init_timer(struct timer_list *timer);
mytimer.function = my_function;
mytimer.expires = jiffies +HZ;

//增加定時器
void add_timer(struct timer_list *timer);

//刪除定時器
int del_tiemr(struct timer_list *timer);






//----------time_t ----------->>>>>>>>struct tm   獲取真實的世界時間
struct tm gmtime(time_t *timep)


struct tm localtime(time_t *timep)

//-  struct tm ----->>>>>  字串      time_t---------->字串

 char * asctime(struct tm *tm);              char *ctime(time_t *t);

//-------struct tm  ---->time_t 

mktime(struct tm *tm)


-----------------------------------------------
struct timeval    //時間要求較高
struct timezone   //時區結構體

struct timeval tv;

if(-1 = gettimeofdate(&tv ,NULL))
{
    perror("獲取時間錯誤");
    return -1;
}

settimeofdate(&tv,NULL)

/---------------------------------------------------
struct timespec  
sleep  usleep  nanosleep  


struct timespec tsp={2,200000},rem;   //rem  剩餘時間,因為中斷打斷
nanosleep(&tsp ,&rem);//

while(-1 == nanosleep(&tsp ,&rem)&&(errno==EINTR))
{

    tsp = rem;
    rem.tv_sec = 0 ;
    rem.tv_nsec = 0 ;
}



ctrl +  c  是發 sigint  訊號

總個五個時間,一個核心時間