操作系统学习

5 views
Skip to first unread message

earlyfly

unread,
Nov 28, 2007, 10:15:44 AM11/28/07
to earlyfly
/* This is a muti-task opeation system kernel demo code,
Date : 2001-6-13
Author: peng liangqing; HeiFei university Of Technology
File name: PLQOS.c
Compline:Turbo C2.0 */
#include<stdio.h>
#include<conio.h>
#include<dos.h>
/
**************************************************************************-
*
****
--------------Os Kernel Code Decalre Is Start */
/* PCB declare:
#define MAX_APPPROC_NUM 10
typedef struct {
int PID;
void ( *AppProcInit)( void );
void ( *AppProc)( void *Message );
µØÖ·
*/
}PCB;
typedef int PID;
/* Message Data Struct declare:
#define MAX_MESSAGE_LEN 48
#define MAX_MESSAGE_NUM 100
typedef struct {
int SendPID,
RecvPID,
Link,
mLen;
char Message[MAX_MESSAGE_LEN];


}MessageQueueTerm;


MessageQueueTerm MyMessageQueue[ MAX_MESSAGE_NUM ];
int QueueHead =0, QueueEnd=0 ;
/* Timer declare: */
#define MAX_TIMER_NUM 30
enum TimerType { ISTIMER=1, ISTIMETASK=2, NULLTYPE =0};
typedef struct {
int Timer_id,
Type,
Time,
TimeBackup;
void ( *TimerProc)( void );

}TIMER;


TIMER Timer_Table[MAX_TIMER_NUM ];
int FlagOf55ms = 0;
void interrupt Timer55ms( void );
void interrupt (* BiosInt8)(void);
/*OS kernel function prototype decalre: */
void main(void);
void DispatchProcess(int PID , MessageQueueTerm *Message);
void KernelSystemInit( void );
void ProcInit( void );
PID ReadMessageQueue(MessageQueueTerm *Message );
void DispatchProcess(int PID , MessageQueueTerm *Message);
void TimerProcess( void );
void interrupt Timer55ms( void );
/* os kernel function prototype used by application process: */
void TimerStartup(int Timer_id, int Type, int Time, void
( *TimerProc)(v
oid ) );
void SendMessage(PID RecvPID, PID SendPID, int mLen, char
*Message)
;
/* --------------Os Kernel Code Decalre Is End
***************************************************************************-
*
***/
/
**************************************************************************-
*
***
----------------Fellowing is application process declare */
/* application process function prototype declare: */
void KeyProcInit( void );
void KeyProc( void *Message);
void TimeTaskReadKey(void );
void DisplayProcInit(void ) ;
void DisplayProc(void *Message );
void TimerDisplayStr( void );
void TimerClrStr(void );
void TimeTaskClock(void ) ;

/* Application process varity decale: */
enum ProcID {
NULL_PID=0,
KEY_PID=1,
DISPLAY_PID=2
};
enum AppTimerID {
NULL_TID=0,
READKEY_TID=1,
DISPLAYSTR_TID=2,
CLRSTR_TID=3,
CLOCK_TID=4
};
PCB ProcTable[MAX_APPPROC_NUM]={
{ KEY_PID, KeyProcInit, KeyProc },
{ DISPLAY_PID, DisplayProcInit , DisplayProc}
};
/*------------------the end of application process decale
***********************************************************************
*/
/
*************************************************************************
----------------------Fellowing is os kernel code */
void main(void )
{
int RecvPID;
MessageQueueTerm Message;
textmode(C80); clrscr();
printf("Real time operation system demo program\n");
KernelSystemInit( );
ProcInit( );
/*add by myself,these are inited in the produce ProcInit().*/
/*
KeyProcInit();
DisplayProcInit();
*/
printf("Please input any digit(0---exit)\n");
while(1){
if ( FlagOf55ms ==1)
{
TimerProcess( );
FlagOf55ms =0;
};

if( (RecvPID=ReadMessageQueue( &Message ))!=NULL_PID)

DispatchProcess(RecvPID, &Message);
}
setvect(0x08, BiosInt8);

}


void KernelSystemInit( void )
{
int i;
BiosInt8=getvect(0x8);
setvect(0x08, Timer55ms);
for(i=0; i<MAX_MESSAGE_NUM; i++)
MyMessageQueue[i].RecvPID=
MyMessageQueue[i].SendPID=NULL_PID;

}


void ProcInit( void )
{
int i;
for(i=0;i< MAX_APPPROC_NUM; i++)
if( ProcTable[i].PID==NULL_PID) continue;
else
( *ProcTable[i].AppProcInit)( ) ;
}
void TimerProcess( void )
{
int i;
for (i=0; i<MAX_TIMER_NUM ; i++ )
{
if ( Timer_Table[i].Type==NULLTYPE ) continue;
if ( Timer_Table[i].Time==0 ){
( *Timer_Table[i].TimerProc)( ) ;
if( Timer_Table[i].Type==ISTIMETASK )
Timer_Table[i].Time = Timer_Table[i].TimeBackup;
else Timer_Table[i].Type=NULLTYPE; }
else Timer_Table[i].Time--;
};

}


PID ReadMessageQueue(MessageQueueTerm *Message )
{
int i,pid;
if ( MyMessageQueue[QueueHead].RecvPID==NULL_PID)
return(NULL_PID);
pid=MyMessageQueue[QueueHead].RecvPID;
for( i=0; i< MyMessageQueue[QueueHead].mLen; i++ )
Message->Message[i]=MyMessageQueue[QueueHead].Message[i];
MyMessageQueue[QueueHead].RecvPID=NULL_PID;
QueueHead++;
if ( QueueHead >= MAX_MESSAGE_NUM) QueueHead=0;
return(pid);

}


void DispatchProcess(int RecvPID , MessageQueueTerm *Message) /
* Ó|ÓÃ
1/2ø³Ìµ÷Óà */
{
int i;
for(i=0; i<MAX_APPPROC_NUM; i++)
if ( RecvPID== ProcTable[i].PID )
( *ProcTable[i].AppProc)( (void *)Message) ;
}


void interrupt Timer55ms( void )
{
(*BiosInt8)( );
FlagOf55ms = 1;

}


void TimerStartup(int Timer_id, int Type, int Time, void
( *TimerProc)(v
oid ) )
{
int i;
for(i=0; i< MAX_TIMER_NUM; i++ )
if( Timer_Table[i]. Type==0 ) {
Timer_Table[i].Type =Type;
Timer_Table[i].Timer_id =Timer_id;
Timer_Table[i].Time =Time/50;
Timer_Table[i].TimeBackup =Time/50;
Timer_Table[i].TimerProc =TimerProc;
return;
}


}


void SendMessage(PID RecvPID, PID SendPID, int mLen, char
*Message)

{
int i;
MyMessageQueue[QueueEnd].RecvPID=RecvPID;
MyMessageQueue[QueueEnd].SendPID=SendPID;
MyMessageQueue[QueueEnd].mLen =mLen;
for( i=0; i< mLen; i++ )
MyMessageQueue[QueueEnd].Message[i]=Message[i];
QueueEnd++;
if ( QueueEnd >= MAX_MESSAGE_NUM) QueueEnd=0;


}


/* ------------------------OS Kernel code is end
***************************************************************************-
*
*****/
/
**************************************************************************-
*
******
------------------------Fellowing is appcation process code */

void KeyProcInit( void )
{ TimerStartup( READKEY_TID, ISTIMETASK, 200,
TimeTaskReadKey ); }
void KeyProc( void *Message)
{ }
void TimeTaskReadKey( void )
{
char key;
if ( (key=bioskey(1))==0) return;
key=getch();
if(key=='0'){
printf("\nReal Time Operation System Test Is End ");
setvect(0x08, BiosInt8);
exit(0);
}
SendMessage(DISPLAY_PID,KEY_PID , 1, &key);

}


void DisplayProcInit(void )
{ TimerStartup( CLOCK_TID, ISTIMETASK, 1000, TimeTaskClock ); }
void DisplayProc(void *Message)
{
MessageQueueTerm *p;
int time;
p=(MessageQueueTerm *)Message;
time=p->Message[0]-'0';
time=1;
TimerStartup( DISPLAYSTR_TID, ISTIMER, time*200,
TimerDisplayStr );

}


void TimerDisplayStr( void )
{
gotoxy(30,15); puts("****Welcome To You********");
TimerStartup( CLRSTR_TID, ISTIMER, 2*1000, TimerClrStr );

}


void TimerClrStr(void )
{
gotoxy(30,15); puts(" ");

}


void TimeTaskClock(void )
{
static i=0;
gotoxy(30,20); printf("Second=%d",i++);

}


/* ------------------------The end of appcation process code
**************************************************************************/
Reply all
Reply to author
Forward
0 new messages