日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

Linux时间管理和定时学习笔记

發布時間:2024/1/18 linux 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Linux时间管理和定时学习笔记 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Linux時間管理和定時

  • 時間的概念
  • 時間的計量
  • Linux系統中的時間管理
    • Linux內核對時間的維護
    • 獲取當前時間
    • 時間格式轉換
    • 將時間轉換為字符串
    • 指定格式的字符串
    • 獲取高精度時間
      • 微秒
      • 納秒
  • Linux中的定時器
    • 相關API
    • interval timer
    • POSIX timer

時間的概念

計算機中處處需要時間

  • 程序運行時間(音視頻播放進度、下載、上傳)
  • 系統日志log
  • QQ、微信、短信聊天記錄
  • 當前時間、上班時間、睡覺時間
  • 周期性做一些事情(殺毒、清理垃圾、軟件升級更新)
  • 定時開關機
  • OS調度:時間片、定時器

時間分類

  • 絕對時間:2020年1月5日、開機時間
  • 相對時間:萬歷15年、明天、下周、明年、昨天

時區的概念

  • 標準時間:
    • UTC:Coordinated Universal Time,世界協調時間
    • GMT:Greenwich Mean Time,格林威治標準時間、子午線
  • 本地時間:
    • 北京時間:東8區、UTC/GMT+08:00
    • 計時誤差:手表時間快了?慢了?

時間的計量

計時器的發展歷程

  • 日出日落、月圓月缺、春夏交替
  • 日晷、沙漏、刻漏
  • 機械鐘
  • 石英振蕩器、晶振
  • 銫原子鐘
  • 氫原子鐘、銣原子鐘

計算機中的計時

  • 51單片機:晶振
  • 嵌入式系統:定時器
  • PC計算機:定時器
  • 實時時鐘:獨立電源(CMOS電池、手機晶振)

時間之源

  • 原子時鐘
  • 銫原子鐘、銣原子鐘
  • 各地實驗室原子鐘
  • 中國科學院國家授時中心
  • 系統時間校準

Linux系統中的時間管理

Linux內核對時間的維護

  • 定時器—時鐘中斷–時鐘節拍tick—jiffies
  • 實時時鐘–xtime
  • jiffies:內核中的全局變量,系統啟動以來的節拍數
  • HZ:中斷頻率
  • xtime:內核中的全局變量,墻上時間、UTC

獲取當前時間

全局變量:xtime

  • typedef long time_t;
  • time_t time(time_t *tloc);
  • int stime(const time_t *t);

時間格式轉換

把秒數轉換為年月日時分秒

  • struct tm *gmtime (const time_t *timep);
  • struct tm *localtime (const time_t *timep);
  • time_t mktime(struct tm *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 */ };

將時間轉換為字符串

時間格式->字符串格式

  • char *asctime(const struct tm *tm);
  • char *ctime(const time_t *timep);

指定格式的字符串

用戶自定義格式

  • size_t strftime (char *s, size_t max, const char *format, const struct tm *tm);
  • char *strptime (const char *s, const char *format, struct tm *tm);


字符串格式

