📄 main.c
字号:
#include<stdio.h>
#include<dos.h>
#include<graphics.h>
#define true 1
#define false 0
#define TIME 5
#define ALT_A 0x1E00
#define ALT_X 0x2D00
#define CTRL_A 0x1E01
#define ALT_R 0x1300
#define ALT_S 0x1F00
#define ALT_U 0x1600
#define WIDE 110
#define HEIGHT 50
#define STARTX 30
#define STARTY_FOR_RESERVE 30
#define STARTY_FOR_READY 90
#define STARTY_FOR_SUSPEND 150
#define STARTY_FOR_FINISH 210
typedef struct {
int pid;
int prior;
int time_total;
int time_rest;
/*int has_data;*/
}pid_t;
/* The following queue : first data is just to store the number*/
/*// of data that this queue has(of course not including the zero node ;)*/
/* In fact , only the prior area is used;*/
pid_t reserve[41];
pid_t jam[6];
pid_t ready[6];
pid_t suspend[6];/* when over 5,the old process will be delete;*/
pid_t finish[41];
pid_t run ;
pid_t empty;
void initg();
int main()
{
int j=0;
to_init();/* to make all the queue inited */
to_add();/* to add some process to the reserve queue*/
/*to_test(reserve);*/
init_bf_run();/*init all the queue after adding process*/
/*to_test(ready); */
initg();
to_run();
getch();
system("&pause");
return 0;
}
/* to test*/
int to_test(pid_t *p)
{
int i = 0;
for(i=1; i<=p[0].prior; i++)
{
/*run = reserve[i];*/
printf("%d ", p[i].pid);
printf("%d ", p[i].prior);
printf("%d ", p[i].time_total);
printf("%d \n", p[i].time_rest);
/*printf("\n \n");*/
}
return 0;
}
/*to init all queues*/
int to_init()
{
int i = 0 ;
pid_t temp = {0,0,0,0};
for(i=0; i<=41; i++)
{
reserve[i] = temp ;
finish[i] = temp;
}
for(i=0; i<=6; i++)
{
jam[i] = temp;
ready[i] = temp;
suspend[i] = temp;
}
run = temp ;
empty = temp;
return true ;
}
/*to add*/
int to_add()
{
int num = 0;
int i = 0 ;
pid_t temp={0,0,0,0};
printf("Input the number of process you want to add: \n");
scanf("%d",&num);
for(i=1; i<=num; i++)
{
printf("\nInput the pid: ");
scanf("%d",&(temp.pid));
printf("\nInput the priority ");
scanf("%d",&(temp.prior));
printf("\nInput the total time:");
scanf("%d",&(temp.time_total));
temp.time_rest = temp.time_total;
addpro_to_que(reserve, 40, temp);
}
}
/* this process is to add a process into a queue;*/
int addpro_to_que(pid_t *queue, int maxsize, pid_t aprocess)
{
int i = 0;
int j = 0;
int flag = false;
pid_t *p = &aprocess ;
if(queue[0].prior == 0)
{
queue[1] = *p;
queue[0].prior++;
}
else
{
if(queue[0].prior>= maxsize)
{
return 1 ;
}
for(i=1; i<=queue[0].prior; i++)
{
if(p->prior > queue[i].prior)
{
for(j=(queue[0].prior)++; j>=i;j--)
{
queue[j+1] = queue[j];
}
queue[i] = *p;
/*queue[0].prior++;*/
flag = true;
break ;
}
}
if(flag==false)
queue[++(queue[0].prior)]=*p;
}
return 0;
}
/* get a max priority process*/
int getmax(pid_t *queue, pid_t *value)
{
int i = 0;
if(queue[0].prior > 0)
{
*value = queue[1];
{
}
for(i=1;i<queue[0].prior; i++)
{
queue[i] = queue[i+1];
}
queue[0].prior--;
}
else
{
return -1;
}
return 0;
}
int cmp(void *a,void *b)
{
return(*(pid_t *)a).prior>(*(pid_t *)b).prior? -1:1 ;
}
/* to init all the queue to a ready state*/
int init_bf_run()
{
int i=0;
int j=0;
int return_flag = 1;
pid_t temp={0,0,0,0};
if(reserve[0].prior==0)
return 1;
/*getmax(reserve, &run);*/
for(i=1; (1<=reserve[0].prior) && (ready[0].prior < 5); i++)
{
return_flag=getmax(reserve, &temp);
if(return_flag==0)
{
addpro_to_que(ready, 5, temp);
/*//printf("%d\n",reserve[0].prior); */
/*//to_test(reserve); */
/* //to_test(ready); */
}
}
return 0;
}
/*This fuction is to move a process from*/
int to_run(void)
{
int i = 0;
int haskey = false;
int key = 0;
int time_piece = 0;
int first_time_to_set = 1;
setcolor(RED);
setbkcolor(BLUE);
setlinestyle(SOLID_LINE, 0, THICK_WIDTH);
while(true)
{
int returnflag = 0;
update(finish, STARTX, STARTY_FOR_FINISH);
init_bf_run();
if((returnflag=getmax(ready, &run))==-1)
{
printf("all task is over!");
break;
}
setviewport(0, 0, 800,STARTY_FOR_FINISH+60,0);
clearviewport();
setviewport(0,0,getmaxx(),getmaxy(),1);
draw_frame();
update(reserve, STARTX, STARTY_FOR_RESERVE);
update(ready, STARTX, STARTY_FOR_READY);
update(suspend, STARTX, STARTY_FOR_SUSPEND);
update(finish, STARTX, STARTY_FOR_FINISH);
time_piece=TIME;
do{
sleep(1);
run.time_rest -= 1;
setviewport(100,300,100+WIDE, 300+HEIGHT,0);
clearviewport();
draw_process(0, 0, run);
setviewport(0,0,getmaxx(),getmaxy(),1);
if(run.time_rest==0)
{
/* printf("task %d is over\n",run.pid);*/
addpro_to_que(finish, 40, run);
break;
}
if(--time_piece==0)
{
if(run.prior>ready[ready[0].prior].prior)
{
if(ready[0].prior==5)
{
addpro_to_que(reserve, 40, ready[5]);
}
addpro_to_que(ready, 5, run);
}
else
{
addpro_to_que(reserve, 40,run);
}
break;
}
}
while((haskey=kbhit(1)) == false);
if(haskey)
{
key = kbhit(0);
switch(key)
{
case ALT_A:
case CTRL_A:
case ALT_R:
case ALT_S:
case ALT_U:
case ALT_X: ;
} /* END OF SWITCH*/
} /* END OF IF */
}/*END OF outside WHILE */
return 0;
}
/*To init the graphic*/
void initg()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc");
cleardevice();
}
int draw_frame()
{
settextstyle(DEFAULT_FONT, VERT_DIR, 1);
settextjustify(CENTER_TEXT, CENTER_TEXT);
outtextxy(15, 60, "reserve");
outtextxy(15,120, "ready");
outtextxy(15,180,"suspend");
outtextxy(15,240,"finish");
return true;
}
int draw_process(int x, int y, pid_t process)
{
char* s=NULL;
int ax,ay;
rectangle(x, y, x+WIDE, y+HEIGHT/2);
rectangle(x, y+HEIGHT/2, x+WIDE, y+HEIGHT);
sprintf(s, "PID:%d T:%d R:%d",process.pid, process.time_total, process.time_rest);
settextstyle(DEFAULT_FONT, HORIZ_DIR, 1);
settextjustify(LEFT_TEXT, BOTTOM_TEXT);
outtextxy(x+1, y+HEIGHT/4, s);
{
ax = x + WIDE*process.time_rest/(2*process.time_total);
ay = y + 3*HEIGHT/4; /*GET A POINT FROM THE FLOOD AREA*/
}
line(x+WIDE*process.time_rest/process.time_total, y+HEIGHT/2, x+WIDE*process.time_rest/process.time_total , y+HEIGHT);
floodfill(ax, ay, RED);
return true;
}
int update(pid_t *p,int x, int y)
{
int i=0;
for(i=1; (i<=p[0].prior) && (i<=5); i++)
{
draw_process(x, y, p[i]);
x=x+WIDE;
}
return true;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -