实验一 进程的管理实验教程
一、 实验目的
1、 理解进程的概念,明确进程和程序的区别。
2、 理解并发执行的实质。
3、 掌握进程的创建、睡眠、撤销等进程控制方法。
二、 实验要求及环境
1、用C语言编写程序,模拟实现创建新的进程;查看运行进程;换出某个进程;杀死进程等功能
三、 实验过程及方法
1 #include<conio.h>
2 #include<stdio.h>
3 #include<stdlib.h>
4 struct jincheng_type
5 {
6 int pid;
7 int youxian; int daxiao; int msg; int live;
8 };
9 struct jincheng_type neicun[20],waicun[20];
10
11 int shumu =0,pid=1;
12 int waicunnuber = 0;
13 int create()
14 {
15
16 printf("正在创建进程\n");
17 if(shumu>20)
18 printf("\n 请先结束或换出进程\n");
19 else{ printf("请输入新的进程pid :\n");
20 scanf("%d",&neicun[shumu].pid);
21 printf("请输入新的进程优先级 :\n");
22 scanf("%d",&neicun[shumu].youxian);
23 printf("请输入新的进程大小:\n");
24 scanf("%d",&neicun[shumu].daxiao);
25 printf("请输入新的进程消息 :");
26 scanf("%d",&neicun[shumu].msg);
27 }
28
29 neicun[shumu].live=0;
30 shumu++;
31
32 return 0;
33 }
34 void viewrun()
35 {
36 int vpid;
37 int i;
38 printf("正在查看进程\n");
39 printf("\n 请输入进程的pid\n");
40 scanf("%d",&vpid);
41 if(vpid>=0 )
42 {
43 for(i = 0; i < 20; i++)
44 {
45 if(vpid == neicun[i].pid )
46 {
47 printf("进程的pid是%d\n", neicun[i].pid);
48 printf("进程的优先级是%d\n",neicun[i].youxian);
49 printf("进程的大小是%d\n", neicun[i].daxiao);
50 printf("进程的消息是%d\n", neicun[i].msg);
51 printf("进程的状态是%d\n", neicun[i].live);
52 break;
53 }
54 }
55
56 }
57 else
58 printf("\n 所查看的进程不存在 \n");
59 printf("\n 请按回车退出查看\n");
60 vpid=getch();
61 }
62 void huanchu()
63 {
64 int pid1 = -1;
65 int pid2 = -1;
66 int pid11,pid22;
67 int i;
68 char c;
69 printf("正在换出进程\n");
70 printf("\n 请输入要换出的进程的pid:\n");
71 scanf("%d",&pid11);
72 printf("\n 请输入要换入的进程的pid:\n");
73 scanf("%d",&pid22);
74 for(i = 0; i < 20; i++)
75 {
76 if(pid11 == neicun[i].pid )
77 {
78 pid1 = i;
79 break;
80
81 }
82 }
83 for(i = 0; i < 20; i++)
84 {
85 if(pid22 == neicun[i].pid )
86 {
87 pid2 = i;
88 break;
89
90 }
91 }
92 /*
93 printf("进程的pid是%d\n", neicun[pid1].pid);
94 printf("进程的优先级是%d\n",neicun[pid1].youxian);
95 printf("进程的大小是%d\n", neicun[pid1].daxiao);
96 printf("进程的消息是%d\n", neicun[pid1].msg);
97 printf("进程的状态是%d\n", neicun[pid1].live);
98 */
99 if(pid1>=0&&pid1<20){
100 //if(neicun[pid1].youxian>neicun[pid2].youxian)
101 //{
102 neicun[pid1].live = -1;
103 neicun[pid2].live = 0;
104
105 waicun[19].pid =neicun[pid1].pid;
106 waicun[19].youxian =neicun[pid1].youxian;
107 waicun[19].daxiao =neicun[pid1].daxiao;
108 waicun[19].msg =neicun[pid1].msg;
109 waicun[19].live =neicun[pid1].live;
110 printf("*****\n");
111 neicun[pid1].pid=neicun[pid2].pid;
112 neicun[pid1].youxian=neicun[pid2].youxian;
113 neicun[pid1].daxiao=neicun[pid2].daxiao;
114 neicun[pid1].msg=neicun[pid2].msg;
115 neicun[pid1].live=neicun[pid2].live;
116
117 neicun[pid2].pid =waicun[19].pid;
118 neicun[pid2].youxian =waicun[19].youxian;
119 neicun[pid2].daxiao =waicun[19].daxiao;
120 neicun[pid2].msg =waicun[19].msg;
121 neicun[pid2].live =waicun[19].live;
122 /*
123 printf("进程的pid是%d\n", neicun[pid2].pid);
124 printf("进程的优先级是%d\n",neicun[pid2].youxian);
125 printf("进程的大小是%d\n", neicun[pid2].daxiao);
126 printf("进程的消息是%d\n", neicun[pid2].msg);
127 printf("进程的状态是%d\n", neicun[pid2].live);
128
129 printf("进程的pid是%d\n", neicun[pid1].pid);
130 printf("进程的优先级是%d\n",neicun[pid1].youxian);
131 printf("进程的大小是%d\n", neicun[pid1].daxiao);
132 printf("进程的消息是%d\n", neicun[pid1].msg);
133 printf("进程的状态是%d\n", neicun[pid1].live);
134 */
135 printf("换出的进程的pid是%d\n", waicun[19].pid);
136 printf("换出的进程的优先级是%d\n",waicun[19].youxian);
137 printf("换出的进程的大小是%d\n", waicun[19].daxiao);
138 printf("换出的进程的消息是%d\n", waicun[19].msg);
139 // }
140 //else printf("要换入的进程的优先级不够。\n");
141 }
142 else printf("要换出的进程不存在。\n");
143 printf("请输入回车键 退出换出作业。\n");
144 c=getch();
145 }
146 void kill(){
147 int kpid;
148 int i;
149 char c;
150 printf("正在杀死进程\n");
151 printf("\n 请输入的进程的pid:\n");
152 scanf("%d",&kpid);
153
154 if(kpid>=0&&kpid<20)
155 {
156 for(i = 0; i < 20; i++)
157 {
158 if(kpid == neicun[i].pid )
159 {
160 neicun[i].live =-1;
161 printf("杀死成功\n");
162 break;
163
164 }
165
166 }
167
168 printf("杀死成功\n");
169 }
170 else printf("\n 没有该进程\n");
171 printf("请输入回车键 退出换出作业。\n");
172 c=getch();
173 }
174 void tongxin()
175 {
176 int buffer;
177 int tpid1 = -1;
178 int tpid2 = -1;
179 int tpid11,tpid22;
180 int i;
181 char c;
182 printf("正在通信进程\n");
183 printf("\n 请输入通信源的进程的pid:\n");
184 scanf("%d",&tpid11);
185 printf("\n 请输入通信目的的进程的pid:\n");
186 scanf("%d",&tpid22);
187 for(i = 0; i < 20; i++)
188 {
189 if(tpid11 == neicun[i].pid )
190 {
191 tpid1 = i;
192 break;
193
194 }
195 }
196 for(i = 0; i < 20; i++)
197 {
198 if(tpid22 == neicun[i].pid )
199 {
200 tpid2 = i;
201 break;
202
203 }
204 }
205 if(tpid1>=0&&tpid1<20&&neicun[tpid1].live==1)
206 {
207 buffer=neicun[tpid1].msg ;
208 neicun[tpid1].msg=neicun[tpid2].msg;
209 neicun[tpid2].msg=buffer;
210 printf("\n 通信源的进程的消息是:%d\n",neicun[tpid2].msg);
211 printf("\n 通信目的的进程的消息是:%d\n",neicun[tpid1].msg);
212 }
213 else("\n所查看的进程不存在。\n");
214 printf("请输入回车键 退出换出作业。\n");
215 c=getch();
216 }
217
218 int main()
219 {
220 int m,n,i;
221 char a; n=1;
222 int maxyouxian = 21;
223 int maxi= -1;
224 struct jincheng_type max ;
225
226 for(i =0; i < 20; i ++)
227 {
228 neicun[i].live = -1;
229 }
230 while(n==1){
231 system("cls");
232 printf("***********************************");
233 printf("\n 进程演示系统 ");
234 printf("\n***********************************");
235 printf("\n 1 创建进程 2查看进程 ");
236 printf("\n 3 换出某个进程 4杀死进程 ");
237 printf("\n 5 进程之间通信 6退出 ");
238 printf("\n***********************************");
239 printf("\n 请选择(1~6)");
240 a=getch();
241 switch(a)
242 { case'1' :create(); break;
243 case'2' :viewrun(); break;
244 case'3' :huanchu(); break;
245 case'4' :kill(); break;
246 case'5' :tongxin(); break;
247 case'6' :exit(0); break;
248 default :n=0;
249 }
250 maxi = -1;
251 maxyouxian = 21;
252 for(i = 0; i < 20; i++)
253 {
254 if(maxyouxian > neicun[i].youxian && neicun[i].live == 0 )
255 {
256 maxi = i;
257 maxyouxian = neicun[i].youxian;
258 }
259
260 }
261 if(maxi != -1)
262 {
263 neicun[maxi].live = 1;
264 }
265
266 }
267 return 0;
268 }
四、实验结果及其分析
五、实验总结
1、进程,开始给每一个进程设定一个优先级数,对于优先级高的进程先调度,优先级低的进程后调度,在调度一个进程时,其他进程将处于就绪态,而正在被调度的进程应处于运行态。
2、优先级调度算法只是进程调度算法的一种,我们还应依照书本去学习进程调度的其它算法,以便更好地了解进程调度。
3、刚开始写的时候感觉很困难、因为我的c 语言不是很好,写起来很吃力,通过上网查阅资料完成。