All pastes #435693 Raw Edit

Something

public text v1 · immutable
·published 2007-04-12 02:19 UTC
rendered paste body
// C Source File
// Created 4/11/2007; 5:32:18 AM
#undef OPTIMIZE_ROM_CALLS
#define SAVE_SCREEN

#include <tigcclib.h>
#include <stdio.h>
#include <math.h>
#include <float.h>




float calculateU(int numberOfTasks);
void getparameters();

#define ARRAYSIZE         10
float carray[ARRAYSIZE];
float parray[ARRAYSIZE];
float barray[ARRAYSIZE];
float darray[ARRAYSIZE];
int uschedfailedtasks[ARRAYSIZE];
/*
float *carray;
float *parray;
float *barray;
float *darray;
int *uschedfailedtasks;
*/
int ufailedcount;
int tasks;
int advancedmode;
float contextswitch;

// Main Function
void _main(void)
{
   int i, i2;
   char buffer[50];
   
   advancedmode = 0;
   ufailedcount = 0;
   tasks = 0;
   contextswitch = 0.0F;
/*
   if ((carray = (float *)malloc(tasks * sizeof(float)))==NULL) {
   	 DlgMessage("ERROR", "Out of memory", BT_OK, BT_NONE);
   	 return;
   }
   if ((parray = (float *)malloc(tasks * sizeof(float)))==NULL) {
   	 DlgMessage("ERROR", "Out of memory", BT_OK, BT_NONE);
   	 return;
   }
   if ((barray = (float *)malloc(tasks * sizeof(float)))==NULL) {
   	 DlgMessage("ERROR", "Out of memory", BT_OK, BT_NONE);
   	 return;
   }
   if ((darray = (float *)malloc(tasks * sizeof(float)))==NULL) {
   	 DlgMessage("ERROR", "Out of memory", BT_OK, BT_NONE);
   	 return;
   }
   if ((uschedfailedtasks = (int *)malloc(tasks * sizeof(int)))==NULL) {
   	 DlgMessage("ERROR", "Out of memory", BT_OK, BT_NONE);
   	 return;
   }
*/
   clrscr();
   while (1) {
      printf("Advanced Mode(Y/N)? ");
      getsn (buffer, 50);
      if (buffer[0]=='Y' || buffer[0]=='y') {
         advancedmode = 1;
         break;
      } 
      else if (buffer[0]=='N' || buffer[0]=='n') {
         advancedmode = 0;
         break;
      }
   }
   
   // get context switch
   if (advancedmode) {
      while (1) {
         printf("\n");
         printf("Context switch=? ");
         getsn (buffer, 50);
         if (!isdigit(buffer[0])) continue;
         contextswitch = atof(buffer);
         if (contextswitch>=0.0F) break;
      }
   }
   
   while (1) {
      getparameters();   	

      // clears the screen
      clrscr();
      printf("Confirm\n");
      for (i=0;i<tasks;i++) {
         printf("Task %d C=%.1f P=%.1f", i+1, carray[i], parray[i]);
         if (advancedmode) {
            printf(" B=%.1f D=%.1f", barray[i], darray[i]);
         }
         printf("\n");
      }
      int breakinput = 0;
      while (1) {
         printf("\nIs this correct(Y/N)? ");
         getsn (buffer, 50);
         if (buffer[0]=='Y' || buffer[0]=='y') {
            breakinput = 1;
            break;
         } 
         else if (buffer[0]=='N' || buffer[0]=='n') break;
      }
      if (breakinput) break;
   }
   
   clrscr();
   printf("Utilization Bound Test\n");
   for (i=0;i<tasks;i++) {
      // buffer becomes forumla
      float utilization=0.0F;
      float ubound;
      buffer[0]='\0';
      for (i2=0;i2<=i;i2++) {
        float numerator=carray[i2]+2*contextswitch;     
        // see if we are in advanced mode and make sure 
        // there is either blocking or preperiod deadline
        if (advancedmode && i2==i && (barray[i2]!=0.0F || darray[i2]!=parray[i2])) {
        	 sprintf(buffer, "%s(%.1f", buffer, numerator);
           if (barray[i2]!=0.0F) {
        	    numerator += barray[i2];
        	    sprintf(buffer, "%s+%.1f", buffer, barray[i2]);
        	 }
           if (darray[i2]!=parray[i2]) {
        	    numerator += (parray[i2] - darray[i2]);
        	    sprintf(buffer, "%s+%.1f-%.1f", buffer, parray[i2], darray[i2]);
        	 }
        	 sprintf(buffer, "%s)", buffer);
        } else {
       	   sprintf(buffer, "%s%.1f", buffer, numerator);
        }
      	sprintf(buffer, "%s/%.1f", buffer, parray[i2]);
      	if (i2!=i)
         	strcat(buffer, "+");
      	utilization += numerator/parray[i2];
      }
      ubound = calculateU(i+1);
      if (utilization > ubound) {
         printf("U%d->%s=%f>U(%d)=%1.3f [check]\n", (i+1), buffer, utilization,
            (i+1), ubound);
         uschedfailedtasks[ufailedcount]=i;
         ufailedcount++;
      }
      else {
         printf("U%d->%s=%1.3f<U(%d)=%1.3f [scheduable] \n", (i+1), buffer, utilization,
            (i+1), ubound);
      }
   }
   
   // wait
   printf("\nContinue\n");
	 ngetchx();

   clrscr();
   printf("Exact Scheduability Test\n");
   // i = index in failed list
   // task actual task
   for (i=0;i<ufailedcount;i++) {
      int task=uschedfailedtasks[i];

      // calculate r0
      float currentr=0.0F;
      int iteration = 1;
      float previousr;

      buffer[0]='\0';
      for (i2=task;i2>=0;i2--) {
        float numerator = 0.0F;
        // check if there is block and working on that particular task
        if (advancedmode && i2==task && barray[i2]!=0.0F) {
           numerator += barray[i2];  	
           sprintf(buffer, "%s%.1f+", buffer, barray[i2]);
        }
        numerator += carray[i2]+2.0F*contextswitch;
        sprintf(buffer, "%s%.1f", buffer, (carray[i2]+2.0F*contextswitch));
      	if (i2!=0)
         	strcat(buffer, "+");
        currentr+=numerator;
      }
      previousr=currentr;
      printf("r(%d,0)->%s=%.1f\n", (task+1), buffer, currentr);


      // calculate each r > 0
      float deadline=parray[task];
      if (advancedmode)
         deadline=darray[task];
      
      while (1) {
         // do previous task only
         currentr=carray[task]+2.0F*contextswitch;
         sprintf(buffer, "%.1f+", currentr);
         // check if there is blocking for current task
         if (advancedmode && barray[task]!=0.0F) {
               currentr += barray[task];  	
               sprintf(buffer, "%s%.1f+", buffer, barray[task]);
         }
         for (i2=0;i2<task;i2++) {
            float cmask = carray[i2]+2.0F*contextswitch;
      	    sprintf(buffer, "%sceil(%.1f/%.1f)*%.1f", buffer, previousr, parray[i2], cmask);
      	    if (i2!=task-1)
               strcat(buffer, "+");
            float currentterm = (ceil((float)previousr/(float)parray[i2]))*cmask;
            currentr += currentterm;
//             printf("currentterm = %.1f", currentterm);
         }
         printf("r(%d,%d)->%s=%.1f\n", (task+1), iteration,
            buffer, currentr);
         if (currentr==previousr || currentr>=deadline) {
            break;
         }
         previousr = currentr;
         iteration++;
      }
      
      if (currentr>deadline)
         printf("task(%d) %.1f>%.1f is not scheduable\n", (task+1), currentr, deadline);
      else
         printf("task(%d) %.1f<=%.1f is scheduable\n", (task+1), currentr, deadline);
     
      if (i!=ufailedcount-1) {
         printf("\nPress any key to continue\n");
	       ngetchx();
         clrscr();
	    }
   }

/*
   free(carray);
   free(parray);
   free(array);
   free(darray);
   free(uschedfailedtasks);
*/ 
 	 // wait for program to exit
   printf("\nDone\n");
	 ngetchx();
}

