原文:
先决条件– 优先级调度是一种非抢占式算法,也是批处理系统中最常见的调度算法之一。如果两个进程具有相同的到达时间,则为每个进程分配第一个到达时间(先减去到达时间的进程),然后与优先级进行比较(先比较最高的进程)。此外,如果两个进程具有相同的优先级,则比较进程号(先减去进程号)。当所有过程都被执行时,这个过程被重复。
实施–
- 首先输入进程及其到达时间、突发时间和优先级。
- 第一个进程将调度到达时间最低的进程,如果两个或多个进程到达时间最低,则优先级较高的进程将首先调度。
- 现在将根据流程的到达时间和优先级安排进一步的流程。(这里我们假设优先级越低,优先级越高)。如果两个进程优先级相同,则按照进程号排序。 注:在问题中,他们会明确提到,哪个号码优先级高,哪个号码优先级低。
- 一旦所有的过程都已经到达,我们就可以根据它们的优先级来安排它们。
甘特图–
示例–
input :
process no-> 1 2 3 4 5
arrival time-> 0 1 3 2 4
burst time-> 3 6 1 2 4
priority-> 3 4 9 7 8
output :
process_no arrival_time burst_time complete_time turn_around_time waiting_time
1 0 3 3 3 0
2 1 6 9 8 2
3 3 1 16 13 12
4 2 2 11 9 7
5 4 4 15 11 7
average waiting time is : 5.6
average turn around time is : 8.8
c
// c implementation for priority scheduling with
//different arrival time priority scheduling
/*1\. sort the processes according to arrival time
2\. if arrival time is same the acc to priority
3\. apply fcfs
*/
#include
using namespace std;
#define totalprocess 5
// making a struct to hold the given input
struct process
{
int at,bt,pr,pno;
};
process proc[50];
/*
writing comparator function to sort according to priority if
arrival time is same
*/
bool comp(process a,process b)
{
if(a.at == b.at)
{
return a.pr
java 语言(一种计算机语言,尤用于创建网站)
// java implementation for priority scheduling with
//different arrival time priority scheduling
import java.util.*;
/// data structure
class process {
int at, bt, pri, pno;
process(int pno, int at, int bt, int pri)
{
this.pno = pno;
this.pri = pri;
this.at = at;
this.bt = bt;
}
}
/// gantt chart structure
class gchart {
// process number, start time, complete time,
// turn around time, waiting time
int pno, stime, ctime, wtime, ttime;
}
// user define comparative method (first arrival first serve,
// if arrival time same then heigh priority first)
class mycomparator implements comparator {
public int compare(object o1, object o2)
{
process p1 = (process)o1;
process p2 = (process)o2;
if (p1.at < p2.at)
return (-1);
else if (p1.at == p2.at && p1.pri > p2.pri)
return (-1);
else
return (1);
}
}
// class to find gantt chart
class findgantchart {
void findgc(linkedlist queue)
{
// initial time = 0
int time = 0;
// priority queue sort data according
// to arrival time or priority (ready queue)
treeset prique = new treeset(new mycomparator());
// link list for store processes data
linkedlist result = new linkedlist();
// process in ready queue from new state queue
while (queue.size() > 0)
prique.add((process)queue.removefirst());
iterator it = prique.iterator();
// time set to according to first process
time = ((process)prique.first()).at;
// scheduling process
while (it.hasnext()) {
// dispatcher dispatch the
// process ready to running state
process obj = (process)it.next();
gchart gc1 = new gchart();
gc1.pno = obj.pno;
gc1.stime = time;
time = obj.bt;
gc1.ctime = time;
gc1.ttime = gc1.ctime - obj.at;
gc1.wtime = gc1.ttime - obj.bt;
/// store the exxtreted process
result.add(gc1);
}
// create object of output class and call method
new resultoutput(result);
}
}
python 3
# python3 implementation for priority scheduling with
# different arrival time priority scheduling
"""1\. sort the processes according to arrival time
2\. if arrival time is same the acc to priority
3\. apply fcfs """
totalprocess = 5
proc = []
for i in range(5):
l = []
for j in range(4):
l.append(0)
proc.append(l)
# using fcfs algorithm to find waiting time
def get_wt_time( wt):
# declaring service array that stores
# cumulative burst time
service = [0] * 5
# initialising initial elements
# of the arrays
service[0] = 0
wt[0] = 0
for i in range(1, totalprocess):
service[i] = proc[i - 1][1] service[i - 1]
wt[i] = service[i] - proc[i][0] 1
# if waiting time is negative,
# change it o zero
if(wt[i] < 0) :
wt[i] = 0
def get_tat_time(tat, wt):
# filling turnaroundtime array
for i in range(totalprocess):
tat[i] = proc[i][1] wt[i]
def findgc():
# declare waiting time and
# turnaround time array
wt = [0] * 5
tat = [0] * 5
wavg = 0
tavg = 0
# function call to find waiting time array
get_wt_time(wt)
# function call to find turnaround time
get_tat_time(tat, wt)
stime = [0] * 5
ctime = [0] * 5
stime[0] = 1
ctime[0] = stime[0] tat[0]
# calculating starting and ending time
for i in range(1, totalprocess):
stime[i] = ctime[i - 1]
ctime[i] = stime[i] tat[i] - wt[i]
print("process_no\tstart_time\tcomplete_time",
"\tturn_around_time\twaiting_time")
# display the process details
for i in range(totalprocess):
wavg = wt[i]
tavg = tat[i]
print(proc[i][3], "\t\t", stime[i],
"\t\t", end = " ")
print(ctime[i], "\t\t", tat[i], "\t\t\t", wt[i])
# display the average waiting time
# and average turn around time
print("average waiting time is : ", end = " ")
print(wavg / totalprocess)
print("average turnaround time : " , end = " ")
print(tavg / totalprocess)
# driver code
if __name__ =="__main__":
arrivaltime = [1, 2, 3, 4, 5]
bursttime = [3, 5, 1, 7, 4]
priority = [3, 4, 1, 7, 8]
for i in range(totalprocess):
proc[i][0] = arrivaltime[i]
proc[i][1] = bursttime[i]
proc[i][2] = priority[i]
proc[i][3] = i 1
# using inbuilt sort function
proc = sorted (proc, key = lambda x:x[2])
proc = sorted (proc)
# calling function findgc for
# finding gantt chart
findgc()
# this code is contributed by
# shubham singh(shubhamsingh10)
输出:
process_no start_time complete_time turn_around_time waiting_time
1 1 4 3 0
2 5 10 8 3
3 4 5 2 1
4 10 17 13 6
5 17 21 16 12
average waiting time is : 4.4
average turn around time is : 8.4
时间复杂度: o(n * logn),其中 n 为进程总数。
辅助空间: o(n)
本文由 供稿。如果你喜欢 geeksforgeeks 并想投稿,你也可以使用写一篇文章或者把你的文章邮寄到 contribute@geeksforgeeks.org。看到你的文章出现在极客博客pg电子试玩链接主页上,帮助其他极客。
如果发现有不正确的地方,或者想分享更多关于上述话题的信息,请写评论。
麻将胡了pg电子网站的版权属于:月萌api www.moonapi.com,转载请注明出处