發表文章

目前顯示的是 8月, 2022的文章

jiffies用法

  jiffies用法 jiffies定義在 (引入 會自動引入此檔)。Jiffies為Linux核心變數,它被用來紀錄系統自開幾以來,已經過多少的tick。Tick是HZ的倒數,意即timer interrupt每發生一次中斷的時間。 如HZ為250時,tick為4毫秒(millisecond)。 範例: #include<linux/jiffies.h> unsigned long j ,stamp_1,stamp_half,stamp_n; j = jiffies /* 讀取當時的時戳 */  stamp_1 = j + HZ; /* 1秒之後 */  stamp_half = j + HZ/2; /* 半秒之後 */  stamp_n = j + n*HZ /1000 /* n毫秒之後 */ #include<linux/jiffies.h> int time_after(unsigned long a,unsigned long b); int time_before(unsigned long a,unsigned long b); int time_after_eq(unsigned long a,unsigned long b); int time_before_eq(unsigned long a,unsigned long b);

pthread priority

Linux核心的三種排程策略: 1,SCHED_OTHER 分時排程策略, 2,SCHED_FIFO實時排程策略,先到先服務。一旦佔用cpu則一直執行。一直執行直到有更高優先順序任務到達或自己放棄 3,SCHED_RR實時排程策略,時間片輪轉。當程序的時間片用完,系統將重新分配時間片,並置於就緒佇列尾。放在佇列尾保證了所有具有相同優先順序的RR任務的排程公平 int main() { int i; i = getuid(); if(i==0)      printf("The current user is root\n"); else      printf("The current user is not root\n"); pthread_t ppid1; struct sched_param param; pthread_attr_t attr2; pthread_attr_init(&attr2); param.sched_priority = 51; pthread_attr_setschedpolicy(&attr2,SCHED_RR); pthread_attr_setschedparam(&attr2,&param); pthread_attr_setinheritsched(&attr2,PTHREAD_EXPLICIT_SCHED);//要使優先順序其作用必須要有這句話 pthread_create(&ppid1,&attr2,(void *)Thread1,NULL); pthread_join(ppid1,NULL); pthread_attr_destroy(&attr2); return 0; }