float calculateU(int numberOfTasks) {
	return numberOfTasks*(pow(2, 1.0F/(float)numberOfTasks)-1);
}

void getparameters() {
   int i=0;
   char buffer[50];

   // clears the screen
   clrscr();

   printf("Enter number of tasks=");
   tasks = atoi (getsn (buffer, 50));
   for (i=0;i<tasks;i++) {
      while (1) {
         printf("\n");
         printf("C%d=", i+1);
         getsn (buffer, 50);
         if (!isdigit(buffer[0])) continue;
         carray[i] = atof(buffer);
         if (carray[i]>0.0F) break;
      }
      while (1) {
         printf("\n");
         printf("P%d=", i+1);
         getsn (buffer, 50);
         if (!isdigit(buffer[0])) continue;
         parray[i] = atof (buffer);
         if (parray[i]>0.0F) break;
      }
      if (!advancedmode) continue;
      
      // advanced mode has block
      while (1) {
         printf("\n");
         printf("B%d=", i+1);
         getsn (buffer, 50);
         if (!isdigit(buffer[0])) continue;
         barray[i] = atof (buffer);
         if (barray[i]>=0.0F) break;
      }
      while (1) {
         printf("\n");
         printf("D%d=", i+1);
         getsn (buffer, 50);
         if (!isdigit(buffer[0])) continue;
         darray[i] = atof (buffer);
         // no preperiod deadline assign period
         if (darray[i]==0.0F) {
            darray[i]=parray[i];  
            break;       	
         }
         else if (darray[i]>0.0F) break;
      }
      
   }
}