进程管理
进程管理是操作系统中的重要功能,用来创建进程、撤消进程、实现进程状态转换,它提供了在可运行的进程之间复用CPU的方法。在进程管理中,进程调度是核心,因为在采用多道程序设计的系统中,往往有若干个进程同时处于就绪状态,当就绪进程个数大于处理器数目时,就必须依照某种策略决定哪些进程优先占用处理器。
调度算法
编写允许进程并行执行的进程调度程序,在常用的进程(作业)调度算法:先来先服务算法、时间片轮转法、最高响应比优先算法、高优先权优先算法等调度算法中至少选择2种调度算法进行模拟,并输出平均周转时间和平均带权周转时间。
本次模拟的算法为:先来先服务、非抢占式静态优先级算法
源代码
- 先来先服务
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104#include <bits/stdc++.h> #include <iostream> #include <stdio.h> #include <math.h> #include <stdlib.h> #include <string> #include <ctype.h> #include <string.h> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #include <stack> #define maxn 100 using namespace std; typedef struct Node{ char name; double daoda; double yunxing; double youxian; double start; double end; double zhouzhuan; double daiquan; }PCB; PCB pcb[maxn]; bool cmp(Node a,Node b){ return a.daoda < b.daoda; } //平均 double T; double S; int n;//进程数量 void First(){ cout<<endl; cout<<"tt先来先服务算法:"<<endl; cout<<endl; sort(pcb,pcb+n,cmp);//把进程根据到达的时间从小到达进行排序 cout<<"进程运行的顺序为:"<<endl; cout<<pcb[0].name; for(int i=1;i<n;i++){ cout<<"-->"<<pcb[i].name; } cout<<endl; double time = max(0.0,pcb[0].daoda); double t = 0;//保存周转时间的和 double s = 0; //保存带权周转时间和 for(int i=0;i<n;i++){ pcb[i].start = time; pcb[i].end = time + pcb[i].yunxing; pcb[i].zhouzhuan = pcb[i].end - pcb[i].daoda; pcb[i].daiquan = pcb[i].zhouzhuan / pcb[i].yunxing; time += pcb[i].yunxing; //当这时的结束时间小于下一个进程的到达时间的话,就要重新更新time if(i != n-1){ time = max(time,pcb[i+1].daoda); } //求两个和 t += pcb[i].zhouzhuan; s += pcb[i].daiquan; } T = t / n; S = s / n; cout<<"平均周转时间:"<<T<<endl; cout<<"平均带权周转时间:"<<S<<endl; } int main() { int i; cout<<"输入进程个数:"<<endl; cin >> n; cout<<"输入各个进程名字到达时间,运行时间及优先级:"<<endl; for(i=0;i<n;i++){ cin>>pcb[i].name>>pcb[i].daoda>>pcb[i].yunxing>>pcb[i].youxian; } cout<<"显示各个进程的参数:"<<endl; cout<<"进程名"<<"t"<<" 到达时间"<<"t"<<" 运行时间"<<"t"<<" 优先级"<<endl; for(i=0;i<n;i++){ cout<<"Job"<<i+1<<"tt"<<pcb[i].daoda<<"tt"<<pcb[i].yunxing<<"tt"<<pcb[i].youxian<<endl; } First(); T = 0; S = 0; return 0; }
- 非抢占式静态优先级
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107#include <bits/stdc++.h> #include <iostream> #include <stdio.h> #include <math.h> #include <stdlib.h> #include <string> #include <ctype.h> #include <string.h> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #include <stack> #define maxn 100 using namespace std; typedef struct Node{ char name; double daoda; double yunxing; double youxian; double start; double end; double zhouzhuan; double daiquan; }PCB; PCB pcb[maxn]; //平均 double T; double S; int n;//进程数量 //根据到达的时间先进行排序一次 bool cmp1(Node a,Node b){ return a.daoda < b.daoda; } //根据优先级进行排序 bool cmp2(Node a,Node b){ return a.youxian > b.youxian; } //非抢占 静态优先级 void jingtaiyouxianji() { sort(pcb,pcb+n,cmp1); cout<<"===============静态非抢占式优先级调度算法==============="<<endl; //先要执行第一个到达的进程 //time为记录的总时间 double time = max(0.0,pcb[0].daoda); time += pcb[0].yunxing; int cnt = 0; double t = 0,s = 0; for(int i=1;i<n;i++){ //判断当前时间是否大于后面的进程的到达时间 if(time > pcb[i].daoda){ cnt++; } //根据进程的优先级进行排序 sort(pcb+i,pcb+cnt+i,cmp2); pcb[i].start = time; pcb[i].end = time + pcb[i].yunxing; pcb[i].zhouzhuan = pcb[i].end - pcb[i].daoda; pcb[i].daiquan = pcb[i].zhouzhuan / pcb[i].yunxing; //更新一下当前总时间 time += pcb[i].yunxing; //求两个和 t += pcb[i].zhouzhuan; s += pcb[i].daiquan; } T = t / n; S = s / n; cout<<"平均周转时间:"<<T<<endl; cout<<"平均带权周转时间:"<<S<<endl; } int main() { int i; cout<<"输入进程个数:"<<endl; cin >> n; cout<<"输入各个进程名字到达时间,运行时间及优先级:"<<endl; for(i=0;i<n;i++){ cin>>pcb[i].name>>pcb[i].daoda>>pcb[i].yunxing>>pcb[i].youxian; } cout<<"显示各个进程的参数:"<<endl; cout<<"进程名"<<"t"<<" 到达时间"<<"t"<<" 运行时间"<<"t"<<" 优先级"<<endl; for(i=0;i<n;i++){ cout<<"Job"<<i+1<<"tt"<<pcb[i].daoda<<"tt"<<pcb[i].yunxing<<"tt"<<pcb[i].youxian<<endl; } jingtaiyouxianji(); return 0; }
最后
以上就是爱撒娇含羞草最近收集整理的关于进程调度算法模拟与实现的全部内容,更多相关进程调度算法模拟与实现内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复