%a :星期幾名稱的簡寫 %A :星期幾名稱的全稱 %b :月份名稱的簡寫 %B :月份名稱的全稱 %c :首選的日期和時間表示法 %C :世紀(年份除以 1000099%d :一個月中的第幾天(0131%D :時間格式,與 %m/%d/%y 表示法相同 % :一個月中的第幾天(131%g : 與 %G 表示法類似,但不帶世紀 %h :與 %b 表示法相同 %H - 小時,使用 24 小時制(0023%I :小時,使用 12 小時制(0112%j :一年中的第幾天(001366%m :月份(0112%M :分 %n :換行符 %p :與給定的時間值相對應的 am 或 pm %r : a.m. 和 p.m. 的時間標記法 %R : 24 小時制的時間標記法 %S : 秒 %t : tab 制表符 %T :當前時間,與 %H:%M:%S 表示法相同 %u :星期幾的數字表示(17%U :當年周數 %w :十進制數形式表示一周中的某天,Sunday[星期日] = 0 %x :首選的日期表示法,不帶時間 %X :首選的時間表示法,不帶日期%y %Y :包含表示世紀的數字的年份表示 %Z :時區名稱 %z :時區名稱的簡寫 %% :輸出一個 % 字符

獲取高精度時間

微秒

微秒級時間

  • int gettimeofday (struct timeval *tv, struct timezone *tz);
  • int settimeofday (const struct timeval *tv, const struct timezone *tz);
struct timeval {time_t tv_sec; /* seconds */ suseconds_t tv_usec; /* microseconds */ }; struct timezone { int tz_minuteswest; /* minutes west of Greenwich */ int tz_dsttime; /* type of DST correction */ };

納秒

納秒級時間

  • int clock_gettime (clockid_t clk_id, struct timespec *tp);
  • int clock_settime (clockid_t clk_id, const struct timespec *tp);
struct timespec {time_t tv_sec; /* seconds */long tv_nsec; /* nanoseconds */ };


納秒級時間
– 參數:clock_id

  • 說明:設置時鐘時間類型
  • CLOCK_REALTIME:系統實時時間(從1970-1-1 計時)
  • CLOCK_MONOTONIC:系統啟動時間
  • CLOCK_PROCESS_CPUTIME_ID:當前進程CPU花費時間
  • CLOCK_THREAD_CPUTIME_ID:當前線程CPU花費時間

Linux中的定時器

內核對定時器的維護

  • 定時器中斷—OS維護全局變量—系統調用API—獲取/設置時間
  • 定時器中斷— 周期動作:發信號、新建線程
  • 周期性工作:
    • 定時殺毒
    • 數據備份
    • 郵件提醒
    • 日志清理

相關API

  • 內核API:add_timer、mod_timer、del_timer
  • 簡單的鬧鐘:alarm、sleep、usleep、nanosleep
  • 計時器(interval timer):getitimer、setitimer
  • 高級定時器(POSIX timer)
    • timer_create
    • timer_settime
    • timer_gettimer
    • timer_delete
    • timer_getoverrun

interval timer

定時器+signal

  • interval timer,共享相同的定時器
    • 定時器到期,發送信號到當前進程【定時器精度:微秒級】
    • int getitimer (int which, struct itimerval *curr_value);
    • int setitimer (int which, const struct itimerval *new_value, struct itimerval *old_value);
struct itimerval { struct timeval it_interval; /* Interval for periodic timer */ struct timeval it_value; /* Time until next expiration */ }; struct timeval { time_t tv_sec; /* seconds */ suseconds_t tv_usec; /* microseconds */ };

函數參數

  • which:定時器類型
  • ITIMER_REAL:實時計時,到期后發送SIGALRM信號到進程
  • ITIMER_VIRTUAL:進程用戶態運行才計時,到期發送SIGVTALRM信號
  • ITIMER_PROF:進程在用戶和內核空間都計時,到期發送SIGPROF信號

POSIX timer

POSIX定時器API

  • 編譯鏈接:-lrt
  • int timer_create (clockid_t clockid, struct sigevent *sevp, timer_t *timerid);
  • int timer_gettime (timer_t timerid, struct itimerspec *curr_value);
  • int timer_settime (timer_t timerid, int flags, const struct itimerspec *new_value,struct itimerspec *old_value);
  • int timer_getoverrun (timer_t timerid);
  • int timer_delete (timer_t timerid);

函數參數: sevp

typedef struct sigevent {__sigval_t sigev_value; // timer的ID int sigev_signo; // 發送信號類型 int sigev_notify; // 定時器到期之后的動作:發信號、線程 void (*sigev_notify_function) (__sigval_t); /* Function to start. */ void *sigev_notify_attributes; /* Really pthread_attr_t.*/ } sigevent_t; union sigval {int sival_int; // argsvoid *sival_ptr; //timer ID }; struct itimerspec { struct timespec it_interval; struct timespec it_value; };

函數參數:clockid

  • CLOCK_REALTIME:系統實時時間
  • CLOCK_MONOTONIC:系統啟動以來的絕對時間
  • CLOCK_PROCESS_CPUTIME_ID:進程運行時間(用戶和內核空間)
  • CLOCK_THREAD_CPUTIME_ID:線程運行時間(用戶和內核空間)

sevp.sigev_notify:定時器到期后的動作

  • SIGEV_NONE:什么都不做
  • SIGEV_SIGNAL:發送信號給當前進程
  • SIGEV_THREAD:開啟一個線程,執行用戶指定function
  • SIGEV_THREAD_ID:給當前進程中的指定線程發信號

總結

以上是生活随笔為你收集整理的Linux时间管理和定时学习笔记的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。