ايران ويج

نسخه‌ی کامل: الگوریتم زمانبندی RR به زبان C همراه سورس کد
شما در حال مشاهده‌ی نسخه‌ی متنی این صفحه می‌باشید. مشاهده‌ی نسخه‌ی کامل با قالب بندی مناسب.
در این پست شما می توانید  سورس الگوریتم زمانبندی RR به زبان C سی را از ما دریافت کنید.

کد:
#include<stdio.h>
struct job
{
   char jid[10];
   int at;
   int bt;    
   int wt;
   int tt;
}jq[10],rq[100];
int front_jq=0,rear_jq=0;
int front_rq=0,rear_rq=0;
int rt[100];
void gantt_chart(int q)
{
   int i,k,val,j=rq[0].at;
   printf("\n\nGANTT CHART\n");
   for(i=0;i<rear_rq;i++)
   {
       for(k=0;k<rt[i];k++)
           printf("__");
       printf(" ");
   }
   printf("\n");
   for(i=0;i<rear_rq;i++)
   {
       printf("%s",rq[i].jid);
       for(k=0;k<rt[i]-1;k++)
                       printf("  ");
               printf(" ");
   }
   printf("\n");
       for(i=0;i<rear_rq;i++)
       {
               for(k=0;k<rt[i];k++)
                       printf("__");
               printf(" ");
       }
       printf("\n");    
   for(i=0;i<rear_rq;i++)
   {
       if(j<10)
           printf("0%d",j);
       else    
           printf("%d",j);
       for(k=0;k<rt[i]-1;k++)
                       printf("  ");
               printf(" ");
       j+=rt[i];
   }
}
int main()
{
   float avg_wt=0,avg_tt=0;
   int n=0,i,j,q;
   FILE *fp;
   fp=fopen("rr.txt","r");
   fscanf(fp,"%d\n",&q);
   do
   {
       fscanf(fp,"%s\t%d\t%d\n",jq[n].jid,&jq[n].at,&jq[n].bt);
       jq[n].wt=0;
       jq[n].tt=0;
       n++;
   }
   while(!feof(fp));
   fclose(fp);
   rear_jq=n;
   rr_scheduling(q,n);

   for(i=0;i<n;i++)
       jq[i].tt=jq[i].bt+jq[i].wt;
   printf("\nSCHEDULING\n");
   for(i=0;i<rear_rq;i++)
   {
       if(i==rear_rq-1)
           printf("%s",rq[i].jid);
       else
           printf("%s --> ",rq[i].jid);
   }  
   gantt_chart(q);
   printf("\n\nJID\tAT\tBT\tWT\tTT\n");
   for(i=0;i<n;i++)
   {
       printf("%s\t%d\t%d\t%d\t%d\n",jq[i].jid,jq[i].at,jq[i].bt,jq[i].wt,jq[i].tt);
       avg_wt+=jq[i].wt;
       avg_tt+=jq[i].tt;
   }
   printf("Average Waiting Time:: %.2f\n",avg_wt/n);
   printf("Average Turnaround Time:: %.2f\n",avg_tt/n);
   return 0;
}
همین الگریتم به کمک فایل بدون اینتر فیس
کد:
#include <iostream>


