荔园在线
荔园之美,在春之萌芽,在夏之绽放,在秋之收获,在冬之沉淀
[回到开始]
[上一篇][下一篇]
发信人: jjk (Linux Device Driver), 信区: Linux
标 题: 一个简单的模拟操作系统C代码(转寄)[转载]
发信站: 荔园晨风BBS站 (Wed May 15 13:15:48 2002), 转信
【 以下文字转载自 jjk 的信箱 】
【 原文由 jjk.bbs@apue.dhs.org 所发表 】
发信人: init (王阿呆~啥也不会...), 信区: UKP
标 题: 一个简单的模拟操作系统C代码
发信站: UNIX编程 (2002年05月08日18:18:04 星期三), 转信
发信人: danceeagle (danceeagle), 信区: SysInternals
标 题: 一个简单的模拟操作系统C代码
发信站: 武汉白云黄鹤站 (2002年05月07日16:05:01 星期二), 转信
/* 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 { /* PCB表定义 */
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, /* 队列连接指针,表明后面一个消息位置(数组下
标) */
/* 指针由OS填写 */
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, /* 定时器标识ID */
Type, /* 类型,1-表示是定时器,2=表示是定时任务 0- 空*/
Time, /* 定时时长(周期),时间单位见下文第 4小节 说明
*/
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: 以下是OS内核函数的原形申明 */
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:提供给应用进程使
用的OS内核函数 */
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, /* 键盘进程ID */
DISPLAY_PID=2 /* 显示进程ID */
};
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);
/* 根据进程号并查阅PCB表来调用该PID号对应的进程 */
}
setvect(0x08, BiosInt8);
}
void KernelSystemInit( void ) /* OS内核系统初始化 */
{
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) /* 应用
进程调用 */
{
int i;
for(i=0; i<MAX_APPPROC_NUM; i++)
if ( RecvPID== ProcTable[i].PID ) /* 根据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 ) )
{ /*入口参数:定时器ID号,类型,时长,超时处理函数代码的首指
针 */
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
**************************************************************************/
--
※ 来源:·武汉白云黄鹤站 bbs.whnet.edu.cn·[FROM: 202.193.65.8]
--
[root@init init]# rpm -ivh init-0.01.i386.rpm
1.init ####### [10%]
※ 来源:·UNIX编程 apue.dhs.org·[FROM: 202.114.36.201] --
※ 转寄:·UNIX编程 apue.dhs.org·[FROM: 210.39.3.50]
--
※ 转载:·荔园晨风BBS站 bbs.szu.edu.cn·[FROM: 192.168.0.146]
[回到开始]
[上一篇][下一篇]
荔园在线首页 友情链接:深圳大学 深大招生 荔园晨风BBS S-Term软件 网络书店