📄 myos1.c
字号:
/* Note:Your choice is C IDE */
#include "stdio.h"
#include<conio.h>
#include"stdlib.h"
#define UP 'w'
#define DOWN 's'
int menu=1;
char str[6][18]={"CREATE PROCESS","HangUp PROCESS","ACTIVATE PROCESS",
"SHOW PROCESS", "RUN PROCESS","EXIT "};
typedef struct pcb
{
char name[10];
int needtime;
int priority;
int alreadytime;
char *state;
struct pcb* next;
}PCB;
PCB *Hangup=NULL;
PCB *Ready=NULL;
int ProNum=0;
void windows()
{
int n;
window(1,1,80,25);
clrscr();
gotoxy(8,3);
textcolor(WHITE);
cputs("* * * * * * *WELCOME TO USE PROCESSOR ATTEMPER PROGRAM* * * * * * * ");
for(n=3;n<25;n++)
{gotoxy(8,n);
cprintf("*");
gotoxy(75,n);
cprintf("*");
}
gotoxy(9,24);
cputs("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ");
gotoxy(35,9);
textbackground(BLUE);
textcolor(YELLOW);
cputs("CREATE PROCESS");
gotoxy(35,10);
textbackground(BLUE);
textcolor(YELLOW);
cputs("HangUp PROCESS");
gotoxy(35,11);
textbackground(BLUE);
textcolor(YELLOW);
cputs("ACTIVATE PROCESS");
gotoxy(35,12);
textbackground(BLUE);
textcolor(YELLOW);
cputs("SHOW PROCESS");
gotoxy(35,13);
textbackground(BLUE);
textcolor(YELLOW);
cputs("RUN PROCESS");
gotoxy(35,14);
textbackground(BLUE);
textcolor(YELLOW);
cputs("EXIT ");
}
void Menu()
{
char ch;
windows();
while(1)
{
ch=getch();
/*cputs(ch);
scanf("%c",&ch);*/
if(ch==UP)
{
if(menu==1)
{
menu=6;
gotoxy(35,9);
textcolor(YELLOW);
cputs(str[0]);
gotoxy(35,14);
textcolor(WHITE);
cputs(str[5]);
}
else
{
menu--;
gotoxy(35,9+menu);
textcolor(YELLOW);
cputs(str[menu]);
gotoxy(35,8+menu);
textcolor(WHITE);
cputs(str[menu-1]);
}
}
else if(ch==DOWN)
{
if(menu==6)
{
menu=1;
gotoxy(35,14);
textcolor(YELLOW);
cputs(str[5]);
gotoxy(35,9);
textcolor(WHITE);
cputs(str[0]);
}
else
{
menu++;
gotoxy(35,7+menu);
textcolor(YELLOW);
cputs(str[menu-2]);
gotoxy(35,8+menu);
textcolor(WHITE);
cputs(str[menu-1]);
}
}
else
break;
}
}
void windows1()
{
int n;
window(1,1,80,25);
clrscr();
gotoxy(8,3);
textcolor(WHITE);
cputs("* * * * * * *WELCOME TO USE PROCESSOR ATTEMPER PROGRAM* * * * * * * ");
for(n=3;n<25;n++)
{gotoxy(8,n);
cprintf("*");
gotoxy(75,n);
cprintf("*");
}
gotoxy(9,24);
cputs("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ");
}
PCB * Insert(PCB *p,PCB *q)
{
PCB *p1;
PCB *p0,* p2;
p1=p;
p0=q;
if(p==NULL)
{
p=p0;p0->next=NULL;
}
else
{
while((p0->priority<p1->priority)&&p1!=NULL)
{p2=p1;p1=p1->next;}
if(p==p1)
{
p0->next=p;
p=p0;
}
else
{
p2->next=p0;
p0->next=p1;
}
}
return(p);
}
void display(PCB *q,int k)
{
PCB *p;
windows1();
if(q==NULL)
{
gotoxy(20,5);
cputs("The list is null!");
getch();
return;
}
else
{
textcolor(RED);
if(k==0)
{
gotoxy(40,5);
cputs("Run Queue");
}
if(k==2)
{
gotoxy(33,5);
cputs("HangUp Queue");
}
if(k==1)
{
gotoxy(35,5);
cputs("Ready Queue");
}
p=q;
gotoxy(15,6);
cputs(" Name Priority NeedTime RunTime State");
textcolor(WHITE);
do
{
printf("\n\t\t%s %d %d %d %s",p->name,p->priority,p->needtime,p->alreadytime,p->state);
textcolor(WHITE);
p=p->next;
}while(p!=NULL);
}
return;
}
void CreateProcess()
{
int i,n;
PCB *p=NULL;
windows1();
gotoxy(20,9);
cputs("Input the process number:");
scanf("%d",&n);
ProNum+=n;
for(i=0;i<n;i++)
{
p=(PCB*)malloc(sizeof(PCB));
windows1();
gotoxy(10,9);
printf("Input the %dth ProcessName:",i+1+ProNum-n);
scanf( "%s",p->name);
p->needtime=3+random(15);
p->alreadytime=0;
p->priority=random(10)+1;
p->state="Ready";
p->next=NULL;
Ready=Insert(Ready,p);
/*p=NULL;*/
}
display(Ready,1);
getch();
}
void HangUpProcess()
{
PCB *p1,*temp,*p2;
char *s=NULL;
int flag=0;
windows1();
gotoxy(20,9);
textcolor(WHITE);
cputs("Input ProcessName to hangup:");
scanf("%s",s);
if(Ready==NULL)
{
gotoxy(20,10);
cputs("\n\tLIst is null!");
getch();
return;
}
else
{
p1=Ready;
while(p1!=NULL&&strcmp(s,p1->name)!=0)
{p2=p1;p1=p1->next;}
if(strcmp(s,p1->name)==0)
{
if(p1==Ready)
Ready=Ready->next;
else
{
p2->next=p1->next;
}
p1->next=NULL;
p1->state="Hangup";
flag=1;
if(Hangup==NULL) Hangup=p1;
else Hangup=Insert(Hangup,p1);
display(Hangup,2);
getch();
}
}
if(flag==0)
{
cputs("\n\tDon't have this process!");
getch();
}
}
void unhang()
{
PCB *p1,*p2;
char *s=NULL;
int flag=0;
windows1();
gotoxy(20,9);
cputs("Input the Process's name to unhangup:");
scanf("%s",s);
if(Hangup==NULL)
{
cputs("\n\tList is null!");
getch();
return;
}
else
{
p1=Hangup;
while(p1!=NULL&&strcmp(s,p1->name)!=0)
{p2=p1;p1=p1->next;}
if(strcmp(s,p1->name)==0)
{
if(p1==Hangup)
Hangup=p1->next;
else
{
p2->next=p1->next;
}
p1->next=NULL;
p1->state="Ready";
flag=1;
if(Ready==NULL) Ready=p1;
else Ready=Insert(Ready,p1);
display(Ready,1);getch();
}
}
if(flag==0)
{
cputs("\n\tDon't have this process!");
}
}
void RunProcess()
{
PCB *p,*p1;
char ch='y';
p=Ready;
display(Ready,1);getch();
while(p!=NULL&&(p->priority!=0||Ready->next==NULL)&&ch=='y')
{
ch='n';
p=Ready;
p->priority--;
if(Ready->next==NULL) p->priority=0;
p->needtime--;
p->alreadytime++;
p1=p->next;
while(p1)
{
p1->priority++;
p1=p1->next;
}
Ready=Ready->next;
p->next=NULL;
p->state="Run";
if(p->needtime<=0)
{
ProNum--;
free(p);
}
else
{
Ready=Insert(Ready,p);
}
display(Ready,0);
printf("\n\n\t\t Continue?(y/n):");
scanf("%c",&ch);
}
}
void Show()
{
int d;
windows1();
gotoxy(10,9);
cputs("Input the choice:1-ReadyProcess,2-HangUpProcess:");
scanf("%d",&d);
if(d==1)
display(Ready,1);
if(d==2)
display(Hangup,2);
getch();
}
void main()
{
while(1)
{
Menu();
switch(menu)
{
case 1:CreateProcess();break;
case 2:HangUpProcess();break;
case 3:unhang();break;
case 4:Show();break;
case 5:RunProcess();break;
case 6:exit(0);
/*default:break;*/
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -