25 Mayıs 2010 Salı

Rate Monotonic C Code

Evet arkadaslar benim ve arkadaslarimin bu aralar ustunde bayaa bi calistigim ve internette kaynak kodunu bulamadigim bir algoritmanin yani Rate Monotonic in kaynak kodunu sizlerle paylasmak istiyorum. Kisaca Rate Monotonic periyodu en dusuk olan threadin priority sini en yuksek yapiyor.  Threadler calisirken daha ust oncelikli bir thread gelirse calisan thread durduruluyor ve oncelikli thread calismaya  basliyor. Oncelikli threadin isi bittikten sonra durdurulan threadin islemine devam ediliyor. Bu islemler tum thread ler bitene kadar devam ediyor...


//Rate Monotonic Source Code

#include
#include

#define PROCESS 3

pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;

int cpu = 1;

int running_process = -1;

int timer = 0;

int arrival_times[PROCESS] = { 0, 1, 2 };

int burst_times[PROCESS]   = { 4, 5, 6 }; //execution time

int period[PROCESS] = {20, 15, 16}; //periyodu kucuk olanin priority si en buyuk olur

char name[PROCESS] = {'A', 'B', 'C'};

int cur_times[PROCESS] = {0};

int queue[50] = {0};
int tail = 0;

//void pq() { int i; for(i = 0; i < tail; i++) printf("%c(%d) ", name[queue[i]], burst_times[queue[i]] - cur_times[queue[i]]); printf("peek = %d\n", peekQueue()); }

void pushQueue(int p) {//Queue ya atama islemini yapar

pthread_mutex_lock( &mtx );

queue[tail++] = p; printf("%2d.sn'de %c process'i kuyruga girdi..\n", timer, name[p]); //pq();

pthread_mutex_unlock( &mtx );

}



void popQueue(int thrId) {//Queudan eleman cikarma islemini yapar

pthread_mutex_lock( &mtx );

int i, j;

for(i = 0; i < tail; i++)
if ( queue[i] == thrId ){
j = i;
break;
}

for(i = j; i < tail; i++) queue[i] = queue[i+1];

tail--;

pthread_mutex_unlock( &mtx );

}

int peekQueue() {//Periyodu en dusuk olan yani priority si en yuksek olan thread i secer

pthread_mutex_lock( &mtx );

int i, min, r;

r = 0;

min = (period[queue[0]]);

for(i = 0; i < tail; i++) //Min periyodu bulur(max priority)
if((period[queue[i]]) < min) {
min = (period[queue[i]]);
r = i;
}

pthread_mutex_unlock( &mtx );

return r;

}

int calis(int thrId) {

if(running_process != -1 && running_process != thrId && burst_times[running_process] != cur_times[running_process])
printf("%2d.sn'de %c process'i durakladi..\n", timer, name[running_process]);

    if(cur_times[thrId] == 0)
printf("%2d.sn'de %c process'i calismaya basladi..\n", timer, name[thrId]);
else if(running_process != thrId)
printf("%2d.sn'de %c process'i tekrar calismaya basladi..\n", timer, name[thrId]);
//pq();
int ret = 1;
running_process = thrId;

timer++;

cur_times[thrId]++;

cpu = 0;

sleep(1);

cpu = 1;

if(cur_times[thrId] == burst_times[thrId])
{
printf("%2d.sn'de %c process'i calismasini bitirdi..\n", timer, name[thrId]);
popQueue(thrId); /*pq();*/
running_process = -1; ret = 0;
}

return ret;

}

int calisayimMi(int thrId) {

if(cpu == 1 && queue[peekQueue()] == thrId) { return 1; } else return 0;

}

void *myThread(void *thrId){

int tid = *((int*)thrId);
int sonuc;

int izin;

sleep(arrival_times[tid]);

pushQueue(tid);

while(1) {

if(calisayimMi(tid) == 1) {

sonuc = calis(tid);

if(sonuc == 0) break;

}

}

}

int main(){

pthread_t tid;
int ret, i;
int thrIds[4] = {0, 1, 2, 3};
pthread_t t[PROCESS];
for(i = 0; i < PROCESS; i++)
pthread_create(&t[i],NULL,myThread,(void *)&thrIds[i]); //Butun prosesleri yaratir
for(i = 0; i < PROCESS; i++)
pthread_join(t[i],NULL);
printf("\nProgram sonlandi..\n\n");
return 0;
}

0 yorum:

Yorum Gönder