हमें n प्रक्रियाएं P1, P2, P3, ..., Pn दी गई हैं, जिनके साथ उनके बर्स्ट टाइम (burst time) और प्रायोरिटी (priority) जुड़े हुए हैं। कार्य प्रायोरिटी CPU शेड्यूलिंग एल्गोरिदम का उपयोग करके औसत वेटिंग टाइम, औसत टर्नअराउंड टाइम और प्रक्रिया निष्पादन का क्रम ढूंढना है।
वेटिंग टाइम और टर्नअराउंड टाइम क्या है?
टर्नअराउंड टाइम किसी प्रक्रिया के सबमिशन और उसके पूरा होने के बीच का समय अंतराल है।
टर्नअराउंड टाइम = प्रक्रिया का पूरा होना - प्रक्रिया का सबमिशन
वेटिंग टाइम टर्नअराउंड टाइम और बर्स्ट टाइम का अंतर है।
वेटिंग टाइम = टर्नअराउंड टाइम - बर्स्ट टाइम
प्रायोरिटी शेड्यूलिंग क्या है?
प्रायोरिटी शेड्यूलिंग में, प्रत्येक प्रक्रिया एक प्रायोरिटी (0-10) से जुड़ी होती है, जहां 0 सबसे कम और 10 सबसे अधिक प्रायोरिटी दर्शाता है। प्रायोरिटी आंतरिक या बाह्य रूप से परिभाषित की जा सकती है। यह प्रीमेप्टिव (preemptive) या नॉन-प्रीमेप्टिव (non-preemptive) हो सकती है।
- प्रीमेप्टिव: यदि नई आई प्रक्रिया की प्रायोरिटी वर्तमान में चल रही प्रक्रिया से अधिक है, तो CPU को प्रीमेप्ट कर दिया जाता है।
- नॉन-प्रीमेप्टिव: नई प्रक्रिया को रेडी क्यू के सिरे पर रखा जाता है।
नुकसान: अनिश्चित ब्लॉकिंग या स्टार्वेशन (starvation)। कम प्रायोरिटी वाली प्रक्रिया को अनिश्चित काल तक इंतजार करना पड़ सकता है।
उदाहरण
4 प्रक्रियाएं P1, P2, P3, P4 जिनके बर्स्ट टाइम और प्रायोरिटी:
| Process | Burst Time | Priority |
|---|---|---|
| P1 | 15 | 2 |
| P2 | 13 | 0 |
| P3 | 10 | 4 |
| P4 | 11 | 1 |
एल्गोरिदम
- Process स्ट्रक्चर बनाएं: pid, bt, priority
- कंपेरेटर फंक्शन: प्रायोरिटी के आधार पर सॉर्ट करें (उच्च प्रायोरिटी पहले)
- वेटिंग टाइम की गणना: wt[0]=0, wt[i] = pro[i-1].bt + wt[i-1]
- टर्नअराउंड टाइम की गणना: tat[i] = pro[i].bt + wt[i]
- औसत समय की गणना और प्रिंट करें
- शेड्यूलिंग फंक्शन: सॉर्ट करें, क्रम प्रिंट करें, औसत समय कॉल करें
C++ उदाहरण
#include <bits/stdc++.h>
using namespace std;
struct Process {
int pid;
int bt;
int priority;
};
bool compare(Process a, Process b) {
return (a.priority > b.priority);
}
void waitingtime(Process pro[], int n, int wt[]) {
wt[0] = 0;
for (int i = 1; i < n; i++)
wt[i] = pro[i-1].bt + wt[i-1];
}
void turnarround(Process pro[], int n, int wt[], int tat[]) {
for (int i = 0; i < n; i++)
tat[i] = pro[i].bt + wt[i];
}
void avgtime(Process pro[], int n) {
int wt[n], tat[n], total_wt = 0, total_tat = 0;
waitingtime(pro, n, wt);
turnarround(pro, n, wt, tat);
cout << "\nProcesses Burst time Waiting time Turn around time\n";
for (int i = 0; i < n; i++) {
total_wt += wt[i];
total_tat += tat[i];
cout << " " << pro[i].pid << "\t\t" << pro[i].bt << "\t " << wt[i] << "\t\t " << tat[i] << endl;
}
cout << "\nAverage waiting time = " << (float)total_wt / n;
cout << "\nAverage turn around time = " << (float)total_tat / n;
}
void scheduling(Process pro[], int n) {
sort(pro, pro + n, compare);
cout << "Order in which processes gets executed \n";
for (int i = 0; i < n; i++)
cout << pro[i].pid << " ";
avgtime(pro, n);
}
int main() {
Process pro[] = {{1, 10, 2}, {2, 5, 0}, {3, 8, 1}};
int n = sizeof(pro) / sizeof(pro[0]);
scheduling(pro, n);
return 0;
}
आउटपुट
Order in which processes gets executed: 1 3 2 Processes Burst time Waiting time Turn around time 1 10 0 10 3 8 10 18 2 5 18 23 Average waiting time = 9.33333 Average turn around time = 17