|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
<p><font face="Verdana">C语言源程序代码-进程调度</font><br/></p><pre>/*8.3.2 源程序*/7 t9 P9 W& s+ p1 `1 W
#include "stdio.h"
g# n% Y: h5 ^# e7 t#include "stdlib.h"
O6 r+ z) w6 S( ~#include "string.h"* |; W$ h$ b% @8 Y9 u6 o" L
typedef struct node
4 c, q( `; g2 K, o( }/ v# `{
% w! q& b) d# D# { s char name[10]; /*进程标识符*/4 U7 t; O: A n4 O
int prio; /*进程优先数*/4 M, I* c& c4 q: C- c
int round; /*进程时间轮转时间片*/& m' e$ l: [( S# J/ @- U; N% y
int cputime; /*进程占用CPU时间*/
$ w9 j5 f2 ?' o; Z- h, ~# h7 W ]4 I int needtime; /*进程到完成还要的时间*/
. O" `" n* L, n7 q int count; /*计数器*/& l0 I8 N" c6 H3 A; B) z# ]& ]6 u
char state; /*进程的状态*/
5 A* k5 M2 B# n: ^" Z struct node *next; /*链指针*/
" H6 R. V: y w X}PCB;1 r. _4 u5 C( M. H6 [
PCB *finish,*ready,*tail,*run; /*队列指针*// H0 o& z p J9 `
int N; /*进程数*/
! b# t5 T- H" E: y2 V" n/*将就绪队列中的第一个进程投入运行*/
, S. y4 s/ `3 x0 ^1 m0 Y6 Wfirstin()
3 D8 v' O, W7 f* V1 J9 h{2 X( ~* }+ O, t) U
run=ready; /*就绪队列头指针赋值给运行头指针*/
9 c! I+ s. R/ C7 j& O3 b1 L/ `! L run->state='R'; /*进程状态变为运行态*/! D$ C+ d! C3 ?2 |- ~
ready=ready->next; /*就绪对列头指针后移到下一进程*/
& i- t% t1 r1 ]5 N' v}
& |; y3 Z; s) O, e/*标题输出函数*/5 y( \; s% \ Y. d, e6 v$ f
void prt1(char a)
% }4 O) n1 x/ S: e! d* z8 x) b* ?$ U7 v{# R* O! a( t4 e: ^6 `$ M$ N+ C
if(toupper(a)=='P') /*优先数法*/
+ y# m6 A: F, k: T+ s printf(" name cputime needtime priority state\n");
# N0 V* X l) z; @8 x else
( B5 B; ~& F) A( T. s printf(" name cputime needtime count round state\n");; q- _7 Q, Z6 b5 E/ B
}
; A0 d7 |) Q; f8 }" D1 G# q/*进程PCB输出*/- S/ X1 z) n' t/ L# S0 H
void prt2(char a,PCB *q)
; Q' x. E0 A' q{2 T4 e) @. ^4 Y; G m
if(toupper(a)=='P') /*优先数法的输出*/
: y R! h# ^( c9 l& f printf(" %-10s%-10d%-10d%-10d %c\n",q->name,
# Q k6 u y" q R0 ^8 z3 G# J! r q->cputime,q->needtime,q->prio,q->state);
6 L [/ L0 u) i1 M( v7 a% Q# g else/*轮转法的输出*// |) i1 z, G: Q3 b8 n2 y; \
printf(" %-10s%-10d%-10d%-10d%-10d %-c\n",q->name,
; k3 N& R* {& E5 N q->cputime,q->needtime,q->count,q->round,q->state);6 ^# T* d. \. g- [4 I i0 f
}
; x9 B6 E; H7 ?9 P6 g/*输出函数*/1 y6 V& d& I+ x
void prt(char algo)
7 P8 D6 U# B. M7 \{. a) ]; g* b, l' f9 B; n5 O! Q
PCB *p;, F8 U) B6 v+ k5 o, ]; }9 s
prt1(algo); /*输出标题*/
; J, A8 w4 R( G0 G- \3 O/ V6 _: `. j if(run!=NULL) /*如果运行指针不空*/
7 W! e& L) V: X) ?+ ?. S! ? prt2(algo,run); /*输出当前正在运行的PCB*/) v+ U4 V( Q$ N; E! ^; x
p=ready; /*输出就绪队列PCB*/0 u+ a$ G& }, g: i5 a
while(p!=NULL)
9 K8 e9 D( Q8 Y7 s9 j4 g {$ g; u; Z( C/ t+ v; m& y |. _
prt2(algo,p);
4 N. f) f- b- Q ?- V- Z p=p->next;7 T' ^- O n% l. ^. n( |* F" W
}
& z( |( p5 c( b: e: N; b3 y p=finish; /*输出完成队列的PCB*/: }5 l% A+ j& E3 w D3 e+ ~8 M
while(p!=NULL)* x9 A$ q0 H Z3 w* u% Y
{% S! K+ t* X6 V8 Z' C
prt2(algo,p);
) R8 j" ~5 L) t) q1 { D p=p->next;' W9 @8 t, V# _) {8 c: V- i8 P
}
8 p1 ?, O4 I; R; F& `' L* R getch(); /*压任意键继续*/: C, E' @. h" ^4 \* R
}; T# e4 \9 J$ l. A t
/*优先数的插入算法*/
. ]/ K- ^7 J; X- B) `insert1(PCB *q)
) ]- Q$ z H' ^6 v{
+ p t# O$ q0 v+ X PCB *p1,*s,*r;
?6 }' E. G- x( [" d int b;
+ z/ O; M* h8 |! y) ~ s=q; /*待插入的PCB指针*/
/ N9 p/ c! a" Q L p1=ready; /*就绪队列头指针*/
( e6 f t; J6 }& ]0 j) B& k* P r=p1; /*r做p1的前驱指针*/" K, P8 x/ H4 J1 [5 m
b=1;: C+ y7 e" b- u9 e. v3 j
while((p1!=NULL)&&b) /*根据优先数确定插入位置*/
) J( }1 c# z! H& m4 b8 u0 x if(p1->prio>=s->prio)( E9 K. V8 l- C% I9 x+ P
{
( w9 [9 R5 Z& l, `7 v r=p1;: _5 x/ _- ~( l. ^' m
p1=p1->next;3 i5 [1 |9 N* w, |$ M& J. W
}7 E6 s; y* o }0 |
else
9 W2 z: y) v- u& E5 _ b=0;
7 I1 `1 ~' y1 X& U1 L X6 q7 ?9 h& M if(r!=p1) /*如果条件成立说明插入在r与p1之间*/
% F9 p) F& r& q9 @+ n6 F- Y {8 N" C& F" m1 i0 S
r->next=s;' I4 @, X3 Q/ T) }8 w3 }5 H
s->next=p1;
! P: @. Z+ r3 i2 b M }3 S) c# P% F( @; G' o3 y
else
. v7 z9 o* ^2 Y6 v( i" p {
& q3 a, G: b3 q! \) R s->next=p1; /*否则插入在就绪队列的头*/. x# W, L. v- J1 u! q/ B& f
ready=s;7 f: s2 ^$ o- Q6 Y6 ~& Z$ H
}: R0 q; j: z' I- f' ` {; W
}( g/ \/ X( c5 m6 @4 ]
/*轮转法插入函数*/) a6 ]* e9 ~/ c n1 t
insert2(PCB *p2). k3 X$ W4 ]- a4 }% Y) L; B( R
{
/ k T9 Z- p: u* b2 U tail->next=p2; /*将新的PCB插入在当前就绪队列的尾*/* g% ]7 t4 e% U" h3 m, _
tail=p2;7 m/ j/ F( `8 @* O
p2->next=NULL;8 U' m! {9 J, q
}+ u z# `6 W7 A( @) W& v& l: R4 u
/*优先数创建初始PCB信息*/
8 U u8 }; K; j' c$ i* [ Ovoid create1(char alg)( e/ \, e" G2 M7 n5 W6 [ o
{7 T( E1 F6 `# f& q
PCB *p;/ s# ]( b0 _) ]4 T2 {
int i,time;
: V/ ^, n% C9 o4 \ char na[10];
|, O4 V8 h: j2 d P# z6 F8 x ready=NULL; /*就绪队列头指针*/
o, G0 N9 F2 e7 E" x5 u% Q finish=NULL; /*完成队列头指针*/
* n3 [( F1 H E0 n. h run=NULL; /*运行队列指针*/9 f9 V- R I! X5 T+ L/ o- x
printf("Enter name and time of process\n"); /*输入进程标识和所需时间创建PCB*/
4 `# s. M) p- y for(i=1;i<=N;i++)0 e7 s5 v# u8 F2 `( x- X6 _; S
{
0 i. H& v1 D2 [" F p=malloc(sizeof(PCB));
/ z% U% D2 o% A( I scanf("%s",na);6 u' b; T# I- p6 {
scanf("%d",&time);
m7 m4 G( P( r5 n strcpy(p->name,na);# g9 Z" g( t) b
p->cputime=0;" ?" G- H7 r* ~* |
p->needtime=time;
( o# m0 o/ N' B E2 ~3 @ p->state='w';
- G# N2 u) \' E# g p->prio=50-time;
: ^% b8 b( |7 J" N3 M5 P2 X$ j if(ready!=NULL) /*就绪队列不空调用插入函数插入*/4 ]2 p8 f! G! f% t; u R6 b
insert1(p);
6 K* @; d" |9 d; h, u else
5 }7 s% Y* u) d- L7 p- \$ }6 n) r, N { \1 q8 v, Z3 r u% W
p->next=ready; /*创建就绪队列的第一个PCB*/' U9 A7 a: q$ c: h3 w
ready=p;+ w" \# \2 G1 B9 i5 l1 h/ l
}
3 t! f; z2 @; ^3 R }
0 z, P( M* R8 P clrscr();& g4 ^2 _# [3 q& Q# U
printf(" output of priority:\n");
0 `. n$ @# k+ U. S9 `) s printf("************************************************\n");
- |( P" R F& a& q prt(alg); /*输出进程PCB信息*/
7 N m+ f9 [0 P" k& Y5 z" c& } run=ready; /*将就绪队列的第一个进程投入运行*/0 H* R- i% |$ y! N% O
ready=ready->next;
/ O5 q! A; s' j5 d+ }1 p run->state='R';* ^7 u) k5 a( e6 B5 s0 g+ \6 @
}8 S" P- K+ D: E# h
/*轮转法创建进程PCB*/
" l0 k+ y5 e6 c: _void create2(char alg)
# x4 G! S6 n1 b, ^' K; C0 n6 Z/ G1 n{! r1 G- `: F/ }$ a9 y& I. I
PCB *p;$ b6 V6 A/ b) H
int i,time;( y% k/ \9 }2 a+ [! c {
char na[10];
k* `6 A, m% S' t P ready=NULL;( p6 M, a* I. }9 i/ p( J; D4 w
finish=NULL;
+ B9 ~# ?# ]; b; F6 \* J* ^! H run=NULL;
# a$ M9 I V& v7 G! c printf("Enter name and time of round process\n");3 ~8 w: t3 v0 y+ h1 }. A' m
for(i=1;i<=N;i++): {+ R; v" l3 m
{* }6 N9 {2 U9 }5 {5 N( W
p=malloc(sizeof(PCB));) {& t) v8 V* D4 X
scanf("%s",na);' S3 X# [2 D2 S! ^
scanf("%d",&time);
3 u8 B. u, ]" a" o2 ]6 Q strcpy(p->name,na);
. m. {1 ^* \' q p->cputime=0;
% y! s+ @6 |% v p->needtime=time;$ k) I; b {9 ?3 J I2 N7 W
p->count=0; /*计数器*/( m% T$ r& Y9 g9 Q# v
p->state='w';
2 S% u1 ]/ B; ~3 Q p->round=2; /*时间片*/
( [4 y o( _ q if(ready!=NULL)/ o: _8 U; \& l# `- J4 |4 S( e
insert2(p);
v P; c: @, B3 L& f else
! H6 n- o; ]0 r+ m) _ {
* o% h' V7 e' }( ?/ o4 P- ]: f p->next=ready;
( f! ^$ z5 Q+ Q# A% x5 l' j, [ ready=p;3 x1 y' p. m& c& W) D
tail=p;
- O: b7 j: A! w- U }
" Q* }/ l. x: J' e/ k }7 E. s; Z' ]/ H) d! i
clrscr();
. {! _8 q1 G1 J# t8 h, K! G, C printf(" output of round\n");7 B+ B; D' O7 S9 e
printf("************************************************\n");& c8 h( ]6 p* u1 F
prt(alg); /*输出进程PCB信息*/
7 H8 L, A) S# { run=ready; /*将就绪队列的第一个进程投入运行*/
/ F( W& v) {$ h6 L ready=ready->next;" P/ Z1 f3 H4 Z8 h t
run->state='R';
( j- q/ l; ~) J+ M9 a/ F}! O5 c& u3 y0 m$ D# I1 U! H d+ ^
/*优先数调度算法*/9 V& J1 }9 K4 _/ P$ K+ N* j6 @ {3 v
priority(char alg)
- w" r+ }5 }! F: W1 H{% P7 s1 f8 y8 T! J4 y
while(run!=NULL) /*当运行队列不空时,有进程正在运行*/
* z2 W/ h* T$ G# D1 G2 b {
; X' O# f& i4 o% v' N run->cputime=run->cputime+1;/ m1 y- _. L# B$ P; Y! ^+ H0 |
run->needtime=run->needtime-1;
1 H# t+ h7 B7 n0 J4 F+ f2 Q run->prio=run->prio-3; /*每运行一次优先数降低3个单位*/
" e6 H" j* U c n, F, b if(run->needtime==0) /*如所需时间为0将其插入完成队列*/4 b4 p2 E9 s/ l! s" K! w$ O
{) l) R# H) ]- G/ a* E# R
run->next=finish;- ]+ k$ ^( n8 e# `
finish=run;
2 A2 V0 Z8 T( v# w3 N" K8 ] run->state='F'; /*置状态为完成态*/# E& g6 J# v6 P9 i: c
run=NULL; /*运行队列头指针为空*// _" w+ r: @0 v6 h. C+ j8 ]. X
if(ready!=NULL) /*如就绪队列不空*/
0 O- x0 J, p# p- Y8 o4 B5 s firstin(); /*将就绪对列的第一个进程投入运行*/
) s7 E" S$ ^* n0 ]1 c/ M* v }
9 h1 a9 w2 F: o% ? else /*没有运行完同时优先数不是最大,则将其变为就绪态插入到就绪队列*/3 A( y" ~7 s- B% m4 G
if((ready!=NULL)&&(run->prio<ready->prio))1 F; X$ A9 U8 S1 i6 o7 D' _
{- e3 C" ?! X- d6 i* N' V+ x7 J
run->state='W';3 J7 w! P, g# @3 |
insert1(run);
4 O# U A: m2 s" F$ u& w firstin(); /*将就绪队列的第一个进程投入运行*/
3 ^8 }5 [; Q( T! [- A }
5 L3 ~* k& V1 T: q5 s prt(alg); /*输出进程PCB信息*/
" N2 T( A2 M! P/ v! p }6 ^' u* t6 o; }1 ^
}
4 Y. t, \* `" D0 |/*时间片轮转法*/7 x# w9 t9 l. O$ _
roundrun(char alg)
. N7 M. ^. |# T9 U{
, a( f& o# }0 V P7 \& l5 ] while(run!=NULL)
/ `- m) t0 l- N1 S {8 b) w+ z" e/ K# f' d5 m7 t: a
run->cputime=run->cputime+1;
3 H, l9 s0 H4 ~3 x$ Q# d: r run->needtime=run->needtime-1;
" ]( n( Y- L5 l# k1 z( \) O run->count=run->count+1;
0 H& i5 z0 a, Q9 c& ?2 m if(run->needtime==0)/*运行完将其变为完成态,插入完成队列*/7 Y- k/ B! @+ I7 k- G
{
) G5 V7 |' W% H. F, v% Y run->next=finish;; Z1 k$ ~$ M0 W" b$ U- O' a0 P
finish=run;
5 `2 d- `8 u O0 w8 ^% O6 w run->state='F';
/ G) w3 J. h; ], [ run=NULL;& h1 a2 _- q8 f; y& w
if(ready!=NULL)5 N$ G# Q. K$ }1 }3 W
firstin(); /*就绪对列不空,将第一个进程投入运行*/6 {; O# X y1 V6 O$ S
}
) I1 s1 o+ X3 \% ^# l2 P ~ else% L3 J9 s& M& L, c% ^6 }
if(run->count==run->round) /*如果时间片到*/3 j* w. s; q7 I8 s
{$ Z: g' _% ?, j/ x8 n/ L; d, ^
run->count=0; /*计数器置0*/) w. O3 x0 R9 f! Q/ J" x
if(ready!=NULL) /*如就绪队列不空*/
9 s3 m, [/ r5 H$ V4 d1 P Y {
8 R- C- j6 I. i. P* Z run->state='W'; /*将进程插入到就绪队列中等待轮转*/4 m, y0 o* U0 ^* Y8 \$ M) s
insert2(run);2 i2 w& [, X9 B3 l
firstin(); /*将就绪对列的第一个进程投入运行*/
0 q' k: I; P- A/ v" E }
* g! O# V; b0 d! o/ V4 w2 L }7 s$ \* d f2 ^/ A4 }' K
prt(alg); /*输出进程信息*/$ }% s; b+ \* {6 e
}
. o( y0 \$ h( E7 R: S% B$ O, Q}
$ v6 `- [( r8 n" L0 b# g, I/*主函数*/
9 l5 S+ P' z) qmain()
; q% V3 b+ b2 K o# T{
, }) h/ ^1 S" Z. y# H: M- C char algo; /*算法标记*/; i4 r( k2 P1 m6 } ?3 {# k' K, J
clrscr();
& U5 @% w( x5 D, ` printf("type the algorithm /R(priority/roundrobin)\n");
7 j* K7 l; ^: s5 ]) Y; s# H( ` scanf("%c",&algo); /*输入字符确定算法*/
' P9 ]7 @7 J. v# w Q printf("Enter process number\n");5 I" F2 S/ o0 f9 p4 s! s& Q
scanf("%d",&N); /*输入进程数*/) C; E* J* ~2 f4 [9 w
if(algo=='P'||algo=='p')
* N2 x& Q" K) ? {5 {0 I& W( A6 P/ r, I
create1(algo); /*优先数法*/( U6 W6 j/ b4 D
priority(algo);( P$ j, L& B& x) J1 B- @* {
}/ p* ]. B9 { t# [
else
# h2 c8 @/ U; f6 P& r if(algo=='R'||algo=='r')' d, M7 \; J6 {8 r. L! }) A
{
( J" J. _: L& I1 v" I$ \& b% l0 ~ create2(algo); /*轮转法*/; ?2 a F g# a7 J& K; t" @9 k3 n
roundrun(algo);
- G" Z1 T( H: q8 i }
) B: p# e* C" N4 t* f}
& K7 t8 c+ t+ Z4 }</pre> |
|