⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mos_main.cpp

📁 用VC编写的一个微型操作系统
💻 CPP
字号:
// Mos_main.cpp: implementation of the Mos_main class.
// Wrote by biti_zx. Released April,2002.
// Copyright (C) 2002 by biti_zx.
// All rights reserved.
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "mos.h"
#include "Mos_main.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
extern CVCS m_cpu;
extern PCB  m_pcb;
extern JCB  m_jcb;
extern CMos_stor m_stor;
extern CMos_io m_io;
Mos_main::Mos_main()
{

}

Mos_main::~Mos_main()
{

}

void Mos_main::Deal_int(int n, char *to, char *from)
{
	char tempB[5];
	_itoa(n,tempB,10);
	strcat(from,tempB);
	m_UI.PrtMsg(from);
	strcat(from,"\n");
	strcat(to,from);
}

void Mos_main::PrintSysMsg()
{
	char Buff[250];
	char prtbuf[50];
	strcpy(prtbuf,"******System Messages******");
	m_UI.PrtMsg(prtbuf);
	strcpy(Buff,prtbuf);
	strcat(Buff,"\n");

	strcpy(prtbuf,"Total kernel time: ");
	Deal_int(m_cpu.kerneltime,Buff,prtbuf);

	strcpy(prtbuf,"Total user time: ");
	Deal_int(m_cpu.usertime,Buff,prtbuf);

	strcpy(prtbuf,"Total idle time: ");
	Deal_int(m_cpu.idletime,Buff,prtbuf);

	strcpy(prtbuf,"Cpu using rate: ");
	char buff[4];
	_itoa((m_cpu.rtime-m_cpu.idletime)*100/m_cpu.rtime,buff,10);
	strcat(prtbuf,buff);
	strcat(prtbuf,"%");
	m_UI.PrtMsg(prtbuf);
	strcat(prtbuf,"\n");
	strcat(Buff,prtbuf);

	strcpy(prtbuf,"Total io times: ");
	Deal_int(m_cpu.io,Buff,prtbuf);

	strcpy(prtbuf,"Total jobs: ");
	Deal_int(m_cpu.totaljob,Buff,prtbuf);

	strcpy(prtbuf,"Average turnover time: ");
	int average = m_cpu.turnovertimes/m_cpu.totaljob;
	Deal_int(average,Buff,prtbuf);

	strcpy(prtbuf,"System shutdown time: ");
	Deal_int(m_cpu.rtime,Buff,prtbuf);

	fwrite(Buff,sizeof(char),strlen(Buff),m_io.print);
}

bool Mos_main::term()
{
	if(m_cpu.FatalError||(m_pcb.free_pcb_count == (PCBMAX-3)&&
		m_pcb.kbend_head==m_pcb.PcbTab&&
		m_pcb.jcbnull_head==(m_pcb.PcbTab+1)&&
		m_pcb.waitout_head==(m_pcb.PcbTab+2)))
	{
		PrintSysMsg();
		m_UI.DealWithUI(" ",IDC_STATIC_WJOB);
		m_UI.DealWithUI(" ",IDC_STATIC_WRJ);
		m_UI.DealWithUI(" ",IDC_STATIC_WOUT);
		m_UI.DealWithUI("0",IDC_STATIC_PN);
		return true;
	}
	else return false;
}
//////////////////////////////////////////////////////////////////
//Mos_main::init()
//////////////////////////////////////////////////////////////////
void Mos_main::init()
{
	m_cpu.C = '0';
	for(int i = 0;i<=2;i++)
	{
		m_cpu.CHCOUNT[i] = 0;
		m_cpu.CHST[i] = FREE;
		m_UI.DealWithUI("0",IDC_STATIC_IOKB-i);
		m_UI.DealWithUI("FREE",IDC_STATIC_KBS+i);
	}
	m_cpu.MODE = IDLE;
	m_cpu.FatalError = false;
	m_cpu.PI = 0;
	m_cpu.TI = 0;
	m_cpu.IOI = 0;
	m_cpu.SI = 0;

	m_cpu.PC[0] = m_cpu.PC[1] = '0';
	m_cpu.PTR.op1 = m_cpu.PTR.op2 = m_cpu.PTR.op3 = m_cpu.PTR.op4 = '0'; 
	m_cpu.R[0] = m_cpu.R[1] = m_cpu.R[2] = m_cpu.R[3] =  '0';
	m_cpu.rtime = m_cpu.time = m_cpu.idletime = 0;
	m_cpu.usertime =m_cpu.io = m_cpu.turnovertimes = m_cpu.totaljob = m_cpu.kerneltime = 0;

	for(i=0;i<300;i++)
		m_cpu.memory[i].op1=m_cpu.memory[i].op2=m_cpu.memory[i].op3=m_cpu.memory[i].op4='0';
////////以上初始化CPU寄存器、内存及相关数据////////////////////////
/* PCBMAX:8
	P1[0 ]
  	P2[1 ]
	P3[2 ]
user->[3 ]
	  [4 ]
	  [5 ]
	  [6 ]
	  [7 ]
*/
	for(i=3;i<PCBMAX-1;i++)
	{
		m_pcb.PcbTab[i].next = m_pcb.PcbTab+i+1;
		m_pcb.PcbTab[i].pid = i+1;
		m_pcb.PcbTab[i].jcb = NULL;
		m_pcb.PcbTab[i].iorbp = NULL;
		m_pcb.PcbTab[i].mode = USERMODE;
	}
	m_pcb.PcbTab[PCBMAX-1].next = NULL;
	m_pcb.PcbTab[PCBMAX-1].jcb = NULL;
	m_pcb.PcbTab[PCBMAX-1].iorbp = NULL;
	m_pcb.PcbTab[PCBMAX-1].mode = USERMODE;
	m_pcb.PcbTab[PCBMAX-1].pid = PCBMAX;

	m_pcb.pcb_free_head = m_pcb.PcbTab+3;
	m_pcb.pcb_free_tail = m_pcb.PcbTab+PCBMAX-1;
	m_pcb.free_pcb_count = PCBMAX-3;
//////////////////以上建立PCB空闲链////////////////////////
	for(i=0;i<JCBMAX-1;i++)
	{
		m_jcb.JcbTab[i].jnext = m_jcb.JcbTab+i+1;
		m_jcb.JcbTab[i].error = 0;
	}
	m_jcb.JcbTab[JCBMAX-1].jnext = NULL;
	m_jcb.JcbTab[JCBMAX-1].error = 0;

	m_jcb.jcbfree_head = m_jcb.JcbTab;
	m_jcb.jcbfree_tail = m_jcb.JcbTab+JCBMAX-1;
	m_jcb.free_jcb_count = JCBMAX;
	m_UI.DealWithUI("0",IDC_STATIC_JN);
//////////////////以上建立JCB空闲链////////////////////////
	for(i=0;i<29;i++)
	{
		m_stor.MEMTB[i].used=0;
		m_stor.MEMTB[i].index=i;
		m_stor.MEMTB[i].forwp=m_stor.MEMTB+i+1;
	}
    m_stor.MEMTB[29].used=0;
	m_stor.MEMTB[29].index=29;
    m_stor.MEMTB[29].forwp=NULL;

    m_stor.MEMPTR=m_stor.MEMTB + 0;//head
    m_stor.MEMTRA=m_stor.MEMTB + 29;//tail
    m_stor.MEMFRC=30;//count

	for(i=0;i<99;i++)
	{
		m_stor.DSKMTB[i].used=0;
		m_stor.DSKMTB[i].index=i;
		m_stor.DSKMTB[i].forwp=m_stor.DSKMTB+i+1;
	}
	m_stor.DSKMTB[99].used=0;
	m_stor.DSKMTB[99].index=99;
	m_stor.DSKMTB[99].forwp=NULL;

	m_stor.DSPTR=m_stor.DSKMTB+0;//head
	m_stor.DSTRA=m_stor.DSKMTB+99;//tail
	m_stor.DSFRC=100;//count
////////////以上初始化MEMTB、DSKMTB链///////////////////////////
	m_jcb.jcb_head = m_jcb.jcb_tail = m_jcb.out_head = m_jcb.out_tail = NULL;
	
	m_pcb.curr_pcb = m_pcb.jcbnull_head = m_pcb.kbend_head = m_pcb.waitjcb_head 
	= m_pcb.waitpcb_head= m_pcb.waitmem_head = m_pcb.waitout_head = NULL;
	m_pcb.dsk_head = m_pcb.dsk_tail = NULL;
	m_pcb.readypcbhead = m_pcb.readypcbtail = NULL;
	m_pcb.restdsk_head = m_pcb.restdsk_tail = NULL;
	m_pcb.restkb_head = m_pcb.restkb_tail = NULL;
	m_pcb.restprt_head = m_pcb.restprt_tail = NULL;
	for(i=0;i<3;i++)
		m_io.CHHEAD[i]=m_io.CHTAIL[i]=NULL;
//////////////以上初始化指针、队列//////////////////////////////
	for(i=0;i<3;i++)
	{
		for(int j=0;j<2;j++)  m_pcb.PcbTab[i].regpc[j]=0;
		for(j=0;j<4;j++)
		{
			m_pcb.PcbTab[i].regr[j]=0;
		    m_pcb.PcbTab[i].regptr[j]=0;
		}
		m_pcb.PcbTab[i].mode=KERNELMODE;
		m_pcb.PcbTab[i].step=0;
		m_pcb.PcbTab[i].pid = i+1;
		m_pcb.PcbTab[i].regc='0';
		m_pcb.PcbTab[i].cputime=0;
		m_pcb.PcbTab[i].io=0;
		m_pcb.PcbTab[i].errn=0;
		m_pcb.PcbTab[i].iorbp=NULL;
		m_pcb.PcbTab[i].outp=0;
		m_pcb.PcbTab[i].outp=0;
		m_pcb.PcbTab[i].olimit=0;
		m_pcb.PcbTab[i].resultline=0;
		m_pcb.PcbTab[i].inp=0;
		m_pcb.PcbTab[i].ilimit=0;
		m_pcb.PcbTab[i].jcb=NULL;
		m_pcb.PcbTab[i].next=NULL;
    }//end of for

    m_pcb.PcbTab[0].flag=READY;
	m_pcb.PcbTab[1].flag=m_pcb.PcbTab[2].flag=SLEEP;
	pcbtype *p =m_pcb.PcbTab;
    m_proc.rearrange(p,m_pcb.readypcbhead,m_pcb.readypcbtail,KERNELMODE);
	//P1
    m_pcb.jcbnull_head = m_pcb.PcbTab+1;//P2
	m_pcb.waitout_head = m_pcb.PcbTab+2;//P3
	m_UI.DealWithUI("->P1",IDC_STATIC_RP);
	m_UI.DealWithUI("->P2",IDC_STATIC_WRJ);
	m_UI.DealWithUI("->P3",IDC_STATIC_WOUT);
	m_UI.DealWithUI("3",IDC_STATIC_PN);
////////////////////////以上创建三个系统进程///////////////////////
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -