?? 操作系統(tǒng)——pcb模擬.txt
字號:
mem->Sts = INUSE;
return mem;
}
}
last = now;
now = last->Next;
}
return NULL;
}
int ReleaseMem(MemBlock *mem)
{
MemBlock *last,*now;
if (MemTable==NULL) return FALSE;
last = MemTable;
now = last->Next;
if (last == mem)/*如果第一個就是要釋放的分區(qū)*/
{
last->Sts = IDLE;
if (now!=NULL && now->Sts==IDLE)/*如果后鄰接分區(qū)也是空閑的*/
{
last->Size = last->Size+now->Size;
free(now);
}
return TRUE;
}
while (now!=NULL)/*在鏈表中尋找要釋放的分區(qū)*/
{
if (now == mem)/*找到了*/
{
now->Sts = IDLE;
if (now->Next != NULL && now->Next->Sts==IDLE)/*察看后相鄰分區(qū)是否空閑*/
{
last->Next = now->Next;
now->Next->Offset = now->Offset;
now->Next->Size = now->Size + now->Next->Size;
free(now);
now = last->Next;
}
if (last->Sts == IDLE)/*察看前相鄰分區(qū)是否空閑*/
{
last->Next = now->Next;
last->Size = last->Size + now->Size;
free(now);
now = last->Next;
}
return TRUE;
}
last = now;
now = last->Next;
}
return FALSE;
}
/****************************************************************/
/*圖形系統(tǒng)相關(guān)函數(shù)聲明及全局變量定義*/
/****************************************************************/
#define HEIGHT 11
#define WIDTH 20
#define HN(n) HEIGHT*(n)
#define HNT(n) HEIGHT*(n)-1
/****************************************************************/
int InitGraph();/*初始化圖形系統(tǒng)*/
int DrawProcHeader(int x, int y);/*在x,y處繪制進程控制塊表頭結(jié)構(gòu)*/
int DrawProcStruct(int x, int y, PCB* pcb);/*在x,y處繪制進程控制塊pcb數(shù)據(jù)*/
int DrawAllProc();/*繪制進程列表*/
int DrawFrame();/*繪制圖形框架*/
int DrawMemStruct(int x, int y, MemBlock* mem);/*繪制分區(qū)mem的數(shù)據(jù)*/
int DrawMemTable();/*繪制分區(qū)表*/
int DrawConsole();/*繪制控制臺*/
int DrawConsoleHelp();/*繪制控制臺幫助信息*/
int DrawConsoleEcho();/*繪制控制臺命令提示符*/
int gprintf( int xloc, int yloc, char *fmt, ... );/*圖形系統(tǒng)中的格式輸出*/
/****************************************************************/
/*系統(tǒng)控制臺(用戶接口)相關(guān)函數(shù)聲明及相關(guān)全局變量*/
/****************************************************************/
enum _ConsoleCmd/*從系統(tǒng)控制臺輸入的命令枚舉*/
{
CMD_EXIT =0,/*退出系統(tǒng)*/
CMD_PAUSE,/*系統(tǒng)時間暫停*/
CMD_PROC,/*加入新的進程*/
CMD_READY,/*將某個掛起進程轉(zhuǎn)入就緒隊列*/
CMD_HIBAT,/*增加道數(shù)*/
CMD_LOBAT,/*減少道數(shù)*/
};
typedef enum _ConsoleCmd ConsoleCmd;
/****************************************************************/
char StrCmd[][6]={"exit\0","pause\0","proc\0","ready\0","hibat\0","lobat\0"};
char CmdString[30];
/****************************************************************/
int InitConsole();/*初始化控制臺*/
ConsoleCmd GetConsoleCmd();/*從控制臺輸入緩沖得到用戶命令*/
UINT GetData(int n);/*得到第n個數(shù)據(jù)參數(shù)(n>=1)*/
int ExecuteCmd();/*分析命令并得到命令*/
int CmdKeyDown(char ch);/*命令緩沖得到新的鍵盤輸入*/
/****************************************************************/
int InitGraph()
{
int GraphDriver, GraphMode;
GraphDriver = DETECT;
initgraph( &GraphDriver, &GraphMode, "" );
if( graphresult() != grOk )
return FALSE;
cleardevice();
return TRUE;
}
int DrawProcHeader(int x, int y)
{
gprintf(x,y,"ID");
gprintf(x+31,y,"Prio");
gprintf(x+72,y,"Time");
gprintf(x+113,y,"Offset");
}
int DrawProcStruct(int x, int y, PCB* pcb)
{
bar(x,y,x+197,y+HEIGHT-2);
if (pcb->PID>=0)/*繪制數(shù)據(jù)*/
{
gprintf(x,y,"%d", pcb->PID);
gprintf(x+32,y,"%d",pcb->Prior);
gprintf(x+73,y,"%d",pcb->Time);
gprintf(x+114,y,"%u",pcb->Mem->Offset);
}
else
{
gprintf(x,y,"idle process");
gprintf(x+114,y,"%u",pcb->Mem->Offset);
}
return TRUE;
}
int DrawAllProc()
{
int run=0,sus=0,red=0,i;
PCB *last, *now;
setfillstyle(1, BLACK);
if (ProcQueue==NULL) return FALSE;
last = ProcQueue;
now = last->Next;
switch (last->Sts)
{
case RUN: DrawProcStruct(1,HN(3+(run++)),last); break;
case SUSPEND: DrawProcStruct(201,HN(3+(sus++)),last); break;
case READY: DrawProcStruct(201,HN(22+(red++)),last); break;
}
while (now!=NULL)
{
switch (now->Sts)
{
case RUN: DrawProcStruct(1,HN(3+(run++)),now); break;
case SUSPEND: DrawProcStruct(201,HN(3+(sus++)),now); break;
case READY: DrawProcStruct(201,HN(22+(red++)),now); break;
}
last = now;
now = last->Next;
}
bar(1,HN(3+run),198,HN(26)+HEIGHT-2);/*擦除運行進程列表與控制臺之間的空隙*/
bar(201,HN(3+sus),398,HN(19)+HEIGHT-2);/*擦除掛起隊列與就緒隊列之間的空隙*/
bar(201,HN(22+red),398,478);/*擦除就緒隊列下面的空隙*/
rectangle(199,HNT(1),399,478);/*補上誤擦除的線*/
return TRUE;
}
int DrawFrame()
{
rectangle(0,HNT(1),639,479);
gprintf(200,0,"Multi-Batchs System Emulation");
rectangle(199,HNT(1),399,479);
rectangle(0,HNT(2),639,HNT(3));/*最上方的表頭框*/
rectangle(0,HNT(27),199,HNT(28));/*系統(tǒng)控制臺表頭框*/
rectangle(0,HNT(28),199,HNT(29));
rectangle(199,HNT(20),399,HNT(21));
rectangle(199,HNT(21),399,HNT(22));/*就緒進程隊列表頭框*/
gprintf(1,HN(1),"Running Processes");
DrawProcHeader(1,HN(2));
gprintf(201,HN(1),"Suspend Processes");
DrawProcHeader(201,HN(2));
gprintf(201,HN(20),"Ready Processes");
DrawProcHeader(201,HN(21));
gprintf(401,HN(1),"Memory Use Information");
gprintf(401,HN(2),"Offset");
gprintf(501,HN(2),"Size");
gprintf(601,HN(2),"Use");
gprintf(1,HN(27),"System Console");
return TRUE;
}
int DrawMemStruct(int x, int y, MemBlock* mem)
{
bar(x,y,x+237,y+HEIGHT-2);
gprintf(x,y,"%u", mem->Offset);
gprintf(x+100,y,"%u",mem->Size);
switch(mem->Sts)
{
case INUSE: gprintf(x+200,y,"USE"); break;
case IDLE: gprintf(x+200,y,"IDLE"); break;
}
return TRUE;
}
int DrawMemTable()
{
int num=0;
MemBlock *last, *now;
setfillstyle(1, BLACK);
if (MemTable==NULL) return FALSE;
now = MemTable;
while (now!=NULL)
{
DrawMemStruct(401,HN(3+(num++)),now);
now = now->Next;
}
bar(401,HN(3+num),638,478);/*擦除列表下面的空隙*/
line(639,HN(2),639,479);/*補上誤擦除的線*/
return TRUE;
}
int DrawConsole()
{
setfillstyle(1, BLACK);
bar(1,HN(28),198,HN(28)+HEIGHT-2);
DrawConsoleEcho();
gprintf(19,HN(28),CmdString);
return TRUE;
}
int DrawConsoleHelp()
{
gprintf(1,HN(29),"exit---Quit System");
gprintf(1,HN(30),"pause--Clock Pause");
gprintf(1,HN(31),"proc prior time memory ");
gprintf(1,HN(32),"-------Add New Process");
gprintf(1,HN(33),"ready ID");
gprintf(1,HN(34),"-------UnSuspend Process");
gprintf(1,HN(35),"hibat--Increase Batchs");
gprintf(1,HN(36),"lobat--Decrease Batchs");
return TRUE;
}
int DrawConsoleEcho()
{
static show = TRUE;
bar(1,HN(28),18,HN(28)+HEIGHT-2);
if (show)
gprintf(1,HN(28),">>");
else
gprintf(1,HN(28),"<<");
show = show?FALSE:TRUE;
return TRUE;
}
int gprintf( int xloc, int yloc, char *fmt, ... )
{
va_list argptr;
char str[140];
int cnt;
va_start( argptr, format );
cnt = vsprintf( str, fmt, argptr );
outtextxy( xloc+1, yloc+1, str );
va_end( argptr );
return( cnt );
}
/****************************************************************/
int InitConsole()
{
CmdString[0] = '\0';
return TRUE;
}
ConsoleCmd GetConsoleCmd()
{
char cmd[10];
int pos=0,i;
while (pos<10)
{
cmd[pos] = CmdString[pos];
if ((cmd[pos]<'A')
||(cmd[pos]>'Z' && cmd[pos]<'a')
||(cmd[pos]>'z')
)/*該字符不是字母*/
{
cmd[pos] = '\0';
break;
}
++pos;
}
cmd[9] = '\0';
for (i=0; i<6; i++)
{
if (strcmp(cmd, StrCmd[i])==0)
return (ConsoleCmd)(i);
}
return -1;
}
UINT GetData(int n)
{
char ch, buf[10];
int pos=0, space=0,i;
while (space<n && pos<20)/*未到達要讀取數(shù)據(jù)的位置*/
{
ch = CmdString[pos++];
if (ch==' ')/*如果找到空格*/
++space;
}
for (i=0; i<10; i++)
{
buf[i] = CmdString[pos+i];
if (buf[i]<'0' || buf[i]>'9')/*不是數(shù)字*/
{
if (i==0)/*第一個字符便不是數(shù)字*/
return 0;
buf[i] = '\0';
break;
}
}
return (UINT)atoi(buf);
}
int ExecuteCmd()
{
switch (GetConsoleCmd())
{
case CMD_EXIT:
return FALSE;
case CMD_PAUSE:
flushall();
bar(18,HN(28),197,HN(28)+HEIGHT-2);
gprintf(18,HN(28),"press any key...");
getch();
flushall();
break;
case CMD_PROC:
InsertProc(GetData(1),GetData(2),GetData(3));
UpdateBatchs();
break;
case CMD_READY:
UnSuspendProc(GetData(1));
UpdateBatchs();
break;
case CMD_HIBAT:
AddBatch();
UpdateBatchs();
break;
case CMD_LOBAT:
DeleteBatch();
UpdateBatchs();
break;
default: break;
}
return TRUE;
}
int CmdKeyDown(char ch)
{
static int pos = 0;
int ret;
if (ch == ENTER)
{
CmdString[pos] = '\0';
ret = ExecuteCmd();
pos=0;
CmdString[0] = '\0';
return ret;
}
else if (ch == BACKSPACE)
{
if (pos>0)
CmdString[--pos] = '\0';
return TRUE;
}
else
{
CmdString[pos] = ch;
CmdString[++pos] = '\0';
if (pos>20)
pos=20;
return TRUE;
}
}
/****************************************************************/
/*main函數(shù)*/
int main()
{
clock_t start=0, end=0;
char ch;
if (!InitConsole())
{
printf("can;t initialize console");getch();
return FALSE;
}
if (!InitMem())
{
printf("can't initialize memory");getch();
return FALSE;
}
if (!InitBatchs(3))
{
printf("can't initialize system batchs");getch();
return FALSE;
}
if (!InitGraph())
{
printf("can't initialize graphics system");getch();
return FALSE;
}
DrawFrame();
DrawAllProc();
DrawMemTable();
DrawConsole();
DrawConsoleHelp();
while (TRUE)
{
start = end = clock();
while (!kbhit())
{
start = clock();
if ((start-end)/18.2 >= 1)/*時間過了一秒*/
{
start = end = clock();
DrawConsoleEcho();
PassSeconds(1);
DrawAllProc();
DrawMemTable();
}
}
ch = getch();
if (ch == ESC)
return TRUE;
if ((ch>='0' && ch<='9')/*如果字符是數(shù)字*/
|| (ch>='A' && ch<='Z')
|| (ch>='a' && ch<='z')/*或是字母*/
|| (ch==ENTER)/*如果是回車*/
|| (ch==' ')/*是空格*/
|| (ch==BACKSPACE)/*是退格*/
)
{
if (CmdKeyDown(ch)==FALSE)/*如果執(zhí)行了exit命令*/
return TRUE;
else if (ch==ENTER)/*如果執(zhí)行了命令*/
{
DrawAllProc();
DrawMemTable();
}
DrawConsole();
}
}
return TRUE;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -