完成了你们看吧!
- d3 f E3 Z: V7 K/ V" z( D5 O+ G; p7 K, V/ z, z
#include
5 q% d9 Y/ M6 G& f! E1 m3 C. \#include
8 m @7 a; D$ ~+ D6 S% C#include 4 W: R- A! ~8 d/ A
#define GET_INDOS 0x34
6 `9 s; k" R1 ] A0 f9 V6 h' y- O; {#define GET_CRIT_ERR 0x5d06
# ?3 |: F, d+ U$ K3 x#define FINISHED 0
/ B" F0 a8 @9 F$ n9 W. f#define RUNNING 1' ]% B+ K$ `; v4 C. x" H1 @8 ]) W% E
#define READY 2
/ y4 x" `/ E& I. e& a( _+ j/ |#define BLOCKED 3
/ x' z% c4 j2 f# r#define Ntcb 8
0 b1 r. G$ D+ s6 A5 `#define TL 17 h$ y' {# j- o4 `8 O. _& {0 J
struct tcb{5 q1 H9 {6 ^& c" p% V3 `0 k/ S0 y
unsigned int * stack;/*堆栈的起始地址*/2 t0 ?; z" o- N5 f* [7 ~7 o
unsigned ss; /*堆栈段址*/
% S( V9 |9 E8 K2 p+ r& {; w unsigned sp; /*堆栈指针址*/. Z( ^( ^ n3 _$ `
char state; /*进程状态*/' v s, g1 B$ h |
char name[10];/*线程的外部标识符*/
( G3 e" `: d% t/ g }tcb[Ntcb]; /* 系统允许最多的任务数 */
2 s; v! D5 i% r2 }9 M! W: x" wint timecount=0;
6 \3 G+ M; Q7 v) T0 J) K$ Xchar far *indos_ptr=0;
, R! ]1 J1 L: S/ lchar far *crit_err_ptr=0;
. L4 r( { o% `/ T) ]/ F1 p) kint num=0,current=0,i;
( C2 B5 o: r. mvoid over();& h: c$ S0 J" \0 m
typedef int (far*codeptr)(void);6 z1 J& p) y$ X
int create(char *name ,codeptr code ,int stacklen)2 H# b5 p1 N- v2 }! Y1 ^1 N
{
- ~( c0 n6 D N* K: e, Uunsigned int * s;0 T2 G. z& U! V" I5 G8 K
num++;5 _3 c, H# X" o/ h0 _& `& I
tcb[num].stack =(unsigned int*) malloc( stacklen*sizeof(int));: Q1 x& {8 E4 I
tcb[num].state = READY;
% L; l4 |# g. F" L7 V7 ]6 e' Y strcpy( tcb[num].name ,name);
! n3 r1 V6 T5 j# H* f6 L tcb[num].ss = FP_SEG(tcb[num].stack);, K- R4 Z$ r$ j( r5 ~' h
tcb[num].sp = FP_OFF(tcb[num].stack+stacklen-14);
* g3 E& s+ I: S; z- ] *(tcb[num].stack+stacklen-1) = FP_SEG(over);) c! i4 \5 s4 N0 V C8 h- m6 L
*(tcb[num].stack+stacklen-2) = FP_OFF(over);" F% {% K3 S9 d V
*(tcb[num].stack+stacklen-3) = 0x200;- W/ W# {5 j0 Z" |' t1 S: ^
*(tcb[num].stack+stacklen-4) = FP_SEG(code);
8 V1 e z( F4 a, W b9 d6 p$ ^ *(tcb[num].stack+stacklen-5) = FP_OFF(code);; c! D4 S, K" N8 g' B6 E1 w
*(tcb[num].stack+stacklen-10) = FP_SEG(tcb[num].stack); Z J6 y* {+ @0 U0 y
*(tcb[num].stack+stacklen-11) = FP_SEG(tcb[num].stack);
! K7 `; |: g. Z4 q/ ^1 | return num;
, _# |9 X, u$ ^! d) f} ;
( @7 L7 b: G+ O# |7 Z7 X* \void interrupt (* old_int8)(void);
) Y3 ?3 u: ~8 V# }, T5 uvoid interrupt new_int8(void)' j: ]7 Z% s/ V3 K/ n1 `
{# x+ d+ T5 H7 `+ N' U9 {
(*old_int8)();
/ ?8 N H4 R- |; T3 _ timecount++;' j2 n& X2 O1 ~
if(timecount>=TL);
+ {3 \9 l8 i. Z3 X2 M if(!DosBusy())3 k- X# R/ e1 N0 I, Z
{
( E* Z# m& }0 {: v' o* S A disable();
% i) N$ D) S+ V6 F6 S8 ^' i/ H8 n4 ^ tcb[current].ss=_SS;
# O |6 K2 x y3 F r6 Q9 n tcb[current].sp=_SP;
# P5 B1 [, k2 p1 @5 D if(tcb[current].state==RUNNING)
" `1 Q, D7 o* }- F8 ~5 W4 F tcb[current].state=READY;5 \& B0 d. x. `+ X; a' T
current=3-current;
1 r3 Q. N/ V2 z- Vif(tcb[current].state==READY)
# L3 a+ o7 ]5 D, N8 L! h. O9 D+ Z{ _SS=tcb[current].ss;
9 D3 P k% O; U: s4 h6 q3 Q6 ]8 h _SP=tcb[current].sp;
3 L3 X: N6 }) ~$ t1 _ tcb[current].state=RUNNING;
$ L2 j1 ?2 I" `; @ timecount=0;
: S1 S$ X7 ~$ X. z enable();
: O3 ]2 B+ f8 r0 `: m- _2 k6 k/ B/ p} }# h% ~/ T8 V
}
# c7 W* u% Z, N' j# Y5 y! z# c};! J( k/ _: ~' s+ p. P% I1 q
void tcb_state()
6 U, \/ B- [1 Z" W{
( _* O. {5 p1 C0 Q, B+ V int i;
7 r2 u( _) G1 A for(i=1;i<=num;i++)
3 _6 Z& ^% Y& S0 H1 L2 h printf("Thread %s is %d ",tcb.name,tcb.state);' \# O. S) ?4 R% q; J
};
, z: |2 q+ X& `int all_finished()
! X& Z2 D4 J8 O) |{
: \5 E/ a2 `' s4 C. {8 ~1 l1 t6 B int i;. F, Z" H0 N2 a1 o9 H! W0 `( E
for(i=1;i<=num;i++)- B- `# ?! d* e; z" H
if (tcb.state!=FINISHED)
. a# n% S0 N9 p' U6 d return 0;# X8 Q) n# [% W5 @* k
return 1;
! ^5 b. p/ L8 e( J' W% M/ z; Y1 ?) `" J" Z0 W/ y" c$ a9 N! p
};$ E) P; M; B! \& k/ q3 a* Y
void interrupt swtch(void)
' O) a) R9 D2 C+ N6 Z5 [{
" G3 m# D: j' [4 a! Aint i;; b( N* H' a# {9 a
disable();+ x! g& c5 V8 T& l: ^# L2 }
tcb[current].ss=_SS;8 v/ B. J: P. A" a1 B
tcb[current].sp=_SP;
' B# [7 M* K( i6 c6 k if(tcb[current].state==RUNNING)
! { ?+ r) o& S ^( N9 ^ tcb[current].state=READY;6 t: J4 l! i8 S
for( i= current+1 ; i <= num;i++)
4 Z: K$ r5 Z8 e# U) U { if(tcb.state == READY )) Q, a6 V8 C# v2 K
goto excute;
' K4 a, r! F" A }% B9 A1 S$ X' j8 `, s4 W
for( i = 1 ; i <= current ; i++)
, X: I# n0 o$ s: `% H" n { if(tcb.state == READY)
" z+ E' L4 m* _5 ?& Y+ _2 q goto excute;- t- N0 f! S" X9 \
}; Y0 N. F5 T# m
i = 0 ;
. v/ \1 z/ x, a, v0 b excute:
( W1 _6 W' T9 L; o _SS = tcb.ss;
5 D/ d2 C7 A3 c) ]1 h _SP = tcb.sp;
! X$ N- B6 z' c' S# l" A3 p tcb.state = RUNNING;
C/ Q6 V# n7 a3 j5 k current = i;" E) g; \8 |: l
enable();7 m* B, C' n/ t& K9 Y+ ~$ z
}' H! z3 u. ?# H
/*-------------------------------------------------------------------------*/1 f5 X1 j4 ~9 s2 U7 F+ s
void over()9 X3 ?$ @/ {* S; Q( Q! V' G3 M
{# u! Z% S( t# j5 V' ?9 l
int i=current;
3 P/ R9 R. j+ z+ K0 U# F /*strcpy(tcb[current].name,'\0');*/
5 c' q) ?2 Y8 g tcb[current].state=FINISHED; x4 \ K+ ^+ Z2 G5 d* H( `# q) V5 \
swtch();
6 A1 i L, C& Z" l9 K& X free(tcb.stack);
0 M3 d' {: [) L};2 Q% \ m- |2 A p
/*---------------------------------------------------*// j, }1 t6 Q0 w" D
void InitInDos(void), m# p0 B% G( c
{# ]( j- P& ~, Z0 z+ e' `
union REGS regs;
& l1 y& _6 ]) }0 N! E! T( x$ ^ struct SREGS segregs;
a1 k6 \7 m" _* ^, m# _( ^5 }8 k /* get the address to INDOS flag*/
1 Q# u0 z9 G9 S5 Y& v0 s+ _ regs.h.ah=GET_INDOS;
# x7 [' o3 n% a* s3 h intdosx(®s,®s,&segregs);
8 [; p/ ~ }* a }2 P; {6 @' g; @3 V' S8 s indos_ptr=MK_FP(segregs.es,regs.x.bx);
: ~5 ~/ K, L3 y9 i& q4 p& h S w /*get the address to CRIT_ERR flag*/
) Z) w7 V/ M' Y- n+ Y4 H if(_osmajor<3)
3 \" z; _* B3 R crit_err_ptr=indos_ptr+1;$ @$ Y- D9 ~% ?) l: h" w+ M+ w
else if (_osmajor==3&&_osminor==0)
1 g0 M- w3 E2 E# v9 ?. @- w crit_err_ptr=indos_ptr-1;" F6 U6 Q$ `& e/ @
else) {, U, e4 Y: d3 Y3 h; c% r" y
{( S4 o# H! o* n V$ ?2 t- V) I
regs.x.ax=GET_CRIT_ERR;
- ^& i, ?+ @! P intdosx(®s,®s,&segregs);
3 `+ L6 s( Z% V& P7 ^- F0 U crit_err_ptr=MK_FP(segregs.ds,regs.x.si);% |% E A( I: q6 m9 z" U: B7 g& }
}" Y$ n9 ]$ T( u/ j: S. ]+ ?) h1 q
};
P$ g8 Y) p9 Yint DosBusy(void)# D) N2 F" U7 C: c
{
1 R( ]- N, d [$ k( K5 X if(indos_ptr&&crit_err_ptr)* d4 x* M, B1 L
return(*indos_ptr&&*crit_err_ptr);* T+ j+ U' N. ^ Z; H9 F
else c" B4 n* A1 m) H0 \, x/ ?
return( -1);
+ Q+ Y2 B) y1 L5 t/ I8 K# l A};" M" n) r/ B( I% I5 B& i+ S3 O/ a
void f1(void)4 K/ m' N1 b& F( B8 C" G
{
- z7 T! q. k! n" j9 e int i,j,k;
, Y6 |+ f: b/ R- u Q( e" Z for(i=0;i<40;i++)
8 B4 W f; H& w: [3 ~7 e {
+ @+ B9 p; o9 k* [ F putchar('a');( T* h' k% @' L
$ L6 V1 J1 ]5 K9 H5 q( P
for(j=0;j<1000;j++)8 m1 I- ]9 _) F8 u: M
for(k=0;k<100;k++)
* t2 c8 f1 y0 c; v+ R ;, b# }- A$ B0 `+ P( r
}
" w6 L* n. u9 y- S; o& M};
! E# u& T& r) X: r" v/ cvoid f2(void)* U3 G; j2 }3 t% W! R. T
{$ {# z) m3 K- A' X5 d ?2 X
int i,j,k;
) _. v0 v& W& N& [8 d N for(i=0;i<30;i++)# a% g0 `: N* U7 U6 q
{' e: x! l8 h5 k" k( M
putchar('b');* K+ r! c1 E% k
for(j=0;j<1000;j++)
, F5 R4 V; C" z for(k=0;k<50;k++)
6 y w0 ~9 I) N7 `) j, O: ~5 M7 x8 U ;6 F f7 m5 A! u- F7 F
}
5 N9 `) F# C, g) n( Z; c};" J7 u2 b9 |* y6 l8 ?
/*------------------------------------------*/) C3 ]- ]" O3 {- G
main()
2 B: J3 e5 h- F) @2 n{, R J, S/ {9 m) h
InitInDos();7 e3 @0 r" U* J4 h& @
old_int8=getvect(8);8 r. e" P% U& R) ], o, i# d
strcpy(tcb[0].name,"main");
0 F& C% ]0 @, K& {/ J# J tcb[0].state=RUNNING;; _& x9 t7 l+ J2 Z3 z
current=0;
* I) W- ?$ p* q( Z" K9 k- m create("f1",(codeptr) f1,1024);
. S7 { Y4 W _ create("f2",(codeptr) f2,1024);
1 e" q: S8 L2 ~5 \/ H tcb_state();: @: z" }) K, K8 x- r8 R8 O
setvect(8,new_int8);/ o$ V9 A$ ^3 L8 Y$ {1 q
swtch();. q2 V9 b0 S) L4 ]' ]2 m7 Q
while(!all_finished());
5 l3 v6 ^: k( p5 j' V% C: E strcpy(tcb[0].name,'\0');) t2 _9 E- s! |3 F k
tcb[0].state=FINISHED;5 m8 v; n! [- C O) z
setvect(8,old_int8);
5 g6 P" u3 Z2 d1 U$ @+ k tcb_state();
9 u% J; W9 x9 L# J+ C printf("\n Multi_task system terminanted.\n");4 [4 t, H5 ~! G' o
}; |