|
|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
<p><font face="Verdana">C语言源程序代码-进程调度</font><br/></p><pre>/*8.3.2 源程序*/
& }$ @8 c: _0 ?+ |. p#include "stdio.h", ~8 W4 p5 b. `0 V
#include "stdlib.h"# s0 x5 I4 V/ w
#include "string.h"
, G+ h3 k1 q% R* ^" Gtypedef struct node
9 _5 X8 U- A2 y( y2 M{
8 e c* a0 O5 z2 ^! L char name[10]; /*进程标识符*/6 K8 Q* f2 a+ Y
int prio; /*进程优先数*/
' z9 a& h9 h, x2 k int round; /*进程时间轮转时间片*/+ l1 [0 g' }# b" p
int cputime; /*进程占用CPU时间*/
/ J4 x9 r+ A; t7 z6 W int needtime; /*进程到完成还要的时间*/5 P& d8 o. f& Y8 E; _8 |
int count; /*计数器*/; l c3 B' b4 R8 \& N
char state; /*进程的状态*/! Z$ ^! ?- P+ ^
struct node *next; /*链指针*/% f& m2 o) ^& }9 y2 Z7 B; p
}PCB;
7 I# T4 c, o! e9 v$ wPCB *finish,*ready,*tail,*run; /*队列指针*/
$ P$ x3 I! A8 {; {9 Yint N; /*进程数*/
6 s" r& h4 M* Q+ _) l+ E F g" }/*将就绪队列中的第一个进程投入运行*/
' U3 ?! @' A# i4 u* t6 `firstin()
& Y0 U3 C; [( ?3 X{
0 c" m! c3 H! ^! d run=ready; /*就绪队列头指针赋值给运行头指针*/
8 @5 O) J8 c+ n7 l: X' J$ ~& J run->state='R'; /*进程状态变为运行态*/
( D* l2 A* L/ ]/ |( e9 E ready=ready->next; /*就绪对列头指针后移到下一进程*/
* a$ `% ` |* G% y}
+ R$ w$ d& g' |/ d/*标题输出函数*/8 y2 U- {! W3 }1 u
void prt1(char a)4 R8 y7 R. T2 L" M
{# t7 t/ Z5 x9 w9 a
if(toupper(a)=='P') /*优先数法*/
* z1 f6 o2 @) o0 u# }8 k4 _. [. ] printf(" name cputime needtime priority state\n");
& d. `- _6 I) T6 J6 P else
' ?2 F! x4 L; @% j% g printf(" name cputime needtime count round state\n");0 y: f, ?# e) e+ U
}
7 S, ?- r; O9 K) n+ f6 M( ]/*进程PCB输出*/
3 b9 A( T$ d# ~: D! L9 v# R. mvoid prt2(char a,PCB *q)
; q4 |0 g$ F9 {% v{! u5 w0 `: S% Z, D: ]6 s% {: |
if(toupper(a)=='P') /*优先数法的输出*/4 T* R: C8 {! y0 a7 q" o
printf(" %-10s%-10d%-10d%-10d %c\n",q->name,$ U8 r9 M) G( ]8 _1 l. S
q->cputime,q->needtime,q->prio,q->state);
+ t! m3 z+ n# {" m! X! C( Q3 T else/*轮转法的输出*/
& _9 p, X Q, G, e) w* f# n printf(" %-10s%-10d%-10d%-10d%-10d %-c\n",q->name,3 t+ W f) l1 |# a" n$ q) N
q->cputime,q->needtime,q->count,q->round,q->state);
. l6 @" @9 e% T* Y( J}! [; k' E8 y. k+ \2 b1 r G0 I
/*输出函数*/
( _/ ^5 Z5 |3 N- xvoid prt(char algo)9 I* e' Y# F, i" | d8 o
{
8 T }3 P% \, Y/ @ PCB *p;
/ l* I* T& j1 m5 z* x prt1(algo); /*输出标题*/
$ j$ q7 t" z7 a: h9 h7 U if(run!=NULL) /*如果运行指针不空*/3 l# b- Z. ^ o' k* q) v# [
prt2(algo,run); /*输出当前正在运行的PCB*/
1 |9 H; r* n5 g% } p=ready; /*输出就绪队列PCB*/
$ w- M7 p& I) H7 d while(p!=NULL)
4 Y( g+ }4 L# W: U {
, F* O7 Q1 w, {9 W* K, j! g$ L6 p4 F+ L prt2(algo,p);
4 t9 u2 x3 h- ?' |7 Q) E# Q p=p->next;
# {1 @6 m$ P% x8 z0 {6 }. R }
4 `; t- ]5 F0 A6 S; i( ?) ? p=finish; /*输出完成队列的PCB*/( q" |6 V) I% K8 o3 ?" }
while(p!=NULL)' Y' Y D% D+ L" I) N
{
- @: G4 h6 f! Y9 d" k5 O4 S prt2(algo,p);
5 O8 A" n' N( R. ~1 I; m p=p->next;
3 P8 P$ `; d4 Q8 O' W" Q/ Z }
: ?" _6 T! P! G0 C! k getch(); /*压任意键继续*/
* I8 H. p4 B! S" W}
, y c2 j3 x! @8 r ]/ ~/*优先数的插入算法*/
5 d& J3 q' ]8 ?3 I% rinsert1(PCB *q)9 K9 v* P/ a2 w
{
9 o' `! G$ N0 p7 m$ ~! o6 e! k; z PCB *p1,*s,*r;/ _4 b; F: N7 O: C/ O5 s9 ~+ O, {
int b;' U% w t9 e9 r0 f9 G# H7 t
s=q; /*待插入的PCB指针*/& K4 G! h3 d2 G
p1=ready; /*就绪队列头指针*/4 I3 ]0 W5 f& `! n9 k/ w% d
r=p1; /*r做p1的前驱指针*/
. j1 }+ @' E. f6 m4 E b=1;
]2 z6 s/ {) ~ while((p1!=NULL)&&b) /*根据优先数确定插入位置*/0 J+ m) B# c0 c d$ H7 @1 C0 C
if(p1->prio>=s->prio)
0 R' W: @3 w s) R/ n2 | {' |! {9 M3 w/ l) _
r=p1;) Y! ]3 V& O; M6 b2 D5 ?- b; G. o
p1=p1->next; X5 g9 V, Y$ q- |0 s8 m
}
$ u' X; i/ P r0 ?" c else
+ u' J% j2 q. p; v9 ]+ x2 n b=0;% b. x) G$ H, a5 r; F
if(r!=p1) /*如果条件成立说明插入在r与p1之间*/2 G# p2 `5 R$ x& R4 K" C
{! l4 U+ w0 w! k8 z" t
r->next=s;
- ^9 C: X& d: p s->next=p1;
* h3 Z0 Z" I: Z& p2 k$ b$ X }
+ M! `* @) \& d* c, h# V else
+ @, U7 Z/ H5 i. N8 K4 ] {
$ O0 |7 j8 Y5 F8 n s->next=p1; /*否则插入在就绪队列的头*/2 _% h0 t, E' E3 I: H$ D+ m ^ b
ready=s;& b2 A2 z3 ^9 ]6 V' Z
}
T+ o* W) ?. G/ B! x}
3 e9 J4 H3 I, ^+ g3 W8 g2 ^0 _4 `/*轮转法插入函数*/
! m O: s( L2 O4 ?: J binsert2(PCB *p2)
6 g: r2 [) U* V1 L' J{3 {& N5 {5 v% M: W
tail->next=p2; /*将新的PCB插入在当前就绪队列的尾*/
/ R- {' g8 [% r tail=p2;* G2 ~+ \/ D+ k% C& ~' ?( K' @4 \- M
p2->next=NULL;4 S7 o3 b" j. E: F- F4 @
}, f2 _( d* ?! o5 n! }5 E
/*优先数创建初始PCB信息*/
9 J! E) I. c0 Nvoid create1(char alg)
! H) L7 x e5 \7 l8 ?" f{1 T9 J% B8 `; _* a/ t4 v
PCB *p;
5 u$ r/ r( x6 t4 d: B, r* P int i,time;
5 m8 W, t/ F3 w# I) x8 T+ c( |# l char na[10];6 `& b b$ c% F+ X
ready=NULL; /*就绪队列头指针*/
[; y* r0 i4 z! O finish=NULL; /*完成队列头指针*/
{0 }/ k5 S( O run=NULL; /*运行队列指针*/
3 Y. B7 C( d7 [- Q/ b& [' ] printf("Enter name and time of process\n"); /*输入进程标识和所需时间创建PCB*/
; b- a7 C3 g/ [/ W# o* c8 X. ^1 m8 d for(i=1;i<=N;i++)4 V) K3 f+ c) d( j& e
{
% }, P* L' X' l$ ` p=malloc(sizeof(PCB));% {) w, I7 G9 Z1 U3 [7 x8 R; `6 |
scanf("%s",na);
) u4 v: u0 a# O+ y& L$ X! M scanf("%d",&time);1 J2 ?. O3 F4 G! F z7 D
strcpy(p->name,na);
: Q* a! A3 F4 o; {+ ?2 | p->cputime=0;
4 R" Q" A5 x, z; Q2 { p->needtime=time;
. h. Y! p" U/ E/ E3 c+ {) @0 p0 q p->state='w';2 n/ a( P n1 J! f# J
p->prio=50-time;, m+ G" Z V3 [$ B) @
if(ready!=NULL) /*就绪队列不空调用插入函数插入*/
0 {, j$ {4 `( y# D; B. [( V insert1(p);
7 p" A$ l( g8 d- }- h1 s; D4 E: q else
, q" b2 P8 j3 u {
. a1 X" S2 h" L8 t9 J6 G% n p->next=ready; /*创建就绪队列的第一个PCB*/0 o& ]# D; Q R$ Q4 D0 [7 _% w
ready=p;. A3 B% T! P& U9 o7 S, S" g
}0 l/ U2 f. _5 B
}
( ~$ \/ l; c1 k9 I# | clrscr();' \4 V& ^) Z' `7 N
printf(" output of priority:\n"); S2 b' x7 k6 e6 K& J" G
printf("************************************************\n");
0 W2 P! T: X5 ?: P3 H prt(alg); /*输出进程PCB信息*/0 R6 m# d% n" I8 B. z$ k! V! U
run=ready; /*将就绪队列的第一个进程投入运行*/6 {% C7 l3 y- _; M5 S( Q2 ?
ready=ready->next;# T" _. m0 o2 ~1 q0 @% C
run->state='R';
6 l: h( d7 E0 s. G" x0 d2 t}& f) f* l* N% U3 V) I
/*轮转法创建进程PCB*/
' o; O% N7 M$ Gvoid create2(char alg)
x8 s; G s# F$ j: L. q# L/ m{/ j" [2 m8 M& Q8 C6 @3 Y# a
PCB *p;
6 T' S/ E% ]6 [- f; y int i,time;
2 V& ?. N) Y6 U7 v5 O, l% N- j1 h char na[10];
u/ G! D3 H* a ready=NULL;
9 S7 k( l. `- c H7 @6 L& @ finish=NULL;
0 ?8 z9 R+ x% E" I8 R! K" o4 k9 H run=NULL;
0 t5 F9 s+ D% u' k# u$ H printf("Enter name and time of round process\n");
7 p& n6 q0 ]9 J/ T {# \ for(i=1;i<=N;i++), b, T) Y2 i/ w2 o! V- O. F
{
) G! b8 Z: Y) I' S5 D r3 W p=malloc(sizeof(PCB));+ ]2 k! I# O" z6 d9 }7 \- V( [
scanf("%s",na);
% f6 Q" b" x) p4 ? scanf("%d",&time);
% ]3 _# y5 g- I) i" C+ i2 U- E; @ strcpy(p->name,na);0 m+ | \+ ?9 e
p->cputime=0;
% }9 f, B, ?& R! U0 T9 h$ {/ {$ S p->needtime=time;
/ i% x: y/ T3 Q& N4 s/ \0 O p->count=0; /*计数器*/
. f- v# q/ M: s0 d% P/ V( W p->state='w';
( v0 L# I' z9 C: G* k& d# Y+ @* _ p->round=2; /*时间片*/- ]3 [' K% g6 {: y
if(ready!=NULL)
* o2 L7 S7 G8 W( |. J0 m insert2(p);
5 l4 |6 p: |& M w9 H; j: ~* t else; \, @0 u( ?+ p) ~' V& D
{$ p( K9 y+ A4 c
p->next=ready;- R5 g( Z( {5 J
ready=p;5 S1 T" q+ Q2 [. n Q R5 f
tail=p;# v+ X8 Y) K! l% T
}" F0 [) e7 K! {8 [" n6 }& \$ W
}0 \$ q& l% P4 u5 e
clrscr();9 G2 [- V9 m4 P3 i# d: ^2 I
printf(" output of round\n");
: a$ f# F; `- r3 E: b& [1 z printf("************************************************\n");) X3 B" ]* ~% _3 n5 L$ E' o
prt(alg); /*输出进程PCB信息*/) S) O; A& t! N" m
run=ready; /*将就绪队列的第一个进程投入运行*/
$ E) S" ]! i- n b ready=ready->next;
% z! {; H; V- h" A! O run->state='R';
& Z- q6 m5 h! z: ~+ i( m}; [$ \6 B% q, v$ Z, R* P& }; f
/*优先数调度算法*/2 d# b4 k8 ~6 D& b( e
priority(char alg); e$ |2 }1 o: R; |5 H
{
* V6 G8 D0 ^/ u/ z/ S while(run!=NULL) /*当运行队列不空时,有进程正在运行*/) ?) H9 _6 e6 P6 j8 S
{
2 \! w) l0 x s) h, M run->cputime=run->cputime+1;
$ S! f1 Q0 ?0 A: Y5 ^ run->needtime=run->needtime-1;5 {5 Q k5 Z: C8 | Q5 b
run->prio=run->prio-3; /*每运行一次优先数降低3个单位*/
' c1 L: A$ o9 v" `3 S1 u/ u- z8 b if(run->needtime==0) /*如所需时间为0将其插入完成队列*/
' q C1 A' ?% q' p; m8 X5 j {
1 S d5 `) Y4 `& ?( x9 r1 L run->next=finish;6 f2 t9 ^7 G4 h% c4 O
finish=run;
( ^' r& ]# r' f/ }+ T$ P% C run->state='F'; /*置状态为完成态*/
1 N7 ~: c/ w7 ], B$ S* D; v run=NULL; /*运行队列头指针为空*// k( S% Y# {* ~
if(ready!=NULL) /*如就绪队列不空*/* b8 p8 n/ S# W' C. N0 T8 h% G
firstin(); /*将就绪对列的第一个进程投入运行*/
. e- F/ \& |$ I }' w t: Z% e0 p( @$ K% T- h1 S
else /*没有运行完同时优先数不是最大,则将其变为就绪态插入到就绪队列*/
6 C; }. Q9 s: j0 S if((ready!=NULL)&&(run->prio<ready->prio))0 \! v+ z1 ]: V) b' U
{4 F: s' C7 l2 P' V
run->state='W';
* K I) u4 B0 ^: O( o insert1(run);8 p1 Q* i% L- v. B$ s' I4 B
firstin(); /*将就绪队列的第一个进程投入运行*/
: D/ T+ r, \$ I4 E2 v% ? }
+ k6 R/ `8 r7 f! F* ]; K prt(alg); /*输出进程PCB信息*/# @! Z$ m) V& t; @
}
6 _8 c# s/ U0 ^) \, I# w2 d}& l2 ?4 R0 P: N
/*时间片轮转法*/
* g" S' e- b; o1 V1 {/ Qroundrun(char alg)
( j: c. p' P% s4 Y{, l1 L+ [) ~. P' x
while(run!=NULL)
7 w; F* i5 b w9 h* b' o, f! v {
( p" E* G2 z; P; H) J6 f run->cputime=run->cputime+1;
) q/ Z. _$ ~; E- e5 T" Z2 J* _ run->needtime=run->needtime-1;# ]% c) R/ C$ J- ~2 F
run->count=run->count+1;
% V# X# M9 q( e% I if(run->needtime==0)/*运行完将其变为完成态,插入完成队列*/! X' C. ~; P: N
{
- o4 h- V9 ]% C3 Z _% m run->next=finish;
8 _. J$ ~) @+ v* @ finish=run;
! w. ?& j4 w/ Z8 ] run->state='F';
% ~3 i9 ^0 z7 o4 I+ u( r run=NULL;& t. P/ l6 J2 A
if(ready!=NULL)( v" l! i1 Y2 S- Z0 J$ e. H/ G
firstin(); /*就绪对列不空,将第一个进程投入运行*/) m4 F) K5 K& b- X) W* o D: ~. O7 ~
}; w3 V* _2 {7 S3 u
else* u( D* o+ f2 x$ |* F2 ~& X& e
if(run->count==run->round) /*如果时间片到*/7 e; M- | g! _3 A% N4 [' B
{& l0 ] {% @ _/ P
run->count=0; /*计数器置0*/
2 p# \; q t6 G- z if(ready!=NULL) /*如就绪队列不空*/
F1 s! L) H G6 S {1 |4 o; f Q6 q' i" S
run->state='W'; /*将进程插入到就绪队列中等待轮转*/" I1 G8 w z7 C5 Y5 ]
insert2(run);
" K7 b* _$ f2 W% e. P5 O7 |5 J7 g firstin(); /*将就绪对列的第一个进程投入运行*/
9 j7 W* J1 l1 R4 t }8 P1 ^! T2 ]% o2 O/ ?$ S
}& u% ~# O' o7 t, n" F6 I3 x3 k
prt(alg); /*输出进程信息*/
+ @+ }$ ?, h; [* I( a% J }( i9 G$ c7 X+ O5 V7 M0 G% r
}
) ~+ r9 O/ d& F, F* U% Q- }% L/*主函数*/
* y/ W3 C3 d5 |) l5 C4 rmain()
! ^5 m% P0 B; f% S# Z4 |{' b& Z5 t$ O, O5 C$ y/ |
char algo; /*算法标记*/
+ r+ T1 {2 I v clrscr();
# ?- X9 z/ H( ]% U* e4 V% a- p printf("type the algorithm /R(priority/roundrobin)\n");
* M9 D+ {$ }( X# n- x. @) {( z5 F scanf("%c",&algo); /*输入字符确定算法*/
9 e. [ Q* G4 ~, T# `8 O( A printf("Enter process number\n");' K) F: t" p. M
scanf("%d",&N); /*输入进程数*/! q; F* m# P% L8 q
if(algo=='P'||algo=='p')
3 ]6 @2 K- A/ ?! ^8 t/ {% C {
+ i: b& l1 I4 H% \3 K create1(algo); /*优先数法*/5 v4 `9 p2 M( X8 [
priority(algo);3 W* h' }; b, k/ L
}
# {5 T# }5 |( G else$ I! d9 z/ S8 e; g1 L
if(algo=='R'||algo=='r')
7 D+ R* e: V, i+ b {/ C. [" g; P% D- Q( N& v% q$ q0 \9 i
create2(algo); /*轮转法*/
* E/ Z6 S" @. Q3 O3 M roundrun(algo);
" ]' k9 y6 ]2 f) V3 j0 G, t }& y4 @$ C" ]$ ]& C7 i: Q
} m: Z! o% o1 n
</pre> |
|