|
EDA365欢迎您!
您需要 登录 才可以下载或查看,没有帐号?注册
x
<p><font face="Verdana">C语言源程序代码-进程调度</font><br/></p><pre>/*8.3.2 源程序*/( m! S$ W! Q8 [& j. s9 M Z: T
#include "stdio.h"
) {8 d" k6 Q M! t7 |" V#include "stdlib.h"3 s# Q) @3 o4 @2 J* ~
#include "string.h"1 L& }& X# b9 L6 t# e4 Q
typedef struct node
( s( T/ V. o8 s3 h4 F+ }% [6 z{8 G* C1 A. h( K' u" n
char name[10]; /*进程标识符*/
' `! e5 t2 V0 K7 K1 h) P( Z int prio; /*进程优先数*/# |$ b2 e+ `/ ?8 f. {
int round; /*进程时间轮转时间片*/ D) o6 q, l8 @( U
int cputime; /*进程占用CPU时间*/& |0 }$ ]! n& A _6 I' I. D/ A
int needtime; /*进程到完成还要的时间*/
/ y% a, Q5 g7 ~5 j- S int count; /*计数器*/
0 G# i" c* D; C8 \* y- s+ ] char state; /*进程的状态*/
, P6 F) Y0 s, o5 q& p. k& n8 c struct node *next; /*链指针*// o* L5 ]6 b* G9 W
}PCB;( V$ S% z7 n; O& B) B$ M1 I
PCB *finish,*ready,*tail,*run; /*队列指针*/, Q/ ~6 Z. ]2 r. \, V, ~
int N; /*进程数*/4 P5 H' a1 Z: k6 V+ i+ q8 H! x3 ?+ |0 Y
/*将就绪队列中的第一个进程投入运行*// V4 R( {( ?% e5 y& g
firstin()
! M1 p* h, p. b{. }+ u! S! w% ~6 r6 z+ v! l
run=ready; /*就绪队列头指针赋值给运行头指针*/2 i+ t( R5 D, w: T
run->state='R'; /*进程状态变为运行态*/
8 z* L7 i" e5 } ready=ready->next; /*就绪对列头指针后移到下一进程*/( V. R( D h$ d5 }$ j- J
}8 R1 _0 ]4 v6 Q d
/*标题输出函数*/
- Q" n, j, J+ e" W7 uvoid prt1(char a)# Z# |5 t+ G4 S2 H$ C7 x& f: P% ]/ t
{6 s) w" M. {6 v: c+ s: n. l
if(toupper(a)=='P') /*优先数法*/2 B' ^' \, D! O e/ ^+ T
printf(" name cputime needtime priority state\n");4 Z3 O/ x: \0 E7 E
else+ s! Q) y8 s# L, R; `# D
printf(" name cputime needtime count round state\n");- @+ z L, V5 t$ F
}* G5 B( g" s3 S& B) N* X8 {
/*进程PCB输出*/% Y- z, ~ u) M$ o
void prt2(char a,PCB *q)' A, ]' N* a' ]( F$ \4 l
{" |' D' \* Y" l' J* D
if(toupper(a)=='P') /*优先数法的输出*/$ Z2 M3 D! \2 I R8 ^2 A( j0 i
printf(" %-10s%-10d%-10d%-10d %c\n",q->name,, _# R2 U. f$ U" b0 m
q->cputime,q->needtime,q->prio,q->state);
9 ]. ^& c- K1 k# D2 Q4 U8 l3 C else/*轮转法的输出*/# _8 X! I& q$ S% b( p5 t; u; O2 U. b
printf(" %-10s%-10d%-10d%-10d%-10d %-c\n",q->name,
" Z* Q. \% k: m8 t6 G q->cputime,q->needtime,q->count,q->round,q->state);
0 X5 M/ \, x( [+ _! i8 b}
' Y, m. M2 A8 T3 z$ Y" l/*输出函数*/
" a0 |4 }, U6 T4 F) Kvoid prt(char algo)! k& [2 a- X5 o! A5 V' z% j
{
4 x/ h1 B4 ]% i0 g7 a. N PCB *p;
! }# O9 Z- d. p% f' u1 F1 F; ? prt1(algo); /*输出标题*/4 u( N. n2 O6 k$ V, @$ P
if(run!=NULL) /*如果运行指针不空*/
( J# ~/ k: T4 o- m* M prt2(algo,run); /*输出当前正在运行的PCB*/' k1 \6 ]1 o: ?3 Y
p=ready; /*输出就绪队列PCB*/. B) v* ^% q6 K6 x# }
while(p!=NULL)0 g) O# L* _ s8 X( |. s
{6 u- i6 D9 v; ^# }6 w% v
prt2(algo,p);
7 x+ x1 L2 x) W. U" ~) ? p=p->next;9 R$ t M* o' ?8 c% N' d
}8 p; Q4 G" I( V: p$ v* q$ H# Q+ P
p=finish; /*输出完成队列的PCB*/
' e$ k; m- V: I. N7 J9 U while(p!=NULL)- i- i* {, s( L
{
4 _. R( ~# {( i+ E7 T' P |# L prt2(algo,p);
; d2 u2 D% ?, O8 X7 C% r p=p->next;
" ~5 l! @5 m9 ^ }6 x/ z4 c& G. }5 n& t
getch(); /*压任意键继续*/! M$ j2 l5 Y: y' A* }
}
( ^- ~! C* v1 R1 o! x2 K/*优先数的插入算法*/6 E+ p n7 {2 S8 W( H6 X$ u2 j' b, Y
insert1(PCB *q)
' r8 O& U' I! r6 m- `{
8 h; X' s" T$ r( O& ~ PCB *p1,*s,*r;( E/ C9 G! v/ p: t' c( L! U
int b;* t2 J. D3 k% b% q3 J- K% j" T
s=q; /*待插入的PCB指针*/8 e+ |$ z- J8 u6 Y# b
p1=ready; /*就绪队列头指针*/6 q+ O! Q) n: ?5 L4 _, x8 I
r=p1; /*r做p1的前驱指针*/
: [. F* p$ t- G2 ? b=1;# [" B. ~# v) A$ V1 [
while((p1!=NULL)&&b) /*根据优先数确定插入位置*/
% ^1 J6 a6 F4 A7 p' D. f if(p1->prio>=s->prio)4 t! `: v+ j( b/ u8 M. V
{* K# u, w; o0 o" J
r=p1;
$ J$ E, l6 W. J: q% V" N) N6 [; h' Y p1=p1->next; R4 N2 f5 o) h9 O' i; T+ W
}: n! d& H# J( }8 E; t0 `
else
, w5 K, g0 |2 {0 Z$ M1 a b=0;
: w2 k E# S4 y6 T, b if(r!=p1) /*如果条件成立说明插入在r与p1之间*// z" o- t. l6 F, D
{2 {" g/ ]" w: }/ _% T6 W \
r->next=s;
6 f, M% [9 K3 o, ? s->next=p1;2 |) I5 E3 O/ _; b" l9 b7 q
}
6 {; i4 f& o) V else+ M# `3 E9 i1 [! F0 U0 c% N6 B
{6 Z) B' V1 [) J( R, m: O% c+ X
s->next=p1; /*否则插入在就绪队列的头*/ a6 c T3 e* }, a4 f2 p0 Y
ready=s;
; n5 {! x: _, _' A }
) Y4 A' J; B/ F1 b( I}
' {2 H6 m6 ^9 W! R9 q" w- q/*轮转法插入函数*/
5 {# B" O, @' G* t: U4 [+ Einsert2(PCB *p2), r; u* u( [6 G4 z0 h" N0 w; j! s
{5 t0 x; J, N; T9 }
tail->next=p2; /*将新的PCB插入在当前就绪队列的尾*/; p Z7 _6 ?* T& W. R, L( H, @
tail=p2;3 ?+ N% K7 j! r
p2->next=NULL;3 ~# H) v: I. O. q! W* v
}' z7 b+ ^; G/ ^* r2 j
/*优先数创建初始PCB信息*/( @# f3 ^7 W1 x8 w1 i, c! o4 D
void create1(char alg)/ P1 G. H" \) Z
{5 U% `" }$ b9 g7 v+ ~# r
PCB *p;
" z+ z( g# r" j4 | int i,time;
e4 R2 r8 @( `6 x$ ?2 I+ ]8 C8 { char na[10];
1 G$ e% Q4 @+ i9 ? ready=NULL; /*就绪队列头指针*/8 J' f! r: k% Y3 x3 {
finish=NULL; /*完成队列头指针*/
" j I& b( N, l+ B q run=NULL; /*运行队列指针*/
$ X. Z# z9 ~* k$ \5 X! w! o: G printf("Enter name and time of process\n"); /*输入进程标识和所需时间创建PCB*/
+ v- J% P$ Y* r/ X* Q6 | for(i=1;i<=N;i++)
; R- K4 G l+ n% S. S {
$ g3 G" L/ O8 x. g p=malloc(sizeof(PCB));
6 p% P$ v- f' Z& o1 j( K. ^ m scanf("%s",na);0 n, t. w) e o6 B) r9 S4 c/ k& x
scanf("%d",&time);" f% q8 v r3 F$ E( z' H; u/ F3 y+ Y
strcpy(p->name,na);, j" C& a2 m# M! t$ ]- K6 N. H# c
p->cputime=0;8 W, Y7 }. z6 ~' C9 j3 f
p->needtime=time; j. ^8 V" e9 T
p->state='w';
) ]) l% f) o& @- C& b p->prio=50-time;
0 n; O+ ^1 w6 f! [ if(ready!=NULL) /*就绪队列不空调用插入函数插入*/
$ Y- C1 U3 p- Y; \2 a insert1(p);
* E/ R1 Z; e0 N) K else6 n5 P# Q8 @' D; y4 e
{
; P9 a: h5 P3 h6 {. l8 @/ w% g Y p->next=ready; /*创建就绪队列的第一个PCB*/
; o3 B& X9 T( t: v! v4 W4 w ready=p;5 C/ l* H9 l. F% i* S4 c$ Y
}* C& X8 O$ F- H2 a1 I0 J0 O' d
}
1 o3 ]) t* O* }; w G9 H clrscr();1 q7 {* y) ^ H/ k
printf(" output of priority:\n");/ |; c+ o3 h$ f5 t! h
printf("************************************************\n");# d+ I2 Z& y' K. l* s
prt(alg); /*输出进程PCB信息*/4 U5 }% f, ^+ S0 p9 _* K
run=ready; /*将就绪队列的第一个进程投入运行*/& N! E% V7 `- Y( b$ R- K/ A+ g
ready=ready->next;# e, z, V5 y9 `% ~% f
run->state='R';6 s8 U+ g. a/ l$ g! g/ p
}
& a" Q. O& G/ x3 W( _6 ~/*轮转法创建进程PCB*/, h' m' Y/ `- q
void create2(char alg)
/ g3 B j0 p# a' g9 \{
# X; q) `, @( V! g& t G* S$ M PCB *p;
, G) q$ h6 l0 @! \# j% a int i,time;
1 n9 T4 f% y. ^5 |( M6 U char na[10];
" I+ M6 t5 c/ I0 G ready=NULL;
# L/ ], x: \/ a; B/ T% s finish=NULL;
$ J/ r4 o/ |' _ p! B' Y run=NULL;( q9 Z* @. p2 i0 g
printf("Enter name and time of round process\n");
# \* v; D3 w3 v5 ~4 U- a" j/ e4 P for(i=1;i<=N;i++)5 C7 e, T& ?: b3 n! G3 o' b2 d( P
{
& X6 k1 Y- X! l" G3 x- T \8 F p=malloc(sizeof(PCB));
; G" _) y8 D% `4 R scanf("%s",na);
% Z+ [6 _' j: F+ j9 X, }+ l scanf("%d",&time);
) }% p# ]; ^: y/ `! u5 e) [ strcpy(p->name,na);0 a- `+ E/ y0 m3 |! \2 c0 A
p->cputime=0;
4 E" Z% T7 J, K p->needtime=time;
8 Q& _' v8 ^4 `' g1 T p->count=0; /*计数器*/3 `6 I- @; }- o, @: {3 b
p->state='w';& y1 d6 k% U9 d8 H
p->round=2; /*时间片*/( c G' ^, R) z& O2 O, c' C
if(ready!=NULL)8 M- b; w% A3 F: I; ]- n8 B3 R
insert2(p);. c6 [, c4 c: i
else% e6 j9 Z2 {2 d: {
{
4 E0 }! W+ h0 @" R, R7 }$ j p->next=ready;" Y; w& \5 \# c; M3 b# \
ready=p;, R* T: m2 ^6 I
tail=p;5 e3 I# D* M+ h% I
}
; R+ ~- v4 r$ t& J6 ^" A' i! z }
, u- ^; f2 v" _. R, o" W clrscr();& Y6 K* s: Z' e# o! O
printf(" output of round\n");
, b2 Y, t5 C( s/ `* r printf("************************************************\n");8 w0 B' f+ X: R6 J }% o3 o* b
prt(alg); /*输出进程PCB信息*/
2 ]" s5 e$ Q0 m run=ready; /*将就绪队列的第一个进程投入运行*/1 e# ?% e' @2 J) r$ s+ y7 }' \) D" t
ready=ready->next;
# L' {2 f4 ]* n- C- v4 G run->state='R';
% q: C* _( y1 P: [8 J}% k# ? m/ H3 B" V r3 X9 K1 y
/*优先数调度算法*/; _5 G& g. ?! f( j; v& ?
priority(char alg)
m6 i& r1 a: Y! c{& ^4 I! C t! {5 q4 ?- O3 r
while(run!=NULL) /*当运行队列不空时,有进程正在运行*/
; P$ A! d" D" P) B+ s( Z/ H; m% e! m- m {: b: Y3 {0 l# u3 o
run->cputime=run->cputime+1;
$ i y( e0 u% S5 L; Y' a2 X9 F run->needtime=run->needtime-1;
" a+ [- F- P7 T9 r8 Q. o run->prio=run->prio-3; /*每运行一次优先数降低3个单位*/
& h2 \$ @* F3 r$ i0 {7 M if(run->needtime==0) /*如所需时间为0将其插入完成队列*/% I4 P6 R$ q* M0 E3 f+ h
{
" [, D2 k/ |# G, U run->next=finish;& ]6 R' T0 `- _" ~6 g' n
finish=run;, J: x/ C! x6 m- Z% S- p
run->state='F'; /*置状态为完成态*/1 x4 ~9 b$ [: S0 }7 Y' _$ `& M
run=NULL; /*运行队列头指针为空*/
) g; P. j9 ?6 X9 F' C if(ready!=NULL) /*如就绪队列不空*/. M8 d- M+ p$ g$ l
firstin(); /*将就绪对列的第一个进程投入运行*/
0 {" ^) l3 E6 y' l$ C }
, Y- @7 t! g6 t2 B; z# @ else /*没有运行完同时优先数不是最大,则将其变为就绪态插入到就绪队列*/
# ?1 @* y- \, y2 m) K, ^7 ^. k if((ready!=NULL)&&(run->prio<ready->prio))* ~, Z" p' w, `# I+ P# \, {
{! W) [' ]9 _+ b( ?
run->state='W';8 z6 G3 y3 X. v4 F7 j8 d5 N
insert1(run);
3 x& a7 r1 t5 C& m! ]4 T8 O firstin(); /*将就绪队列的第一个进程投入运行*/2 q" `( V( E2 L5 B; G$ ^
}
: j: Y# b* d" q x8 `+ L. g, g+ \) A6 j0 x prt(alg); /*输出进程PCB信息*// U9 b% |2 s! B& }3 N' w3 C
} R( z. I# y! L8 c0 a, b
}: u. c. C; ^' q0 J3 o* U
/*时间片轮转法*/
! H: \: I' l# A. l) O Broundrun(char alg)- g% C0 z/ ]' P9 k
{ A+ K* {0 a. V1 e$ c+ [7 M
while(run!=NULL)
; b4 y0 x; X& D) Q# S {
) l2 p$ c0 i4 @$ h' O$ w run->cputime=run->cputime+1;9 K- e4 |5 c+ N, V
run->needtime=run->needtime-1;
! |) D8 x W4 S run->count=run->count+1; j l& N; I" D% }
if(run->needtime==0)/*运行完将其变为完成态,插入完成队列*/- J: ^4 q7 }7 Z1 p n
{
" X' R+ P3 D" V9 y2 P' A/ D2 q run->next=finish;
5 N' g/ m6 f9 q% h$ h* U finish=run;
4 z: S' C2 t+ v% H0 V. x run->state='F';8 F$ m; ^$ g2 R/ Y8 V7 g ~
run=NULL;
7 o% u" Q" F' m' c# f$ C$ b if(ready!=NULL)
, @$ C+ y! s2 i- U; m }! { firstin(); /*就绪对列不空,将第一个进程投入运行*/, U5 {6 J& c% v
}5 E. x( |' E1 P. @# M. C1 d
else7 Z! D, s& ?6 ]) b- q' f3 w" \' K+ Q
if(run->count==run->round) /*如果时间片到*/
- }5 @. b$ _* ^! v4 s: x {
0 y1 v3 Q& x1 P l) P6 z run->count=0; /*计数器置0*/
8 v1 D% Q& A! C) Y* B if(ready!=NULL) /*如就绪队列不空*/
3 U5 [6 ~0 v9 ?' h8 b0 t$ e5 q {
+ u+ A/ ~5 w7 Y. x. H run->state='W'; /*将进程插入到就绪队列中等待轮转*/$ L( M9 K( U# s8 M5 @" l
insert2(run);
0 U& I( s W2 a9 Q8 I firstin(); /*将就绪对列的第一个进程投入运行*/
, }& g2 d* f/ P5 i6 z7 n! ^ }5 B E& u3 _4 [- Z x* S
}
$ H- ` |* }. {6 U3 H prt(alg); /*输出进程信息*/0 v& U- M' M8 u' O* \
}) k& B) g$ s2 e- P
}
! L- ^" v. T; `4 }) H/*主函数*/, G" C2 ?; j4 r; { E B, D5 C2 r
main()
* h" h8 _8 B( ?5 H3 W: K{
+ b+ i& l3 x6 w) s0 \ char algo; /*算法标记*/0 X6 A) n- q7 b2 K
clrscr();
) W v2 H. _8 y8 ]# J printf("type the algorithm/R(priority/roundrobin)\n");
; n% O$ M/ o( ~$ R; e! a9 k, u; E scanf("%c",&algo); /*输入字符确定算法*/5 q1 H" F7 R+ J6 X6 j
printf("Enter process number\n");
* O( a" s7 U/ t E scanf("%d",&N); /*输入进程数*/
. x% p2 O% h( U" L if(algo=='P'||algo=='p')
/ m9 h" z+ u) i2 Z4 e {: P) W; }: ^5 v6 n. H- p
create1(algo); /*优先数法*/$ |4 |6 [) B5 f% c' U- m0 o% F$ R
priority(algo);1 K: s; Z9 B! E! M0 B* ^- E" k
}" _( T/ S! |5 m, b, Y& @9 I
else
# t( }/ k. P. ]6 g& [% j. C, m$ T if(algo=='R'||algo=='r'), _3 R' I9 J0 I
{
# Q5 g2 \ h: {9 | create2(algo); /*轮转法*/
; r! v* [7 K9 F( s6 { I; Z roundrun(algo); V* z% R. E! ]7 K0 k( o& j/ _
}
% r# q8 W. [5 E. r" r/ o}$ g' C8 n8 f2 I% p) q% R5 X2 ^
</pre> |
|