কম্পিউটার

রাউন্ড রবিন শিডিউলিংয়ের জন্য সি প্রোগ্রাম


আমাদেরকে n প্রসেস দেওয়া হয়েছে তাদের সংশ্লিষ্ট বিস্ফোরণের সময় এবং সময়ের কোয়ান্টামের সাথে এবং কাজটি হল গড় অপেক্ষার সময় এবং গড় পরিবর্তনের সময় খুঁজে বের করা এবং ফলাফল প্রদর্শন করা।

রাউন্ড রবিন শিডিউলিং কি?

রাউন্ড রবিন হল একটি CPU শিডিউলিং অ্যালগরিদম যা বিশেষ করে সময় ভাগ করে নেওয়ার সিস্টেমের জন্য ডিজাইন করা হয়েছে। এটি একটি পরিবর্তনের সাথে একটি FCFS শিডিউলিং অ্যালগরিদমের মতো যা রাউন্ড রবিনে প্রক্রিয়াগুলি একটি কোয়ান্টাম সময়ের আকারের সাথে আবদ্ধ থাকে। সময়ের একটি ছোট একক টাইম কোয়ান্টাম বা টাইম স্লাইস নামে পরিচিত। সময়ের কোয়ান্টাম 10 থেকে 100 মিলিসেকেন্ড পর্যন্ত হতে পারে। সিপিইউ প্রদত্ত সময়ের স্লাইস সহ প্রসেসগুলি চালানোর জন্য একটি বৃত্তাকার সারি হিসাবে প্রস্তুত সারিকে বিবেচনা করে। এটি অগ্রিম পদ্ধতি অনুসরণ করে কারণ প্রক্রিয়াগুলির জন্য নির্দিষ্ট সময় বরাদ্দ করা হয়। এর একমাত্র অসুবিধা হল প্রসঙ্গ পরিবর্তনের ওভারহেড।

আমাদের কী গণনা করতে হবে?

সমাপ্তির সময় প্রক্রিয়াটি কার্যকর করার জন্য প্রয়োজনীয় সময়

টার্নরাউন্ড টাইম একটি প্রক্রিয়া জমা দেওয়া এবং তার সমাপ্তির মধ্যে সময়ের ব্যবধান।

টার্নরাউন্ড টাইম =একটি প্রক্রিয়ার সমাপ্তি - একটি প্রক্রিয়া জমা দেওয়া

ওয়েটিং টাইম হল টার্নঅ্যারাউন্ড টাইম এবং বার্স্ট টাইমের মধ্যে পার্থক্য

অপেক্ষার সময় =টার্নঅ্যারাউন্ড সময় – বিস্ফোরণের সময়

উদাহরণ

আমাদেরকে 3টি প্রসেস দেওয়া হয়েছে P1, P2 এবং P3 সহ তাদের সংশ্লিষ্ট বিস্ফোরণের সময় 24, 3 এবং 3

প্রক্রিয়া বার্স্ট টাইম
P1 24
P2 3
P3 3

যেহেতু টাইম কোয়ান্টাম 4 মিলিসেকেন্ড, প্রক্রিয়া P1 প্রথম 4 মিলিসেকেন্ড পায় তবে এটির এক্সিকিউশন সম্পূর্ণ করতে আরও 20 মিলিসেকেন্ড প্রয়োজন কিন্তু CPU এটিকে প্রথমবার কোয়ান্টামের পরে প্রিম্পট করবে এবং CPU পরবর্তী প্রক্রিয়া P2-এ বরাদ্দ করা হবে। সারণীতে দেখানো হয়েছে, প্রক্রিয়া P2 এর কার্য সম্পাদন সম্পূর্ণ করতে মাত্র 3 মিলিসেকেন্ডের প্রয়োজন তাই CPU-কে 4 মিলিসেকেন্ডের পরিবর্তে 3 মিলিসেকেন্ড সময়ের জন্য বরাদ্দ করা হবে।

রাউন্ড রবিন শিডিউলিংয়ের জন্য সি প্রোগ্রাম

Gantt চার্ট ব্যবহার করে, গড় অপেক্ষার সময় নীচে দেওয়া হিসাবে গণনা করা হয় -

গড় অপেক্ষার সময় =17/3 =5.66 মিলিসেকেন্ড

অ্যালগরিদম

Start
Step 1-> In function int turnarroundtime(int processes[], int n, int bt[], int wt[], int tat[])
   Loop For i = 0 and i < n and i++
      Set tat[i] = bt[i] + wt[i]
   return 1
Step 2-> In function int waitingtime(int processes[], int n, int bt[], int wt[], int quantum)
Declare rem_bt[n]
   Loop For i = 0 and i < n and i++
      Set rem_bt[i] = bt[i]
      Set t = 0
   Loop While (1)
      Set done = true
   Loop For i = 0 and i < n and i++
      If rem_bt[i] > 0 then,
         Set done = false
      If rem_bt[i] > quantum then,
         Set t = t + quantum
         Set rem_bt[i] = rem_bt[i] - quantum
      Else
         Set t = t + rem_bt[i]
         Set wt[i] = t - bt[i]
         Set rem_bt[i] = 0
      If done == true then,
   Break
Step 3->In function int findavgTime(int processes[], int n, int bt[], int quantum)
   Declare and initialize wt[n], tat[n], total_wt = 0, total_tat = 0
   Call function waitingtime(processes, n, bt, wt, quantum)
   Call function turnarroundtime(processes, n, bt, wt, tat)
   Print "Processes Burst Time Waiting Time turnaround time "
   Loop For i=0 and i<n and i++
   Set total_wt = total_wt + wt[i]
   Set total_tat = total_tat + tat[i]
   Print the value i+1, bt[i], wt[i], tat[i]
   Print "Average waiting time = total_wt / n
   Print "Average turnaround time =total_tat / n
Step 4-> In function int main()
   Delcare and initialize processes[] = { 1, 2, 3}
   Declare and initialize n = sizeof processes / sizeof processes[0]
   Declare and initialize burst_time[] = {8, 6, 12}
   Set quantum = 2
   Call function findavgTime(processes, n, burst_time, quantum)

উদাহরণ

#include <stdio.h>
// Function to calculate turn around time
int turnarroundtime(int processes[], int n,
int bt[], int wt[], int tat[]) {
   // calculating turnaround time by adding
   // bt[i] + wt[i]
   for (int i = 0; i < n ; i++)
   tat[i] = bt[i] + wt[i];
   return 1;
}
// Function to find the waiting time for all
// processes
int waitingtime(int processes[], int n,
int bt[], int wt[], int quantum) {
   // Make a copy of burst times bt[] to store remaining
   // burst times.
   int rem_bt[n];
   for (int i = 0 ; i < n ; i++)
   rem_bt[i] = bt[i];
   int t = 0; // Current time
   // Keep traversing processes in round robin manner
   // until all of them are not done.
   while (1) {
      bool done = true;
      // Traverse all processes one by one repeatedly
      for (int i = 0 ; i < n; i++) {
         // If burst time of a process is greater than 0
         // then only need to process further
         if (rem_bt[i] > 0) {
            done = false; // There is a pending process
            if (rem_bt[i] > quantum) {
               // Increase the value of t i.e. shows
               // how much time a process has been processed
               t += quantum;
               // Decrease the burst_time of current process
               // by quantum
               rem_bt[i] -= quantum;
            }
            // If burst time is smaller than or equal to
            // quantum. Last cycle for this process
            else {
               // Increase the value of t i.e. shows
               // how much time a process has been processed
               t = t + rem_bt[i];
               // Waiting time is current time minus time
               // used by this process
               wt[i] = t - bt[i];
               // As the process gets fully executed
               // make its remaining burst time = 0
               rem_bt[i] = 0;
            }
         }
      }
      // If all processes are done
      if (done == true)
         break;
   }
   return 1;
}
// Function to calculate average time
int findavgTime(int processes[], int n, int bt[],
int quantum) {
   int wt[n], tat[n], total_wt = 0, total_tat = 0;
   // Function to find waiting time of all processes
   waitingtime(processes, n, bt, wt, quantum);
   // Function to find turn around time for all processes
   turnarroundtime(processes, n, bt, wt, tat);
   // Display processes along with all details
   printf("Processes Burst Time Waiting Time turnaround time\n");
   // Calculate total waiting time and total turn
   // around time
   for (int i=0; i<n; i++) {
      total_wt = total_wt + wt[i];
      total_tat = total_tat + tat[i];
      printf("\t%d\t\t\t%d\t\t\t%d\t\t\t%d\n",i+1, bt[i], wt[i], tat[i]);
   }
   printf("Average waiting time = %f", (float)total_wt / (float)n);
   printf("\nAverage turnaround time = %f\n", (float)total_tat / (float)n);
   return 1;
}
// main function
int main() {
   // process id's
   int processes[] = { 1, 2, 3};
   int n = sizeof processes / sizeof processes[0];
   // Burst time of all processes
   int burst_time[] = {8, 6, 12};
   // Time quantum
   int quantum = 2;
   findavgTime(processes, n, burst_time, quantum);
   return 0;
}

আউটপুট

রাউন্ড রবিন শিডিউলিংয়ের জন্য সি প্রোগ্রাম


  1. সি++ প্রোগ্রাম ফর শর্টেস্ট জব ফার্স্ট (এসজেএফ) শিডিউলিং (প্রিমম্পটিভ)

  2. সি++ প্রোগ্রাম ফর শর্টেস্ট জব ফার্স্ট (এসজেএফ) শিডিউলিংয়ের জন্য (অ-প্রাথমিক)

  3. অগ্রাধিকার নির্ধারণের জন্য C++ প্রোগ্রাম

  4. সহজ সময়সূচীর জন্য সেরা সময় অঞ্চল রূপান্তরকারী