struct job
{
   char jid[10];
   int at;
   int bt;    
   int wt;
   int tt;
}jq[10],rq[100];
int front_jq=0,rear_jq=0;
int front_rq=0,rear_rq=0;
int rt[100];
void gantt_chart(int q)
{
    FILE *fp;
    fp = fopen ("out.txt","a");
    
   int i,k,val,j=rq[0].at;
   fprintf (fp, "\n\nGANTT CHART\n");
   for(i=0;i<rear_rq;i++)
   {
       for(k=0;k<rt[i];k++)
           printf("__");
       fprintf (fp, " ");
   }
   fprintf (fp, "\n");
   for(i=0;i<rear_rq;i++)
   {
       printf("%s",rq[i].jid);
       for(k=0;k<rt[i]-1;k++)
                       fprintf (fp, "  ");
               fprintf (fp,"  ");
   }
   fprintf (fp, "\n");
       for(i=0;i<rear_rq;i++)
       {
               for(k=0;k<rt[i];k++)
                       printf("__");
               fprintf (fp, "  ");
       }
       fprintf (fp, "\n");    
   for(i=0;i<rear_rq;i++)
   {
       if(j<10)
           fprintf (fp, ""+j);
       else    
           fprintf (fp, ""+j);
       for(k=0;k<rt[i]-1;k++)
                       fprintf (fp, "  ");
               fprintf (fp, "  ");
       j+=rt[i];
   }
}
int main()
{
   float avg_wt=0,avg_tt=0;
   int n=0,i,j,q;
   FILE *fp;
   fp=fopen("rr.txt","r");
   fscanf(fp,"%d\n",&q);
   do
   {
       fscanf(fp,"%s\t%d\t%d\n",jq[n].jid,&jq[n].at,&jq[n].bt);
       jq[n].wt=0;
       jq[n].tt=0;
       n++;
   }
   while(!feof(fp));
   fclose(fp);
   rear_jq=n;
  // rr_scheduling(q,n);



  fp = fopen ("out.txt","a");
 
 
   for(i=0;i<n;i++)
       jq[i].tt=jq[i].bt+jq[i].wt;
   fprintf (fp, "\nSCHEDULING\n");
   for(i=0;i<rear_rq;i++)
   {
       if(i==rear_rq-1)
          fprintf (fp, rq[i].jid);
       else
           fprintf (fp, rq[i].jid);
   }  
   gantt_chart(q);
   
   

   
   
   fprintf (fp, "\n\nJID\tAT\tBT\tWT\tTT\n");
   for(i=0;i<n;i++)
   {
       fprintf (fp, "%s\t%d\t%d\t%d\t%d\n",jq[i].jid,jq[i].at,jq[i].bt,jq[i].wt,jq[i].tt+"\n");
    
       avg_wt+=jq[i].wt;
       avg_tt+=jq[i].tt;
   }
   

   //fprintf (fp, "Average Waiting Time:: \n"+(avg_wt)+"\n");
   //fprintf (fp, "Average Turnaround Time:: "+(avg_tt)+"\n");
     fclose (fp);
   return 0;
}
اینم ی سورس دیگه در همین زمینه
کد:
#include <utility>
#include <stdio.h>
#include <string>
#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <queue>

using namespace std;


int quantum;
int latency;

string state;
string timeline;
string timeStr;

struct Process {
   string name;
   int arrivalTime;
   int remQuantum = quantum;
   vector<int> burstIoList;

   Process( string &name, int arrivalTime, vector<int> &bio) {
       this->name = name;
       this->arrivalTime = arrivalTime;
       this->burstIoList = bio;
   }

   int getAndRemoveFirst() {
       int value = burstIoList.at(0); // store first element value in a temp variable
       burstIoList.erase(burstIoList.begin()); // remove first element from burstIo List
       return value;
   }

   void replaceFirst(int numb) {
       burstIoList.erase(burstIoList.begin()); // remove first element
       burstIoList.insert(burstIoList.begin(), numb); // insert element in start of list
   }
};

// --------------------- DECLARATION ---------------------

// Input - Output
void addProcess(const string &line);
void readInput(string &address);
void writeOutput(string &outputAddress);

// FCFS
void fcfs(int init);

// Round Robbin
void transferFromWaitingToReady(vector<Process> &ready, vector<Process> &waiting, int time);
int rr();


vector<Process> processList; // list of process read from input
vector<Process> fcfsList; // list of process remained after rr scheduler algorithm
int terminates = 0; // number of terminated process

int main(){
   string inputAddress = "H:\\p7.txt";
   string outputAddress = "H:\\output.txt";
   readInput(inputAddress);

   int timeAfterRR = rr(); // execute rr algorithm
   fcfs(timeAfterRR); // pass timeStr after finishing rr algorithm to fcfs as starting timeStr

   writeOutput(outputAddress);

}

//---------------------INPUT - OUTPUT ------------------------------
void addProcess(const string &line) {
   istringstream ss(line); // read words from line separated by space

   // read process name
   string name;
   ss >> name;

   // read process arrivalTime
   string arrivalTime;
   ss >> arrivalTime;


   vector<int> burstIoList;
   string temp;
   while(true){
       ss >> temp;
       if (!ss) break; // if there isn't anything break the loop
       burstIoList.push_back(stoi(temp)); // add new burstIo to end of burstIoList
       // Note: stoi function, convert string to int
   }

   // add process to end of process list
   processList.emplace_back(name,stoi(arrivalTime),burstIoList);
}

/*
* function that sort processes by arrivalTime from small to big
* */
bool compareProcess(Process p1, Process p2){
   return p1.arrivalTime <= p2.arrivalTime;
}

void readInput(string &address) {
   ifstream file(address); // read information from file

   // use address variable as a temp variable...

   getline(file, address); // read latency and put it in address
   latency = stoi(address);
   getline(file, address); // read quantum timeStr and put it in address
   quantum = stoi(address);

   // after getting latency and quantum there's all processes
   // read processes from file until there isn't anything left
   while (getline(file, address)) addProcess(address);

   // sort processList from beginning to end of it with compareProcess Function
   sort(processList.begin(), processList.end(), compareProcess);
}

/*
* write GANTT chart to output file
* */
void writeOutput(string &outputAddress) {
   ofstream output;
   output.open(outputAddress);
   output << state << endl;
   output << timeline << endl;
   output << timeStr << endl;
}

//---------------------FCFS------------------------------

/* used in fcfs
* find position of a process that is ready to be processed
* if there isn't any, return -1 as Nothing found
* */
int nextProcess(int time, int l) {
   for(int i=0; i < fcfsList.size(); i++) {
       if (fcfsList.at(i).arrivalTime <= time && i >= l) return i;
   }

   return -1;
}

/*
* sum burst times of a process
* Note: even-index elements are burst timeStr*/
int sum(vector<int> a) {
   int sum = 0;
   for (int i=0; i < a.size(); i++) if (i % 2 == 0) sum += a.at(i);
   return sum;
}

/* USE FOR MAKING GANTT CHART PRETTY :)
*
* */
string timeCell(int time){
   // if timeStr < 10 ? true : false
   return (to_string(time)).size() == 1 ? (" "+to_string(time)+ " |") : to_string(time)+" |";
}

/* USE FOR MAKING GANTT CHART PRETTY :)
*
* */
string cell(const string &a) {
   return a+"|";
}

/* USE FOR MAKING GANTT CHART PRETTY :)
*
* */

void emptyColumn() {
   state += cell("   ");
   timeline += cell(" # ");
   timeStr += cell("   ");
}

/* FCFS algorithm
* */
void fcfs(int init) {
   int time;
   // loop until all processes terminate
   for (time = init; terminates != processList.size();) {
       // add time column to output
       state += cell("   ");
       timeline += cell("   ");
       timeStr += timeCell(time);

       int processPosition = nextProcess(time, terminates);
       // -1 means no ready process found
       if(processPosition == -1) { // if no ready process found, add 1unit to time and loop
           emptyColumn();
           time++;
       }else{
           Process p = fcfsList.at(processPosition); // get process from position

               // add Dispatcher Latency to time
               time+= latency;
               // add DL column to output
               state += cell("   ");
               timeline += cell("DL ");
               timeStr += cell("   ");
               // add a time column to output
               state += cell("   ");
               timeline += cell("   ");
               timeStr += timeCell(time);

           // add termination column to output
           state += cell("  T");
           timeline += cell(p.name + " ");
           timeStr += cell("   ");

           time += sum(p.burstIoList);
           terminates++;
       }
   }

   // add final time column to output
   state += cell("   ");
   timeline += cell("   ");
   timeStr += timeCell(time);
}

