原算法中被选中任务每运行一个时间单位便将控制权交给主线程。再判断是否需要切换实时任务。实际上不需这样频繁的时钟中断。
故改进成。只在需要重新调度任务时才返回主控线程。且统计出线程切换次数(主线程切换不计)。
//编译
// 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