linux实时线程调度bug,linux中采用用户级线程模拟实现EDF和RMS两种处理机实时调度算法之改进...
原算法中被選中任務每運行一個時間單位便將控制權交給主線程。再判斷是否需要切換實時任務。實際上不需這樣頻繁的時鐘中斷。
故改進成。只在需要重新調度任務時才返回主控線程。且統計出線程切換次數(主線程切換不計)。
//編譯
//?pthread庫不是Linux系統默認的庫,所以在編譯中需加-lpthread參數(posix線程庫 )
gcc -lpthread -lm test.c -o test.out
#include "math.h"
#include "sched.h"
#include "pthread.h"
#include "stdlib.h"
#include "semaphore.h"
//@author vince
typedef struct{
char task_id;
int call_num; //times has been called
int ci; //processing time
int ti; //T
int ci_left;
int ti_left; //time left to next T
int flag; //isActive,0_no,2_yes
int arg; //argument
pthread_t th; //thread
}task;
void proc(int *args);
void idle();
int select_proc();
int task_num=0;
int idle_num=0;
int alg; //1 for EDF, 2 for RMS
int curr_proc=-1; //index
int demo_time=100; //time for show
task *tasks;
pthread_mutex_t proc_wait[100]; //the one be chosed is unlocked
pthread_mutex_t main_wait,idle_wait;
float sum=0;
pthread_t idle_proc;
int i;
int switchCount=0;
int main(int argc,char **argv)
{
pthread_mutex_init(&main_wait,NULL);
pthread_mutex_lock(&main_wait); //
pthread_mutex_init(&idle_wait,NULL);
pthread_mutex_lock(&idle_wait); //
printf("Please input number of real time tasks : \n");
scanf("%d",&task_num);
tasks=(task*)malloc(task_num*sizeof(task));
for(i=0;ir)
{ //unschedulable
printf("(sum=%lf > r=%lf),not schedulable!\n",sum,r);
exit(2);
}
pthread_create(&idle_proc,NULL,(void*)idle,NULL);
//create idle proc
for(i=0;i0)
{
//pthread_mutex_lock(&proc_wait[*args]); //wait to be chosed
if(idle_num!=0)
{
printf("idle(%d)",idle_num);
idle_num=0;
}
printf("%c%d",tasks[*args].task_id,tasks[*args].call_num);
tasks[*args].ci_left--;
if(tasks[*args].ci_left==0)
{
printf("(%d)",tasks[*args].ci);
tasks[*args].flag=0;
tasks[*args].call_num++;
//pthread_mutex_unlock(&main_wait); //wake up main proc
//pthread_mutex_lock(&proc_wait[*args]);
}
int j;
//once it process,count rather than count in main(),coz it need to select in right here!
for(j=0;j");
idle_num++;
int j;
//once it process,count rather than count in main(),coz it need to select in right here!
for(j=0;jtasks[j].ti_left){
temp1=tasks[j].ti_left;
temp2=j;
break; }
case 2: //RMS
if(temp1>tasks[j].ti){
temp1=tasks[j].ti;
temp2=j; }
}
}
}
return temp2;
}
歡迎討論。
總結
以上是生活随笔為你收集整理的linux实时线程调度bug,linux中采用用户级线程模拟实现EDF和RMS两种处理机实时调度算法之改进...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux文本工具总结,Linux 文本
- 下一篇: linux 其他常用命令