//---------------------RR------------------------------


/*
*  transfer all process that become ready from waiting list to ready list
* */
void transferFromWaitingToReady(vector<Process> &ready, vector<Process> &waiting, int time) {
   sort(waiting.begin(),waiting.end(),compareProcess); // sort waiting list
   for(int i=0; i < waiting.size();) {
       Process &p = waiting.at(i);
       if (p.arrivalTime <= time) {
           ready.push_back(p);
           waiting.erase(waiting.begin()+i);
       } else i++;

   }
}

int rr() {
   vector<Process> ready; // ready process list
   vector<Process> waiting; // list of processes waiting for IO to finish
   for(auto &it: processList) ready.push_back(it); // add all processes read from input to ready list
   string lastP; // keep name of previous process

   int time;
   // loop until both ready list and waiting list are empty
   for (time = 0; !ready.empty() || !waiting.empty();) {
       transferFromWaitingToReady(ready,waiting,time);

       // add time column to output
       state += cell("   ");
       timeline += cell("   ");
       timeStr += timeCell(time);

       // if ready list is empty, add # column to output,
       // add 1 unit to time and continue to loop from start of loop
       if (ready.empty()) {
           emptyColumn();
           lastP = "#";
           time++;
           continue;
       }


       Process p = ready.at(0);

       // double check if process loaded into cpu
       if (p.arrivalTime <= time) {

           // if process is not the first process and
           // it isn't equal to last process, add a Dispatcher latency
           if(p.arrivalTime != 0 && lastP != p.name) {
               lastP = p.name;

               // add DL column to output
               time+= latency;
               state += cell("   ");
               timeline += cell("DL ");
               timeStr += cell("   ");

               // add time column to output
               state += cell("   ");
               timeline += cell("   ");
               timeStr += timeCell(time);
           }



           int burst = p.burstIoList.at(0); // burst of process
           int diff = burst - p.remQuantum;
           //
           if (diff <= 0) {
               // quantum is bigger than burst time


               time += p.getAndRemoveFirst();
               p.arrivalTime = time;
               p.remQuantum = -diff;

               // if burstIo list is empty it means process is finished,
               // otherwise send for IO operation
               if (p.burstIoList.empty()) {
                   // add terminate column to output
                   state += cell("  T");
                   timeline += cell(p.name + " ");
                   timeStr += cell("   ");
                   terminates++;
                   // after process termination, remove it from ready list
                   ready.erase(ready.begin());
               } else {
                   // IO operation
                   int io = p.getAndRemoveFirst();
                   p.arrivalTime = time + io;
                   p.remQuantum = max(0,p.remQuantum-io);
                   // if quantum remainder is zero, it needs to be moved to fcfs list,
                   // otherwise move to waiting list for IO operation
                   if (p.remQuantum == 0) fcfsList.push_back(p);
                   else waiting.push_back(p);
                   // after moving process, remove it from ready list
                   ready.erase(ready.begin());

                   // add Waiting column to output
                   state += cell(
                           p.arrivalTime < 10 ? "W " + to_string(p.arrivalTime) : "W" + to_string(p.arrivalTime));
                   timeline += cell(p.name + " ");
                   timeStr += cell("   ");
               }
           } else {
               // take a quantum from process and push it at back of fcfs list
               time += p.remQuantum;
               p.arrivalTime = time;
               p.replaceFirst(diff);
               ready.erase(ready.begin()); // remove process from ready list
               p.remQuantum = 0;
               fcfsList.push_back(p);

               // add process name column to output
               state += cell("   ");
               timeline += cell(p.name + " ");
               timeStr += cell("   ");
           }

       } else {
           // add an empty column to output
           // add 1 unit to time and continue from start of loop
           emptyColumn();
           lastP = "#";
           time++;
           continue;
       }

   }


   return time;
}