|
|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
<p><font face="Verdana">C语言源程序代码-进程调度</font><br/></p><pre>/*8.3.2 源程序*/& \) n" e4 h* m" {
#include "stdio.h"
$ V1 E% O& E o7 K#include "stdlib.h"
3 k! i/ [! G/ {' M$ ]% u#include "string.h"
: ?" g% _; y& Ttypedef struct node {$ G& T5 S* m6 r; ?& q; A
{+ u" [" X4 Q; N: s4 E" E
char name[10]; /*进程标识符*/; X: n* m; s; H! P7 S
int prio; /*进程优先数*/
. f2 ~* C- G) \6 `$ L int round; /*进程时间轮转时间片*/' y1 I) h. C0 d( a$ P6 n" h& V
int cputime; /*进程占用CPU时间*/1 C9 D" {+ X8 `1 S; m- a( N& b5 |
int needtime; /*进程到完成还要的时间*/
7 C* x) G$ U S1 b( \ int count; /*计数器*/
/ {/ A# p( x; R8 C. a char state; /*进程的状态*/: g2 \, T# r! E* m1 C" R# a! E
struct node *next; /*链指针*/& [) J/ J$ X& B$ a! p* Y U! e
}PCB;
3 b' {4 i5 M) S& p8 K; gPCB *finish,*ready,*tail,*run; /*队列指针*/6 }* B% P/ F S3 V9 ?# V3 s& g
int N; /*进程数*/
: i, {4 t* Z# W9 ^2 ?/*将就绪队列中的第一个进程投入运行*/
, y' _! T: S# n# }! o, j$ vfirstin()
. F% i' o6 k+ B8 M{
7 J' m3 X6 {. z, [: [ run=ready; /*就绪队列头指针赋值给运行头指针*/3 y- Q& F, A2 I. j/ D2 r3 d
run->state='R'; /*进程状态变为运行态*/, v; Q @( n) h
ready=ready->next; /*就绪对列头指针后移到下一进程*/( P' p2 |, r6 w# z1 L- o
}
1 j3 C3 u2 A; u. Z" n' z7 |/*标题输出函数*/: ?' t/ ^1 o5 `* P, q) Q* v5 L1 E
void prt1(char a) _$ E' d& ]: Z" D: e
{
# F* }, k) Z' _/ I4 n. B' a if(toupper(a)=='P') /*优先数法*/
0 B; S) G/ [3 |+ ]9 z9 u printf(" name cputime needtime priority state\n");' `5 j4 U& I% {. V
else8 U8 z9 D5 _1 F9 u0 H- v/ @0 n
printf(" name cputime needtime count round state\n");; E0 j6 E) e! B- Q9 @' l
}3 |, w1 h5 P }1 I7 i- @; `8 ~
/*进程PCB输出*/
4 I* D0 Q1 u. P* c- P2 `void prt2(char a,PCB *q), u7 J: f6 a' s) l8 m
{
* G% s0 p$ w- Q* H9 |9 L, a# W if(toupper(a)=='P') /*优先数法的输出*/
! d$ D8 a6 |' g f3 t5 _- g- p6 _( w+ { printf(" %-10s%-10d%-10d%-10d %c\n",q->name,2 O" {8 ^ J" H/ Z% O
q->cputime,q->needtime,q->prio,q->state);/ ]/ }+ T a# s+ h7 A
else/*轮转法的输出*/
! c) g3 z4 Q# P S printf(" %-10s%-10d%-10d%-10d%-10d %-c\n",q->name,
* v0 H8 x: p; Q& |- a0 E3 j q->cputime,q->needtime,q->count,q->round,q->state);& Q* J0 l5 c' h5 N; y5 Z
}& o8 E. U/ ~: _8 {& Z
/*输出函数*/! _" C t' B! q8 z4 E
void prt(char algo)1 E8 i! S5 s9 V1 T
{2 D e9 @! ]5 N4 x
PCB *p;" d& ]: c9 I/ E- }5 g
prt1(algo); /*输出标题*/) h+ A3 e# g @1 y% n
if(run!=NULL) /*如果运行指针不空*/. l% h- m1 L: R. T" I7 z
prt2(algo,run); /*输出当前正在运行的PCB*/
) W: g3 a2 c. D+ [* C p=ready; /*输出就绪队列PCB*/, @$ N- _: c0 H9 E; B9 Y
while(p!=NULL)
8 [4 N7 f1 c' e# ] {; u9 M6 p5 |0 x. @4 \5 O
prt2(algo,p);
( @" Q7 l; ?; t- y% i* [" O% t4 P p=p->next;
! c6 N: N2 D) l' o3 G8 l% k" B }
2 X6 E: K& y) D7 p% Y" ^ p=finish; /*输出完成队列的PCB*/! G$ |3 X) [' J" ~
while(p!=NULL)) O1 F3 E# N/ G; S
{: d7 B' `. Z7 P* e! y: `8 |
prt2(algo,p);
6 x' s/ ?+ J `" Y* H- J5 f8 V7 i p=p->next;9 _0 ]$ S- n2 S$ ?
}
) e( V% g: N( F3 W+ C) E ? getch(); /*压任意键继续*/6 y K u( p% |* o. y3 o. t% F1 |9 |
}
- m8 `. D8 G. v* q" R/*优先数的插入算法*/, S# P$ g0 A/ s( Z# w( a2 x
insert1(PCB *q)
% b- E7 s1 k5 f6 h1 I* X{
1 Y _9 ~1 ^% a4 _6 s9 Y PCB *p1,*s,*r;
_6 W8 m# {* x9 x9 r int b;" Q* y* d1 H8 ]$ z* k" U1 y$ R
s=q; /*待插入的PCB指针*/
* A( }9 w$ V2 U& T, n p1=ready; /*就绪队列头指针*/; e9 b8 M/ g$ h( n
r=p1; /*r做p1的前驱指针*/: o) A" u3 {) i0 c" V% u& _
b=1;3 n2 n* |/ V$ R
while((p1!=NULL)&&b) /*根据优先数确定插入位置*/
/ w' O9 ? v7 ]. q4 \( a if(p1->prio>=s->prio)
& o7 k# r. Z0 k {
( `$ V7 E, A+ D. S/ ?. n r=p1;: N3 }% j7 i t/ {
p1=p1->next;
+ P4 V6 x9 T+ H1 W( u6 [3 ~! _ }
) Q/ j' G- N- D7 Y3 b7 @" M else
) ?; ?# P* B: u) ^ b=0;
0 z% v' t; o) l$ F+ M) f. Z if(r!=p1) /*如果条件成立说明插入在r与p1之间*/
. ?( }( U! v6 g/ x. P) o l {
( l; ]4 R9 r' V. ]6 R! _/ l! _ r->next=s;
( c) H6 A& j# r8 H I" H& z s->next=p1;8 _% k/ d9 J5 h# m
}! H' J, V6 G: W5 c
else- ]4 I) d1 j$ k& |& P
{
9 N/ }) u) `* W0 f% {# z4 E s->next=p1; /*否则插入在就绪队列的头*/" x$ |, ~$ w! O
ready=s;
+ B5 f& P2 y; P# D }
/ C( T2 w: y% l# p2 X}
/ r- L* z t5 C4 q3 p8 s/*轮转法插入函数*/5 e6 V8 _% G6 @, T
insert2(PCB *p2)
% r2 `. J2 S9 M a9 ^$ j4 b{: e1 E5 W8 C8 v: w: z2 n8 I
tail->next=p2; /*将新的PCB插入在当前就绪队列的尾*/
9 C9 y! D' r, D w tail=p2;
3 W" u7 T/ Z/ p% v6 V3 d p2->next=NULL;* K4 ~) \7 J' C- `+ p9 _5 N
}/ N: Q; H7 s! r7 o& e" s m+ O' e
/*优先数创建初始PCB信息*/ z: v5 |& ^, _7 P; l4 j- K
void create1(char alg)
0 t, a' k, g' Z. p' d' |$ _{
, O% }# I" D2 w2 w! m2 B- }' S PCB *p;
; v. g) F5 S3 s+ B) m- B" j8 G int i,time; [8 c- l7 [$ U6 o2 {+ b' V& V7 j3 G
char na[10];
1 L6 [' t D8 p/ N ready=NULL; /*就绪队列头指针*/4 F; |/ r+ I7 H! F' n. \
finish=NULL; /*完成队列头指针*/1 c) a7 }2 c. _! }4 t
run=NULL; /*运行队列指针*/
" s0 t1 F3 C1 \! k printf("Enter name and time of process\n"); /*输入进程标识和所需时间创建PCB*/
# t6 B. Q, n! Z9 z+ X) j# f for(i=1;i<=N;i++)4 ], H' P3 P+ Y
{
2 V. ~# S) W4 Y I; H t p=malloc(sizeof(PCB));
: Y! D3 _1 {! L" W1 W' R3 R scanf("%s",na);* A h) A7 w5 p' `" [
scanf("%d",&time);
$ t+ W' i" N. @0 y strcpy(p->name,na);
0 B( ?0 l' ]4 F- U. t. s6 ] p->cputime=0;' g- D2 ]6 k2 L- m F# ]
p->needtime=time;$ l4 F) P. b4 E4 W$ t8 {2 ]
p->state='w';8 i- z$ W4 Q# C! V* C
p->prio=50-time;7 J) @! N# O+ i3 r- u
if(ready!=NULL) /*就绪队列不空调用插入函数插入*/
! T0 |4 d6 ]! k7 w insert1(p);
6 _, H) i: c5 Z! M( m else2 `7 C$ t6 m0 A6 z
{
+ l6 A/ m. _+ j, o p->next=ready; /*创建就绪队列的第一个PCB*/% K, s( q: K5 V
ready=p;
1 f$ x& |) X* C3 V5 j F& \/ Y% k. m }
+ z4 y3 e6 ?3 N* k7 a }3 Q) W+ e, e6 R2 r& y& o
clrscr();
3 d: Y; g: w* M( t W6 p8 S printf(" output of priority:\n");( F9 q' L; l' X
printf("************************************************\n");% M$ ?$ M- Q9 N1 g7 R4 }
prt(alg); /*输出进程PCB信息*/
. W- `/ c% I. Z) D9 ^ run=ready; /*将就绪队列的第一个进程投入运行*/
) B# L( D" J" z, d- b ready=ready->next;
1 w, T& ~& l9 m8 b3 T- Y run->state='R';
& V4 ^4 Y9 c, Q}6 \5 I" s2 f8 y# W7 y6 h
/*轮转法创建进程PCB*/
* ] \0 V# U( y- j6 avoid create2(char alg)4 Z2 B7 f \4 C% Z; Y8 t' _
{3 J+ `6 C1 p2 S+ h8 n2 X
PCB *p;
- ]+ M7 i n# L int i,time;
! F2 a0 p% ?# a# \ char na[10];
' X1 u+ u; N3 m% U; K ready=NULL;
7 R: X) ?9 H3 B' L* p) L" e finish=NULL;
# f0 N+ g( ^0 x4 E( t6 I run=NULL;
& ~1 C' p3 H$ ?5 e3 ] printf("Enter name and time of round process\n");2 i6 ?$ y8 U/ b0 o! @9 n
for(i=1;i<=N;i++)( Z& g2 E3 ?- [# R+ w; H6 W0 X
{9 [/ m) t) U. q# h0 _4 I6 N0 s
p=malloc(sizeof(PCB));
8 d8 r P) ?8 r* E6 A' w- A scanf("%s",na);5 `5 \4 x% P4 b/ M3 O( J0 H9 i' |
scanf("%d",&time);2 u6 J" K4 ~, t$ V- D2 `0 v
strcpy(p->name,na);3 A1 T* g6 x' p2 \; n
p->cputime=0;
, U7 n& b- Z. G0 A( @% Z p->needtime=time;
; e, e7 j2 w* W% f) X& n0 B9 U p->count=0; /*计数器*/% C2 ~+ w [& L. F- F
p->state='w';
4 q3 F' Z9 N1 w1 f8 v- Z3 z p->round=2; /*时间片*/
9 g$ U6 I; ~1 S; m! x: `8 X; z6 G if(ready!=NULL)5 |+ }9 m# |; P: j) i
insert2(p);
+ ~! w- N) C6 c% g else& X8 e) t. ~9 ~7 S! A% ]5 o6 A
{
$ ~+ u* I( v- P+ I p->next=ready;( N% @+ A$ z/ `% v
ready=p; X7 Z6 U5 v- p" a# g" o2 d0 E
tail=p;9 t \6 r# o( X$ ~
}
4 x8 t w& ^* e% h5 M }, d3 ]; c. d& H3 r$ q! a/ C
clrscr();
" {# ^! F. o$ M printf(" output of round\n");
# r$ ]3 n" s0 c3 L* e printf("************************************************\n");# c$ G# T) y9 p& r
prt(alg); /*输出进程PCB信息*/6 G- F$ P& u% B; J* j3 w* F+ D
run=ready; /*将就绪队列的第一个进程投入运行*/% s' L! t M: s& |" I! B
ready=ready->next;
& K4 B) r1 e b. B7 N8 t7 U run->state='R';$ p* d; V. M- E$ l6 H6 d+ x
}
7 ]1 S' b# a) i' y0 a. ~& c3 M6 l/*优先数调度算法*/
' c6 R' s2 z4 G7 d2 T. b+ J0 Rpriority(char alg)# N# O/ H9 ]1 R/ p
{8 q$ w/ @6 R6 {( o- T
while(run!=NULL) /*当运行队列不空时,有进程正在运行*/
( s3 p- E) _( f {
5 L# d# Q, ]- j4 J' K5 o run->cputime=run->cputime+1;8 p; y/ ~% y- N' l; W* J
run->needtime=run->needtime-1;' X4 Z' ^! @' F% W
run->prio=run->prio-3; /*每运行一次优先数降低3个单位*/
: C! |- i6 V1 o" ~# ]0 v$ U3 Q6 g if(run->needtime==0) /*如所需时间为0将其插入完成队列*/1 y7 |- \' s" {( c! Y
{; [' ?0 v' z6 W
run->next=finish;- M4 @9 J; H2 D3 K. n
finish=run;/ ~( x& I/ N! z7 _
run->state='F'; /*置状态为完成态*/
: s1 n; D" \% q, w run=NULL; /*运行队列头指针为空*/4 u5 u0 Q5 J' S% e5 |/ X
if(ready!=NULL) /*如就绪队列不空*/' q3 B0 W3 M" l
firstin(); /*将就绪对列的第一个进程投入运行*/
( }/ N. a. D, W: q0 p \, _ }$ R- n+ }1 X+ @% W$ X
else /*没有运行完同时优先数不是最大,则将其变为就绪态插入到就绪队列*/8 }. ] g# L6 Y( i
if((ready!=NULL)&&(run->prio<ready->prio))
& l$ \5 F" b1 `' a3 g5 q; n {4 ~3 [& l! `0 V! b" p7 ]3 C7 \
run->state='W';& F" f& C+ B* ^* J4 s# u6 a- @" N
insert1(run);' P7 S& d3 \: A. N8 e% e
firstin(); /*将就绪队列的第一个进程投入运行*/2 T3 @9 o$ m) c \; b3 C3 g
}( f' O3 o8 ?+ @& m( p! z! M" c
prt(alg); /*输出进程PCB信息*/9 y0 Q2 ~0 p0 ?$ f) b- U& t
}
3 y* U# k, H; e' ?% a}- S# X) \2 _8 \
/*时间片轮转法*/3 a6 n0 g4 C/ F/ H) A: U9 s
roundrun(char alg)& o/ r: f0 n5 k2 q1 O$ u! i. ?
{2 b# x+ j! w: l
while(run!=NULL)
2 Y5 d7 c- R3 W6 Z {3 t# h- Q5 }5 \' o0 e0 Z" Z
run->cputime=run->cputime+1;8 q- t) x* m+ {
run->needtime=run->needtime-1;
" V) X# o4 R0 n. n3 t run->count=run->count+1;
# w0 g% |; I' ?' C if(run->needtime==0)/*运行完将其变为完成态,插入完成队列*/
7 \, G" I& c6 w! \ {
% [: k) e( ?; C% C6 t3 o run->next=finish;! g3 O: m" \( k& c1 J, d
finish=run;
) z! T' ^/ y3 z$ B) |9 \; b run->state='F';8 e+ u1 f' j0 b/ A% E* h& q
run=NULL;1 C) `/ h1 H. G, \& J0 d
if(ready!=NULL)3 P& G' a, m) F7 c! e
firstin(); /*就绪对列不空,将第一个进程投入运行*/
1 f( e/ J9 A+ y3 A, b( l% ~+ a4 q }
0 H3 i1 ~: B/ E& ]) u, k6 Z else& m7 s# z4 @! u) E
if(run->count==run->round) /*如果时间片到*/
6 q+ C* |6 z; _+ t% X' X1 e- _ {
) U6 l" I1 @! r* i) Q run->count=0; /*计数器置0*/; R$ x3 g4 e6 D( p1 @
if(ready!=NULL) /*如就绪队列不空*/
" W- z5 O& w- R7 b! k {
2 y E+ o# B, ], H9 V2 h' i run->state='W'; /*将进程插入到就绪队列中等待轮转*/7 G+ O4 L) D2 @' L" ~
insert2(run);
. b) {, B* f) t0 @. B firstin(); /*将就绪对列的第一个进程投入运行*// V) X/ M, f1 K) D( D; R
}: f' Q4 o0 [5 i t
}* v# f x O2 x+ X) C% d- R- O
prt(alg); /*输出进程信息*/
$ m6 b+ R, z' Z, H: V, P8 W }4 q' S5 T+ x7 c% w8 b
}
6 U, p2 V8 _, W" ]0 G/*主函数*/6 }8 H9 ]; R& e( [+ P
main() U0 i- \1 F4 b: O4 X
{) q' ^) _( R% D
char algo; /*算法标记*/% w# K4 z2 l! J
clrscr();
# g0 D9 y0 v1 A) I printf("type the algorithm /R(priority/roundrobin)\n");% f0 F8 i, l: G( |3 o' K" H" |# P
scanf("%c",&algo); /*输入字符确定算法*/# j1 k! {+ T c8 a" u' E% v. v
printf("Enter process number\n");
$ A+ | g" C! P0 S" K0 S( [ scanf("%d",&N); /*输入进程数*/( L6 V$ h8 ~' C: B& d7 u( T
if(algo=='P'||algo=='p')+ s2 Z" @6 X( J: q' B# c
{3 b7 O- ^* S& @9 x6 i; v( x# s
create1(algo); /*优先数法*/) e5 a& o5 j6 A* T- U- w+ @
priority(algo);5 ^. A! {# f# j' V8 O- M9 _1 x
}
: ~- w, H5 B- y# Q7 B else
! q6 K- U5 A0 f) F0 o$ w if(algo=='R'||algo=='r') }. P) _+ W/ r. I, s1 s8 g* W
{
2 H3 p' ?9 T. h2 w0 R" Q6 Q7 _; G create2(algo); /*轮转法*/
8 G8 \( I$ J0 Q; i3 [/ q+ S: I roundrun(algo);
' U1 y+ c2 r) u( i }2 r8 W1 X! B! {( \2 E; i& O
}3 U. i% E: f2 a3 Y; C( v* \3 E. C
</pre> |
|