进程管理模拟系统源代码.docx
《进程管理模拟系统源代码.docx》由会员分享,可在线阅读,更多相关《进程管理模拟系统源代码.docx(16页珍藏版)》请在冰点文库上搜索。
进程管理模拟系统源代码
#include"stdio.h"
#include"graphics.h"
#include"dos.h"
#include"stdlib.h"
#include"conio.h"
#defineSEC3
#defineNULL0
charhan1S[]={
0x00,0x80,0x00,0x80,0xFC,0x80,0x04,
0xFC,0x45,0x04,0x46,0x48,0x28,0x40,
0x28,0x40,0x10,0x40,0x28,0x40,0x24,
0xA0,0x44,0xA0,0x81,0x10,0x01,0x08,
0x02,0x0E,0x0C,0x04,
};
charhan2S[]={
0x00,0x00,0x41,0x84,0x26,0x7E,0x14,
0x44,0x04,0x44,0x04,0x44,0xF4,0x44,
0x14,0xC4,0x15,0x44,0x16,0x54,0x14,
0x48,0x10,0x40,0x10,0x40,0x28,0x46,
0x47,0xFC,0x00,0x00,
};
charhan3S[]={
0x02,0x20,0x42,0x20,0x22,0x28,0x2F,
0xFC,0x02,0x20,0x02,0x20,0xE2,0x20,
0x22,0x28,0x2F,0xFC,0x22,0x20,0x22,
0x20,0x22,0x20,0x24,0x20,0x50,0x26,
0x8F,0xFC,0x00,0x00,
};
charhan4S[]={
0x04,0x00,0x02,0x00,0x01,0x00,0x01,
0x00,0x01,0x00,0x02,0x80,0x02,0x80,
0x02,0x80,0x04,0x40,0x04,0x40,0x08,
0x20,0x08,0x20,0x10,0x10,0x20,0x10,
0x40,0x0E,0x80,0x04,
};
charhan5S[]={
0x10,0x10,0x11,0xF8,0x11,0x10,0x11,
0xF0,0xFC,0x04,0x17,0xBE,0x14,0xA4,
0x1F,0xBC,0x30,0x40,0xD0,0x44,0x1F,
0xFE,0x10,0xE0,0x11,0x50,0x12,0x4E,
0x54,0x44,0x20,0x40,
};
charhan6S[]={
0x09,0x00,0x09,0x00,0x09,0x04,0x11,
0xFE,0x12,0x80,0x32,0x80,0x54,0x90,
0x98,0xF8,0x10,0x80,0x10,0x80,0x10,
0x88,0x10,0xFC,0x10,0x80,0x10,0x80,
0x10,0x80,0x10,0x80,
};
charhan7S[]={
0x00,0x38,0x7F,0xC0,0x04,0x00,0x04,
0x10,0x08,0x20,0x3F,0xC0,0x01,0x00,
0x02,0x20,0x04,0x10,0x3F,0xF8,0x01,
0x08,0x09,0x20,0x09,0x10,0x11,0x08,
0x25,0x08,0x02,0x00,
};
charhan8S[]={
0x10,0x80,0x10,0x40,0x20,0x48,0x27,
0xFC,0x48,0x80,0xF9,0x10,0x12,0x08,
0x27,0xFC,0x41,0x24,0xF9,0x20,0x41,
0x20,0x01,0x20,0x1A,0x22,0xE2,0x22,
0x44,0x1E,0x08,0x00,
};
charhan9S[]={
0x11,0x10,0x11,0x14,0x1F,0xFE,0x11,
0x10,0xFD,0x18,0x13,0xFC,0x32,0x08,
0x3B,0xF8,0x56,0x08,0x53,0xF8,0x90,
0x40,0x1F,0xFE,0x10,0x40,0x10,0xB0,
0x11,0x0E,0x16,0x04,
};
charhan10S[]={
0x10,0x00,0x10,0x08,0x10,0x88,0x12,
0x48,0xFE,0x68,0x12,0x28,0x16,0x08,
0x1A,0x08,0x32,0x08,0xD2,0x08,0x12,
0x48,0x12,0x88,0x13,0x14,0x12,0x22,
0x50,0x42,0x20,0x80,
};
charhan11S[]={
0x02,0x20,0x42,0x20,0x22,0x28,0x2F,
0xFC,0x02,0x20,0x02,0x20,0xE2,0x20,
0x22,0x28,0x2F,0xFC,0x22,0x20,0x22,
0x20,0x22,0x20,0x24,0x20,0x50,0x26,
0x8F,0xFC,0x00,0x00,
};
charhan12S[]={
0x08,0x04,0x1D,0xFE,0xF1,0x04,0x11,
0x04,0x11,0x04,0xFF,0x04,0x11,0xFC,
0x38,0x00,0x37,0xFE,0x54,0x20,0x50,
0x28,0x91,0xFC,0x10,0x20,0x10,0x24,
0x17,0xFE,0x10,0x00,
};
charhan13S[]={
0x10,0x40,0x1F,0x7C,0x28,0x90,0x45,
0x08,0x01,0x00,0x7F,0xFE,0x40,0x02,
0x9F,0xF4,0x10,0x10,0x1F,0xF0,0x10,
0x00,0x1F,0xF0,0x10,0x10,0x10,0x10,
0x1F,0xF0,0x10,0x00,
};
charhan14S[]={
0x00,0x08,0x13,0xFC,0xFA,0x48,0x22,
0x48,0x23,0xF8,0x22,0x48,0xFA,0x48,
0x23,0xF8,0x20,0x40,0x20,0x50,0x23,
0xF8,0x3C,0x40,0xE0,0x40,0x40,0x44,
0x0F,0xFE,0x00,0x00,
};
voiddrawmat(char*mat,intmatsize,intx,inty,intcolor)
{inti,j,k,n;
n=(matsize-1)/8+1;
for(j=0;jfor(i=0;ifor(k=0;k<8;k++)
if(mat[j*n+i]&(0x80>>k))
putpixel(x+i*8+k,y+j,color);
}
/*定义结构体*/
typedefstructPCB
{
intPID;
intUID;
structPCB*next;
}PCB;
PCB*really,*excute,*wait;
/*createqueueheader*/
/*queueoperation入队*/
intenqueue(PCB*head,PCB*node)
{
PCB*p;
p=head;
if(p->next==NULL)
{
head->next=node;
return1;
}
while(p)
{
if(p->next==NULL)
{
p->next=node;
return1;
}
elsep=p->next;
}
}/*enquue*/
/*dequeue出队列*/
PCB*dequeue(PCB*head)
{
PCB*p;
p=head;
if(p->next==NULL)
{
returnNULL;
}
else
{
p=p->next;
head->next=p->next;
p->next=NULL;
returnp;
}
/*headtonext*/
}/*dequeue*/
/*PCBoperate*/
/*新建进程*/
intcreate()
{
PCB*p;
p=(PCB*)malloc(sizeof(PCB));
p->next=NULL;
printf("inputPIDandUIDtoanewprocess\n");
scanf("%d%d",&p->PID,&p->UID);
if(enqueue(really,p))
printf("createaprocess:
PID=%dUID=%d\n",p->PID,p->UID);
else
printf("createFailed\n");
}/*create*/
/*执行fexcute*/
intfexcute()
{
PCB*p=dequeue(really);
if(p==NULL)
{
printf("NOprocessinqueue\n");
return0;
}
else
{
enqueue(excute,p);
printf("addaprocessintoexcutequeueprocess:
PID=%dUID=%d\n",p->PID,p->UID);
return1;
}
}/*excute*/
intwake()
{
PCB*p=dequeue(wait);
if(p==NULL)
{
printf("NOprocessinqueue\n");
return0;
}
else
{
enqueue(really,p);
printf("addaprocessintowaitreallyprocess:
PID=%dUID=%d\n",p->PID,p->UID);
return1;
}
}
intblock()
{
PCB*p=dequeue(excute);
if(p==NULL)
{
printf("NOprocessinqueue\n");
return0;
}
else
{
enqueue(wait,p);
printf("addaprocessintowaitqueueprocess:
PID=%dUID=%d\n",p->PID,p->UID);
return1;
}
}/*block*/
/*输出队列outputqueue*/
intoutputqueue(PCB*head)
{
PCB*p;
if(head->next==NULL)
{/*队列为空*/
printf("queueisnull\n");
return1;
}
p=head->next;
while(p)
{/*打印processidUID*/
printf("PID=%dUID=%d\n",p->PID,p->UID);
p=p->next;
}
return0;
}
/*output输出*/
intoutput()
{
printf("REALLLYQUEUE:
\n");
outputqueue(really);
printf("EXCUTEQUEUE:
\n");
outputqueue(excute);
printf("WAITQUEUE:
\n");
outputqueue(wait);
}/*output*/
/*init初始化*/
intinit()
{inti;
PCB*p;
clrscr();
really=(PCB*)malloc(sizeof(PCB));
really->next=NULL;
excute=(PCB*)malloc(sizeof(PCB));
excute->next=NULL;
wait=(PCB*)malloc(sizeof(PCB));
wait->next=NULL;
printf("_____________________________PROCESSSECHUDLE__________________________________\n");
printf("nowisiniting");
for(i=0;i<18;i++)
{
printf(".");
delay(10000000000);
}
printf("\ninputPIDandUIDasinteger,00asover\n");
while
(1)
{
p=(PCB*)malloc(sizeof(PCB));
p->next=NULL;
scanf("%d%d",&p->PID,&p->UID);
if(p->PID==0&&p->UID==0)
break;
else
{
if(enqueue(really,p))
{
printf("newprocessPID=%dUID=%dadded!
\n",p->PID,p->UID);
}
elsereturn0;
}
}
return1;
}/*init*/
/*运行一个process*/
intrun()
{
PCB*p=excute;
ints=SEC;
if(excute->next==NULL)
{
printf("noprocessinexcutequeue\n");
return0;
}
else
{
p=excute->next;
printf("systemwillsleep%dsasprocessrunning\n",s);
sleep(3);
printf("process:
PID=%dUID=%dexcutesuccessed..\n",p->PID,p->UID);
excute->next=p->next;
free(p);
}
}/*run*/
/*离开*/
intleave()
{
intCOMMAND;
printf("Areyousuretoleave?
YES-
(1),NO-
(2)\n");
scanf("%d",&COMMAND);
if(COMMAND==1)
{clrscr();
exit(0);
}
if(COMMAND==2)
{printf("\n");
help();
}
}
inthelp()
{
printf("__________________________HELPMENU____________________________\n");
printf("\t-hHELPshowhelpoption\n");
printf("\t-cCREATEcreateanewprocess,andputtoreallyqueue\n");
printf("\t-bBLOCKblockaprocessinexcutequeue\n");
printf("\t-wWAKEwakeaprocessinwaitqueue\n");
printf("\t-eEXCUTEexcuteaprocessinreallyqueue\n");
printf("\t-sSUSPENDsuspendaprocessinexcutequeue\n");
printf("\t-oOUTPUToutputallprocessesinqueues\n");
printf("\t-rRUNexcuteaprocessinexcutequeue\n");
printf("\t-xEXITexitthisprogram\n");
printf("_______________________________________________________________\n");
printf("\ttype'H'willshowthismenu\n");
}
intmain()
{
charCOMMAND=NULL;
inti;
intgdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"");
setbkcolor(LIGHTBLUE);
setcolor(21);
for(i=40;i<=600;i+=12)
{
rectangle(i,40,i+10,49);
rectangle(i,451,i+10,460);
}
for(i=40;i<=450;i+=12)
{
rectangle(40,i,49,i+10);
rectangle(601,i,610,i+10);
}
setcolor(19);
rectangle(150,100,480,410);
rectangle(145,95,475,405);
drawmat(han1S,16,200,230,BLUE);
drawmat(han2S,16,230,230,BLUE);
drawmat(han3S,16,260,230,BLUE);
drawmat(han4S,16,290,230,BLUE);
drawmat(han5S,16,320,230,BLUE);
drawmat(han6S,16,350,230,BLUE);
drawmat(han7S,16,380,230,BLUE);
drawmat(han8S,16,410,230,BLUE);
drawmat(han9S,16,220,300,BLUE);
drawmat(han10S,16,250,300,BLUE);
drawmat(han11S,16,280,300,BLUE);
drawmat(han12S,16,310,300,BLUE);
drawmat(han13S,16,340,300,BLUE);
drawmat(han14S,16,370,300,BLUE);
getch();
closegraph();
if(init()!
=1)
{
printf("initfalied!
\n");
getch();
exit(0);
}
else
{
printf("init...OK\n");
output();
help();
}
while
(1)
{
/*当三队列都不空执行调度*/
printf(">");
scanf("%c",&COMMAND);
switch(COMMAND)
{
case'\n':
break;
case'H':
case'h':
help();break;
case'C':
case'c':
create();break;
case'B':
case'b':
block();break;
case'W':
case'w':
wake();break;
case'E':
case'e':
fexcute();break;
case'O':
case'o':
output();break;
case'X':
case'x':
leave();break;
case'R':
case'r':
run();break;
}
}
}_