|
EDA365欢迎您!
您需要 登录 才可以下载或查看,没有帐号?注册
x
<p><font face="Verdana">C语言源程序代码-进程调度</font><br/></p><pre>/*8.3.2 源程序*/
/ r% P! N' g5 q& Z1 R0 {; e#include "stdio.h"+ A/ L) c+ b3 T. k6 }( Q
#include "stdlib.h"
& h* d4 x x r#include "string.h"$ H( I, `. s9 _2 Z+ @
typedef struct node+ k: i2 w4 M* Q5 R3 n9 z, {
{
/ ~8 P! i" [. J# O6 d! a9 n char name[10]; /*进程标识符*/
, o; z2 y6 J/ M5 o int prio; /*进程优先数*/
3 X4 Q: o- n- S5 V& @; p int round; /*进程时间轮转时间片*/# R$ S. P0 f! g1 z
int cputime; /*进程占用CPU时间*/- U/ @2 W7 P4 i% j; M! n9 K
int needtime; /*进程到完成还要的时间*/
! G4 N: N( J6 @# m, b int count; /*计数器*/
- m$ ?8 N. V: f* y0 O0 b: p u# | char state; /*进程的状态*/' W, \! @7 t% j* N+ |1 T
struct node *next; /*链指针*/" ~, A2 ~, D4 G+ Y
}PCB;
z$ ^9 ^. f1 t# [- d# QPCB *finish,*ready,*tail,*run; /*队列指针*/
& Q, ^$ Q$ M, Lint N; /*进程数*/
1 H4 R6 D% I3 T7 c* ^! W2 Z' @/*将就绪队列中的第一个进程投入运行*/6 E2 r% s/ Z! t- @: o* A8 e
firstin(), t$ W6 s" B+ [4 ]2 P4 K* v
{
! o! [0 o3 n0 x/ d( ~2 M& Z run=ready; /*就绪队列头指针赋值给运行头指针*/. \5 m5 T* b# k4 j6 ^- E( y
run->state='R'; /*进程状态变为运行态*/
+ s. A5 ~% y2 K4 C1 ]. I; q% X ready=ready->next; /*就绪对列头指针后移到下一进程*/
" e8 W9 p% ~8 w! h" l}% g8 _, Y0 _/ R: F
/*标题输出函数*/
6 m5 A4 E5 k( Hvoid prt1(char a)0 ]! s, o7 M$ _( l' e5 Q) C
{. S: `: u6 d l$ {
if(toupper(a)=='P') /*优先数法*/ X6 m0 U" U/ b* A g
printf(" name cputime needtime priority state\n");: o) k0 |, z- W/ L2 F4 ]
else
5 U+ U! c2 | y8 F! H! U printf(" name cputime needtime count round state\n");
" t; G# U- {! {}
, [: D% y5 i1 Y1 h6 I' N/*进程PCB输出*/
) ?0 j6 K3 H$ g: |% c2 r4 y5 [5 ]# svoid prt2(char a,PCB *q)
* c6 ~, ^' `3 D) `/ @. Q# d" ~. O{
3 _+ T* q/ r9 h# P$ u" b3 K. ^5 P9 w if(toupper(a)=='P') /*优先数法的输出*/
+ U. s7 b' J2 n2 `( C: p printf(" %-10s%-10d%-10d%-10d %c\n",q->name,: D6 X$ y, p( z; O8 A3 C
q->cputime,q->needtime,q->prio,q->state);7 p3 G& [8 q, [0 \( M
else/*轮转法的输出*/: G) q5 n) r5 N0 I0 j* z/ R. {7 w
printf(" %-10s%-10d%-10d%-10d%-10d %-c\n",q->name,
0 v; D" x2 f) ?! H* n; D q->cputime,q->needtime,q->count,q->round,q->state);
0 [6 J# `3 i& _$ R3 r}0 j" x" ~( a7 [- X7 A$ B; [( g8 y
/*输出函数*/
. [+ k0 B, D9 D o* Z* e: vvoid prt(char algo)3 `3 h/ P3 N4 Z. ~
{" L+ c, @# ^. R/ K( p4 j
PCB *p;
- {% ?( K: p$ I+ K' |0 p1 b prt1(algo); /*输出标题*/" e+ u" T$ I* l8 U$ r
if(run!=NULL) /*如果运行指针不空*/! n; F C& k! b4 \' f' i
prt2(algo,run); /*输出当前正在运行的PCB*/+ D- o1 P! t1 a4 M, v
p=ready; /*输出就绪队列PCB*/. z& U6 C7 [: h$ k a% ^2 x
while(p!=NULL)2 i; N& e, L$ ^' @! J
{; x" L4 N/ G" D; V/ s; |" c0 ^" i
prt2(algo,p);
7 D @& G, w7 c/ v p=p->next;
: r4 w( I8 h Q/ i$ C% \' M }6 K! O8 `5 E* s! a7 A) ~ S3 [, l/ {
p=finish; /*输出完成队列的PCB*/& w D- o+ \$ A8 V8 r
while(p!=NULL)
* C+ x# n* k# \) ~3 Y; k! z; P7 Y {
+ m; U* M0 _ b: z prt2(algo,p);- _' a3 Y# M$ Y7 j! [
p=p->next;
0 V5 {0 Z$ T" r+ V }% Z: ]& l6 X" h: T
getch(); /*压任意键继续*/2 r! d# E7 y' T+ t! O
}
7 A- |0 C9 Z! M- F, G/*优先数的插入算法*/
" t# Z; }6 W& G( o9 ^1 Cinsert1(PCB *q)
, }' \9 S. _: ?, ]{% x6 d& S1 L# x! ^. j; h1 O
PCB *p1,*s,*r;
' r; C8 a8 [9 B5 f* R3 F int b;
4 E' ^' j( _ U+ N% l+ x3 V s=q; /*待插入的PCB指针*/3 u$ }/ |0 _- K* l* o
p1=ready; /*就绪队列头指针*/3 x. ^' t4 f8 y) u1 S* e1 I7 X5 C
r=p1; /*r做p1的前驱指针*/
* k; a4 N& k. U3 s b=1;
6 T1 A0 S: q# S! \ I while((p1!=NULL)&&b) /*根据优先数确定插入位置*/6 h$ t0 J# O* M: q$ Y0 ]0 y
if(p1->prio>=s->prio)
8 U- U% f, |8 S8 U+ b9 |2 J { g* X9 q) k5 ?) W
r=p1;
. S1 k& `/ c7 x! G4 c3 d7 O/ n3 C- Q p1=p1->next;
* f1 v7 V6 B- \; @ }
1 b! D. |$ g; g, R/ ~ else2 F5 N9 s, G+ p+ e! C$ m& R
b=0;
& M9 u6 m v& h if(r!=p1) /*如果条件成立说明插入在r与p1之间*/
0 [3 G1 `- z. g6 t( I {
; P) Z+ ^( c% @( x r->next=s;
4 u5 i" m2 k4 ]5 X8 ~2 I s->next=p1;* k) T6 {( I- a7 {
}
& E6 ^6 [# {6 O( P' |+ J else
' W2 H9 k4 _4 l9 t {
9 _3 k4 c/ `4 |# N j& Q" O s->next=p1; /*否则插入在就绪队列的头*/. M, M6 t+ i2 B8 B- L a
ready=s;
8 `$ ~" B4 e5 w# |6 ~+ r }
! R+ z& A6 Q1 i8 T8 k}$ |8 u# v9 n) K
/*轮转法插入函数*/7 X( Y3 W3 X- t- u K( b1 H9 F- K
insert2(PCB *p2)8 f2 S+ ?* J5 D
{
& x( Z' s0 p; [* L6 x1 @ tail->next=p2; /*将新的PCB插入在当前就绪队列的尾*// p* f, ]% [ X9 U6 H
tail=p2;! p3 H: e2 Z4 I$ s$ j$ Y9 _# r
p2->next=NULL;
( a: k3 d! F' j% P}3 n' O2 A q, Y
/*优先数创建初始PCB信息*/
, N2 s: l4 _4 S+ p7 B& {! d rvoid create1(char alg)+ v7 f' _% Y6 ?0 \( `4 p% D r
{
$ q% B9 t7 J: A h PCB *p;7 z- g2 v1 k& w7 H
int i,time;& J2 f! @2 Q. D- |6 F M4 {
char na[10];
6 c2 F- Z% a1 T1 o2 d ready=NULL; /*就绪队列头指针*/
- V0 L/ K$ A- B3 N8 e% J finish=NULL; /*完成队列头指针*/6 H6 Q3 J6 o* v( ]: _" G
run=NULL; /*运行队列指针*/4 I7 M$ [6 e9 m8 _
printf("Enter name and time of process\n"); /*输入进程标识和所需时间创建PCB*/
* P3 q4 a5 \. E) E: ` for(i=1;i<=N;i++)
: m- X1 T* b$ c* A5 G {" g" ^0 d, B5 j3 e
p=malloc(sizeof(PCB));& x# v, M; M: I& {4 a1 O' F
scanf("%s",na);+ o& E1 }6 C1 o5 m
scanf("%d",&time); K/ b5 R! N" Q! N
strcpy(p->name,na);% g: I# d9 h& f- N
p->cputime=0;
6 o+ R+ g8 D9 j8 I- b' h p->needtime=time;
1 `" J7 J; ~% e$ o: O* m$ `3 A1 G p->state='w';" `8 y- b5 q3 D; t2 I0 P. N
p->prio=50-time;
+ I0 e! j8 U( E# w" G" z if(ready!=NULL) /*就绪队列不空调用插入函数插入*/
0 W$ S, z/ s! F insert1(p);
" g* F7 n% T; @, Y2 V else
* F6 V% u% I% m' W, J {: _% P0 V6 v3 H* K% R, n+ _
p->next=ready; /*创建就绪队列的第一个PCB*/: O$ {# [! j3 M- i% U' t' i/ g
ready=p;
' j' n& i/ t0 j9 u; p }
; ^# a' E. y; n }
, v5 l y4 ~9 V* K! P clrscr();
) e7 E; l& D1 O& j4 ]% Q' x printf(" output of priority:\n");
' L# |" G( |+ V% l printf("************************************************\n");
5 x2 W) b8 U4 y5 N prt(alg); /*输出进程PCB信息*/
0 Q- H ]4 l7 ~( g, y4 d2 b1 d6 u run=ready; /*将就绪队列的第一个进程投入运行*/; X7 P c! B. o& V: o A( s
ready=ready->next;
' p3 ~5 o+ R) n run->state='R';
, d0 Z* ~* g- h B8 t}$ M- ?+ b8 a6 c* J* }% k+ `
/*轮转法创建进程PCB*/" v: N1 q, ?$ q9 R' c! E
void create2(char alg)
h8 b% e2 r- g2 T$ e: Z& h{4 U8 G- I0 |1 F. b7 y- M
PCB *p;
$ H7 [) p6 e3 n( P. O4 x9 ` E6 P int i,time;5 G8 E. i, M1 ?! S& c3 Z* r q! D# `
char na[10];! E x! v# c7 u2 s6 _
ready=NULL;
, n4 V3 |& v1 g2 r* w& h finish=NULL;: ]9 N: j, B# I# x, n1 H+ y9 X
run=NULL;
5 Z0 L, s9 X7 ` printf("Enter name and time of round process\n");/ e+ \* X- l" U5 O: r: B
for(i=1;i<=N;i++)2 M5 d8 w( t) v; {+ F
{
; E% }6 L8 K( ]/ \+ ~2 M p=malloc(sizeof(PCB));: z6 ~/ n; J# j4 }
scanf("%s",na);! |$ E7 D l) E8 {, ]+ {5 S
scanf("%d",&time);- }$ l5 e7 s: S U5 E# e, F/ \, q
strcpy(p->name,na);4 l% T7 d2 H$ ^. d9 e4 y5 ~
p->cputime=0;! i# q8 c: o# K+ E+ y/ e
p->needtime=time;
9 H% n% ~) {! X- Q p->count=0; /*计数器*/1 O" o: d- @6 x& o
p->state='w';
5 F4 u9 S, i5 Z& O p->round=2; /*时间片*/5 L2 _/ x1 Z7 t; H6 p, I
if(ready!=NULL)3 d$ b5 q0 g" C* V/ E# l3 S# [5 s
insert2(p);! n9 n2 [ l& h0 D8 `) v
else
1 [1 F I( L' j9 q! M6 `- T- |, m! | {3 ^: A7 E' \. d; f: c3 D$ D8 @
p->next=ready;( v" X9 ]* A5 c- s0 `
ready=p;1 q; I/ y: t, P8 Y. G
tail=p;9 l3 W% `4 b" P% o" m
}. u0 }8 [8 c6 T" E5 d
}
! k, f4 ~# B6 @2 W clrscr();. Q/ e; v, M% K1 O; g8 ?0 X8 ~
printf(" output of round\n");5 t% L" O# ^* c2 y* P7 t S0 M
printf("************************************************\n");! t' @" T) l! {4 T
prt(alg); /*输出进程PCB信息*/
6 m8 X) v6 Y6 E7 B5 w- ?( O4 r8 ` run=ready; /*将就绪队列的第一个进程投入运行*/
; D0 [2 C# \1 t% _. A$ z ready=ready->next;
3 N4 ?# Q5 t1 b$ W9 h# X run->state='R';% _: ~3 c8 o* m2 ?/ `* I
}
7 W/ u. _* A& U' a/*优先数调度算法*/6 M: U" ]0 |8 \$ T
priority(char alg)
u) B# e+ r+ \{9 z$ C) @/ y" {0 w! ^
while(run!=NULL) /*当运行队列不空时,有进程正在运行*/
* p. }4 [' e5 L1 X3 Z$ x. n {; {7 S' o2 Q0 I8 _; {8 y4 A
run->cputime=run->cputime+1;0 a4 R/ w/ n1 Q# p# q5 T
run->needtime=run->needtime-1;
( s0 M, q3 D4 n4 G# X6 M+ B M% Q2 M run->prio=run->prio-3; /*每运行一次优先数降低3个单位*/, S1 b1 T) y. |5 y! w
if(run->needtime==0) /*如所需时间为0将其插入完成队列*/
6 q5 O) n! V& V8 _0 @ {+ f/ [3 h% r# ] A k, ?
run->next=finish;; L B/ M" a, ^6 a
finish=run;" ^1 W: g( `0 Y- V7 T! p
run->state='F'; /*置状态为完成态*/
( `: z) f! N2 z' k run=NULL; /*运行队列头指针为空*/0 K* K( T- n7 D3 s6 ?, G- [
if(ready!=NULL) /*如就绪队列不空*/1 q+ z- _( y2 U$ ]
firstin(); /*将就绪对列的第一个进程投入运行*/
5 c$ m5 \. ^" k, C8 s }
3 E3 H1 t- @% F$ x* U" U else /*没有运行完同时优先数不是最大,则将其变为就绪态插入到就绪队列*/4 _: T8 a6 c' _3 z0 {: ?
if((ready!=NULL)&&(run->prio<ready->prio))
# ]9 l$ F6 T3 Y- {" }1 N N {/ b( Y$ v3 r; i! ~* U9 S
run->state='W';
, p# I9 p1 `6 f* \& k5 l insert1(run);
9 s& R1 W, }5 L firstin(); /*将就绪队列的第一个进程投入运行*/
- N* h% m$ C* T/ t' e }7 h3 V8 U9 Y& c; |, W# t
prt(alg); /*输出进程PCB信息*/
/ X; T5 E( c* f }$ C5 G9 t0 V, `. F6 D# |
}
6 L/ C, B1 {( a' s+ [+ w/*时间片轮转法*/
9 J) I4 L. l! G9 B% s* rroundrun(char alg)
5 }* ?1 e& D8 P; ^" r{' H: q5 \/ k4 C+ t
while(run!=NULL)9 v& Y# n: n2 X" R. C/ Z7 e$ q/ W
{4 m# j7 H/ |# X6 p
run->cputime=run->cputime+1;9 X c% g6 V5 A5 E
run->needtime=run->needtime-1;1 P: d$ [/ l& r& A0 H
run->count=run->count+1;* `* J8 W' g0 | J0 l) s, W
if(run->needtime==0)/*运行完将其变为完成态,插入完成队列*/( u6 [7 q, e' k4 s, w
{
, G. W7 B3 Q- |6 `6 y, M( E run->next=finish;
+ l3 ], U8 j3 V ]2 H( t0 K finish=run;5 ]$ v- m# z! V$ Z
run->state='F';# U! V( r% E' z% E8 A
run=NULL;
! C" o) h- {5 `3 `# i2 t if(ready!=NULL)
- R) H+ Q: l$ k7 m5 B: _ firstin(); /*就绪对列不空,将第一个进程投入运行*/
. k5 B) K. {/ i7 z }( |) b, D$ N S, I
else
8 w. f9 Z, |# k. d if(run->count==run->round) /*如果时间片到*/ A9 v1 T9 i+ R5 k# j
{
& w* b7 N( X( ] run->count=0; /*计数器置0*/7 e1 \" _; w" O- g) A2 n
if(ready!=NULL) /*如就绪队列不空*/ B7 Z, L6 r4 @/ t2 `1 H, Y
{* H, T( ?* A3 ?) n$ j
run->state='W'; /*将进程插入到就绪队列中等待轮转*/
/ j# \/ U3 W6 t5 K4 o& j insert2(run);8 W+ N7 |3 O3 P& U# h( R
firstin(); /*将就绪对列的第一个进程投入运行*/* k% `' E6 P* T& a
}, y1 [1 F ~& n1 |! d; [
}
' m" B3 `" t+ v k, L9 | prt(alg); /*输出进程信息*/
+ C% R5 E) L( Q% l1 V# K) w }
* t. W5 I8 V- f, h. c}
9 |2 ^' k2 x& w6 ~+ p6 Y& x/*主函数*/2 h0 e3 t* N5 Z& Y( S% R2 \( w
main()
- N, t! ?9 o5 X" H$ o/ y{
0 O2 m2 S6 |7 b% t a) A7 l& R char algo; /*算法标记*/
) f6 f& h& i: G clrscr();: O! D E$ u( y# _
printf("type the algorithm /R(priority/roundrobin)\n");! R) U8 n" |" l5 }# Y# B/ k
scanf("%c",&algo); /*输入字符确定算法*/ F' g% U) _4 @( n5 H
printf("Enter process number\n");
4 x2 p" x. Z: d5 E! E, |4 J. k) B scanf("%d",&N); /*输入进程数*/
' Y0 R9 N# t7 K* v if(algo=='P'||algo=='p'). w0 @% u% j. f1 i* q: g
{6 s1 z3 e1 Q& ?. f3 p( ~5 ^+ U9 T
create1(algo); /*优先数法*/
( ]. t. F, [$ ^9 U! ~. I. ?# j$ M priority(algo);
, O- R4 }7 p7 h }* X( A( W; L1 q5 V0 g1 Z6 q5 t
else
; `8 \+ A) D6 e" i& d4 H if(algo=='R'||algo=='r')& Q1 D( n' n+ t, g
{
' A! ?0 V, V" E! j8 R& Z7 O create2(algo); /*轮转法*/: F( E7 s2 S. @, \: y( v
roundrun(algo);
: R7 P2 M- w5 d4 ]% a5 ]8 p }
. {8 D; v, }, z! A}# B! A) ~/ }, z& C+ I
</pre> |
|