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,linux,EDF,RMS,处理机实时调度算法,改进)