
正文
作业车间调度java代码,车间生产调度员
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
java里thread怎么实现定时调度
java Thread类实现定时调度,可以延迟几秒之后再执行,代码如下:
public class ceshi {
public static void main(String[] args) throws Exception {
// run in a second
final long timeInterval = 1000;
Runnable runnable = new Runnable() {
@Override
public void run() {
while (true) {
// ------- code for task to run
System.out.println("Hello !!");
// ------- ends here
try {
Thread.sleep(timeInterval);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
Thread thread = new Thread(runnable);//线程创建
thread.start();//线程启动
}
}
运行结果:
相关问答
Q1: 编写代码实现作业的三种调度算法
#includewindows.h
#includeiostream
#includestdio.h
#includestring
using namespace std;
const int maxnum=100;
int N; /*进程数*/
double start[maxnum],endtime[maxnum],arrive[maxnum],runtime[maxnum],zhou[maxnum];
double averagezhou; // 平均周转时间
double average_zhou; //平均带权周转时间
char name; //进程名
double dqzhou[maxnum]; //带权周转时间
typedef struct node
{
char name[10]; //进程名
int round; //进程时间轮转时间片
int cputime; //进程占用CPU时间
int needtime; //进程到完成还要的时间
char state; //进程的状态
struct node *next; //链指针
}PCB;
PCB *finish,*ready,*tail,*run; /*队列指针*/
void firstin() /*将就绪队列中的第一个进程投入运行*/
{
run=ready; /*就绪队列头指针赋值给运行头指针*/
run-state='R'; /*进程状态变为运行态*/
ready=ready-next; /*就绪对列头指针后移到下一进程*/
}
void print1(PCB *q) /*进程PCB输出*/
{
printf("进程名 已运行时间 还需要时间 时间片 状态\n");
printf(" %-10s%-15d%-10d%-10d %-c\n",q-name,q-cputime,q-needtime,q-round,q-state);
}
void print() /*输出函数*/
{
PCB *p;
if(run!=NULL) /*如果运行指针不空*/
print1(run); /*输出当前正在运行的PCB*/
p=ready; /*输出就绪队列PCB*/
while(p!=NULL)
{
print1(p);
p=p-next;
}
p=finish; /*输出完成队列的PCB*/
while(p!=NULL)
{
print1(p);
p=p-next;
}
}
void insert(PCB *p2) //轮转法插入函数
{
tail-next=p2; //将新的PCB插入在当前就绪队列的尾
tail=p2;
p2-next=NULL;
}
void create() /*创建进程PCB*/
{
PCB *p;
int i,time;
char na[10];
ready=NULL;
finish=NULL;
run=NULL;
printf("请输入进程名称和所需要CPU的时间:\n");
for(i=1;i=N;i++)
{
p=(PCB *)malloc(sizeof(PCB));
scanf("%s",na);
scanf("%d",time);
strcpy(p-name,na);
p-cputime=0;
p-needtime=time;
if(i==1)
p-state='R';
else
p-state='W';
p-round=1; /*时间片*/
if(ready!=NULL)
insert(p);
else
{
p-next=ready;
ready=p;
tail=p;
}
}
printf("------------------------------------------------------------------\n");
print(); /*输出进程PCB信息*/
run=ready; /*将就绪队列的第一个进程投入运行*/
ready=ready-next;
run-state='R';
}
void RR() //时间片轮转调度
{
while(run!=NULL)
{
run-cputime=run-cputime+1;
run-needtime=run-needtime-1;
if(run-needtime==0) /*运行完将其变为完成态,插入完成队列*/
{
run-next=finish;
finish=run;
run-state='F';
run=NULL;
if(ready!=NULL)
firstin(); /*就绪对列不空,将第一个进程投入运行*/
}
else
if(ready!=NULL) /*如就绪队列不空*/
{
run-state='W'; /*将进程插入到就绪队列中等待轮转*/
insert(run);
firstin(); /*将就绪对列的第一个进程投入运行*/
}
printf("------------------------------------------------------------------\n");
print(); /*输出进程信息*/
}
}
void FCFS(double *arrive,double *runtime,double n) //先来先服务调度算法
{
start[0]=arrive[0]; //开始执行时间=到达时间
endtime[0]=start[0]+runtime[0]; //完成时间=开始时间+服务时间
zhou[0]=endtime[0]-arrive[0]; //周转时间=完成时间-到达时间
dqzhou[0]=zhou[0]/runtime[0];
for(int i=0;in;i++)
{
if(endtime[i-1]arrive[i]||endtime[i-1]==arrive[i])
endtime[i]=endtime[i-1]+runtime[i];
else
endtime[i]=arrive[i]+runtime[i];
zhou[i]=endtime[i]-arrive[i];
dqzhou[i]=zhou[i]/runtime[i];
averagezhou+=zhou[i];
average_zhou+=dqzhou[i];
}
averagezhou=averagezhou/n;
average_zhou=average_zhou/n;
cout"完成时间为:"endl;
for(int j=0;jn;j++)
coutendtime[j]" "endl;
cout"周转时间为:"endl;
for(int k=0;kn;k++)
coutzhou[k]" "endl;
cout"带权周转时间为:"endl;
for(int m=0;mn;m++)
coutdqzhou[m]" "endl;
cout"平均周转时间为:"endl;
coutaveragezhou" "endl;
cout"平均带权周转时间为:"endl;
coutaverage_zhou" "endl;
}
void SJF(double *arrive,double *runtime,double n) //短作业优先调度算法
{
int end[maxnum]; //用于标记进程是否已经执行,应经执行end[i]=1,否则为0;
for(int k=0;kn;k++)
end[k]=0;
int temp,now=0,next=1,p=1; //now表示刚执行完的进程号,next表示下一个要执行的进程号
start[0]=arrive[0]; //开始执行时间=到达时间
endtime[0]=start[0]+runtime[0]; //完成时间=开始时间+服务时间
zhou[0]=endtime[0]-arrive[0]; //周转时间=完成时间-到达时间
dqzhou[0]=zhou[0]/runtime[0]; //带权周转时间=周转时间/服务时间
averagezhou=zhou[0];
average_zhou=dqzhou[0];
end[now]=1; //执行完的进程设置为1;
for(int i=1;in;i++)
{
int j;
for(j=1;jn;)
{
if(arrive[j]endtime[now]||arrive[j]==endtime[now])
j++;
else
break;
}
temp=j;
int min=p;
for(j=1;j=temp;j++)
{
if(runtime[min]runtime[j] end[j]==0)
min=j;
}
next=min;
endtime[next]=endtime[now]+runtime[next];
zhou[next]=endtime[next]-arrive[next];
dqzhou[next]=zhou[next]/runtime[next];
averagezhou+=zhou[next];
average_zhou+=dqzhou[next];
end[next]=1;
now=next;
next=p;
while(end[p]!=0)
p++;
}
averagezhou=averagezhou/n;
average_zhou=average_zhou/n;
cout"完成时间为:"endl;
for(int j=0;jn;j++)
coutendtime[j]" "endl;
cout"周转时间为:"endl;
for(k=0;kn;k++)
coutzhou[k]" "endl;
cout"带权周转时间为:"endl;
for(int m=0;mn;m++)
coutdqzhou[m]" "endl;
cout"平均周转时间为:"endl;
coutaveragezhou" "endl;
cout"平均带权周转时间为:"endl;
coutaverage_zhou" "endl;
}
int EDF() //最早截止时间的调度算法
{
int arrive_A,arrive_B; //标记进程A,进程B的到达时间
int zhouqi_A,zhouqi_B,serve_A,serve_B; //进程的周期时间和服务时间
int i,j,a=0,b=0,ka=0,kb=0; //ka,kb为开关,i,j,a,b为进程下标
int num_a=0,num_b=0; //服务累计时间
printf("输入进程A的周期时间,服务时间: ");
scanf("%d%d",zhouqi_A,serve_A);
printf("输入进程B的周期时间,服务时间: ");
scanf("%d%d",zhouqi_B,serve_B);
for(int T=0;T=100;T++)
{
if(num_a==serve_A) //进程A完成
{
num_a=serve_A+1;
printf("当T=%d时",T);
printf("进程A%d结束\n",a);
if(num_bserve_B)
{
printf(" 调用进程B%d\n",b);
kb=1;
}
ka=0;
}
if(num_b==serve_B)
{
num_b=serve_B+1;
printf("当T=%d时",T);
printf("进程B%d结束\n",b);
if(num_aserve_A)
{
printf(" 调用进程A%d\n",a);
ka=1;
}
kb=0;
}
if(T%zhouqi_A==0 T%zhouqi_B==0)
{
arrive_A=arrive_B=T;
j=++a;
i=++b;
printf("当T=%d时,进程A%d和进程B%d同时产生,此时,",T,j,i);
if(zhouqi_A=zhouqi_B)
{
printf("调用进程A%d,阻塞进程B%d\n",j,i);
ka=1;
kb=0;
}
else
{
printf("调用进程B%d,阻塞进程A%d\n",i,j);
ka=0;
kb=1;
}
num_a=num_b=0;
}
if(T%zhouqi_A==0T%zhouqi_B!=0)
{
arrive_A=T;
printf("当T=%d时",T);
printf("进程A%d产生 ",++a); //不可能与进程A竞争处理器
num_a=0;
if(num_bserve_B) //进程B没有完成
if(arrive_B+zhouqi_Barrive_A+zhouqi_A) //若进程B最早截止时间大于进程A的,则执行进程A
{
printf("进程A%d执行。\n",a);
ka=1;
kb=0;
}
else //若进程B最早截止时间小于等于进程A的
printf("进程B%d继续执行。\n",b);
else //进程B完成
{
printf("进程A%d执行。\n",a);
ka=1;
}
}
if(T%zhouqi_A!=0 T%zhouqi_B==0)
{
arrive_B=T;
printf("当T=%d时",T);
printf("进程B%d产生,",++b); //不可能与进程B竞争处理器
num_b=0;
if(num_aserve_A) //进程A没有完成
if(arrive_B+zhouqi_B=arrive_A+zhouqi_A) //进程A的最早截止时间不小于B
printf("进程A%d继续执行。\n",a);
else
{
printf("进程B%d执行。\n",b);
kb=1;
ka=0;
}
else //进程A完成
{
printf("进程B%d执行。\n",b);
kb=1;
}
}
if(ka)
num_a++;
if(kb)
num_b++;
}
return 1;
}
int main()
{
system("color 0b"); //设置颜色
cout"最早截止时间的调度算法如下: "endlendl;
EDF();
int n;
coutendl;
cout"请输入进程的数目: ";
cinn;
cout"请按进程到达时间从小到大依次输入n个进程的到达时间: "endl;
for(int i=0;in;i++)
cinarrive[i];
cout"请按上面进程的顺序依次输入n个进程的服务时间: "endl;
for(int j=0;jn;j++)
cinruntime[j];
cout"先来先服务调度算法如下: "endl;
FCFS(arrive,runtime,n);
coutendlendl;
cout"短作业优先调度算法如下: "endl;
SJF(arrive,runtime,n);
coutendlendl;
printf("轮转调度算法如下:\n\n");
printf("输入创建进程的数目:\n");
scanf("%d",N);
create();
RR();
return 0;
}
Q2: java的几种定时任务
java定时任务有三种:
- JDK自带 :JDK自带的Timer以及JDK1.5+ 新增的ScheduledExecutorService;
- Quartz :简单却强大的JAVA作业调度框架
- Spring3.0以后自带的task :可以将它看成一个轻量级的Quartz,而且使用起来比Quartz简单许多;
代码参考:
JDK 自带的定时器实现
schedule(TimerTask task, Date time) 特定时间执行
public static void main(String[] args) {
for (int i = 0; i 10; ++i) {
new Timer("timer - " + i).schedule(new TimerTask() {
@Override
public void run() {
println(Thread.currentThread().getName() + " run ");
}
}, new Date(System.currentTimeMillis() + 2000));
}
}
Quartz 定时器实现
2.1 通过maven引入依赖(这里主要介绍2.3.0) 注意:shiro-scheduler中依赖的是1.x版本 如果同时使用会冲突
!-- --
dependency
groupIdorg.quartz-scheduler/groupId
artifactIdquartz/artifactId
version2.3.0/version
/dependency
2.2 创建Job类
public class TestJob implements Job{
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
println(Thread.currentThread().getName() + " test job begin " + DateUtil.getCurrentTimeStr());
}
}
2.3 调度任务
public static void main(String[] args) throws InterruptedException, SchedulerException {
Scheduler scheduler = new StdSchedulerFactory().getScheduler();
// 开始
scheduler.start();
// job 唯一标识 test.test-1
JobKey jobKey = new JobKey("test" , "test-1");
JobDetail jobDetail = JobBuilder.newJob(TestJob.class).withIdentity(jobKey).build();
Trigger trigger = TriggerBuilder.newTrigger() .withIdentity("test" , "test")
// 延迟一秒执行
.startAt(new Date(System.currentTimeMillis() + 1000))
// 每隔一秒执行 并一直重复
.withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(1).repeatForever())
.build();
scheduler.scheduleJob(jobDetail , trigger);
Thread.sleep(5000);
// 删除job
scheduler.deleteJob(jobKey);
}
3.Spring 相关的任务调度
3.1 配置文件实现
spring-schedule.xml
task:scheduler id="myScheduler" pool-size="10" /
task:scheduled-tasks scheduler="myScheduler"
task:scheduled ref="job" method="test" cron="0 * * * * ?"/
/task:scheduled-tasks
3.2注解实现
spring-schedule.xml
task:scheduler id="myScheduler" pool-size="10" /
// 启用注解
task:annotation-driven scheduler="myScheduler"/
@Component
public class Task{
@Scheduled(cron="0/5 * * * * ? ") //每5秒执行一次
public void execute(){
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(DateTime.now().toDate())+"*********B任务每5秒执行一次进入测试");
}
}
Q3: 优先级调度算法如何用JAVA实现
没java的 发段源代码给你 有兴趣自己慢慢理解
#include time.h
#include dos.h
#include math.h
#include conio.h
#include stdio.h
#include stdlib.h
#include time.h
#include graphics.h
#define ESC 0x1b
#define ENTER 0x0d
#define TRUE 1
#define FALSE 0
/*每隔TIME秒就变换一次优先级*/
#define TIME 5
/*数据结构*/
/****************************************************************/
enum _Status/*进程状态枚举*/
{
READY =0,/*就绪*/
RUN,/*执行中*/
SUSPEND,/*挂起*/
};
typedef enum _Status Status;
/****************************************************************/
struct _Pcb/*进程结构*/
{
int PID;/*进程ID,ID为负数的进程为系统后备队列的作业*/
int Time;/*进程运行需要的时间*/
int Prior;/*进程的优先级,越大越优先*/
Status Sts;/*状态*/
struct _Pcb *Next;/*指向本进程队列中下个进程的PCB*/
};
typedef struct _Pcb PCB;
/****************************************************************/
struct _Batch/*多道处理中的道结构*/
{
PCB *pcb;/*该道当前正在处理的进程*/
struct _Batch *Next;/*下一道*/
};
typedef struct _Batch Batch;
/****************************************************************/
/*多道系统相关全局变量*/
PCB *ProcQueue = NULL;/*进程链表,按优先级从大到小排列*/
Batch *BatchQueue = NULL;/*系统多道链表*/
/****************************************************************/
/*动态优先权抢占式调度算法及相关函数声明*/
/****************************************************************/
int InitBatchs(int n);/*初始化多道系统,n为道数*/
int InsertProc(int prior, int time);/*向进程链表中按优先级大小插入一个新进程*/
int InsertIDLE();/*向进程队列中加入后备进程,当系统空闲时将被调入*/
int SortProcQueue();/*将进程链表按优先级从大到小排列*/
int AddBatch();/*增加系统道数*/
int DeleteBatch();/*减少系统道数*/
int UnSuspendProc(int id);/*解除ID为id的进程的挂起状态*/
int UpdateBatchs();/*多道系统根据进程链表进行更新,并将执行完毕的进程删除*/
int PassSeconds(int n);/*系统经过n秒后计算数据并进行优先级调度*/
/****************************************************************/
/*各函数的定义*/
/****************************************************************/
int InitBatchs(int n)
{
int i;
for (i=0; in; ++i)
{
AddBatch();
}
return (UpdateBatchs());
}
int InsertProc(int prior, int time)
{
static int sysid = 0;/*该系统已经加入过多少进程,此值将是新进程的ID*/
PCB *last,*now,*pcb;
pcb = (PCB*)malloc(sizeof(PCB));
if (pcb == NULL) return FALSE;
pcb-Prior = prior;
pcb-Time = time;
pcb-PID = (++sysid);
pcb-Sts = READY;
if (ProcQueue == NULL)/*如果进程队列为空*/
{
ProcQueue = pcb;
pcb-Next = NULL;
return TRUE;
}
last = ProcQueue;
now = last-Next;
if (pcb-Prior last-Prior)/*pcb将排在队头*/
{
pcb-Next = ProcQueue;
ProcQueue = pcb;
return TRUE;
}
while ((now != NULL) (pcb-Prior now-Prior))/*寻找插入位置*/
{
last = now;
now = last-Next;
}
last-Next = pcb;
pcb-Next = now;
return TRUE;
}
int InsertIDLE()
{
PCB *now = ProcQueue;
PCB *idle = (PCB*)malloc(sizeof(PCB));
if (idle == NULL) return FALSE;
idle-PID = -1;
idle-Prior = -1;
idle-Sts = SUSPEND;
idle-Time = -1;
idle-Next = NULL;
if (ProcQueue == NULL)
{
ProcQueue = idle;
return TRUE;
}
while(now-Next != NULL)
{
now = now-Next;
}
now-Next = idle;
return TRUE;
}
int SortProcQueue()
{ /*冒泡排序*/
PCB *last, *now;
int b = FALSE;/*上次遍历是否无交换产生*/
if (ProcQueue==NULL || ProcQueue-Next==NULL)/*如果链表中无进程或只有一个进程*/
return FALSE;
while (!b)
{
b = TRUE;
last=ProcQueue;
now=last-Next;
if (last-Prior now-Prior)
{
ProcQueue = now;
last-Next = now-Next;
now-Next = last;
b = FALSE;
last = ProcQueue;
now = last-Next;
}
while (now-Next!=NULL)
{
if ((now-Prior)(now-Next-Prior))
{
last-Next = now-Next;
now-Next = now-Next-Next;
last-Next-Next = now;
b = FALSE;
}
else
last = last-Next;
now = last-Next;
}
}
return TRUE;
}
int AddBatch()
{
Batch *bt = (Batch*)malloc(sizeof(Batch));
if (bt==NULL) return FALSE;
bt-Next = BatchQueue;
BatchQueue = bt;
bt-pcb = NULL;
return (InsertIDLE());
}
int DeleteBatch()
{
Batch *bt = BatchQueue;
PCB *last, *now;
if (BatchQueue==NULL || BatchQueue-Next==NULL)/*如果只剩最后一道则不删除*/
return FALSE;
if (ProcQueue==NULL || ProcQueue-Next==NULL)/*如果只有最后一个后备空闲进程*/
return FALSE;/**/
last = ProcQueue;
now = last-Next;
while (now-Next != NULL)/*查找到最后一个进程,该进程必定是后备空闲进程*/
{
last = now;
now = last-Next;
}
if (now==NULL || now-PID=0)/*未查找到后备进程*/
return FALSE;/**/
free(now);
last-Next = NULL;
BatchQueue = BatchQueue-Next;
free(bt);
return TRUE;
}
int UnSuspendProc(int id)
{
PCB *now = ProcQueue;
if (ProcQueue==NULL) return FALSE;
while (now != NULL)
{
if (now-PID == id)
{
now-Sts = READY;
return TRUE;
}
}
return FALSE;
}
int UpdateBatchs()
{
Batch *bt = BatchQueue;
PCB *last = ProcQueue, *now;
while (bt != NULL)
{
bt-pcb = NULL;
bt = bt-Next;
}
if (ProcQueue == NULL) return TRUE;
while (last-Sts==RUN last-PID=0 last-Time=0)
{
ProcQueue = ProcQueue-Next;
free(last);
last = ProcQueue;
}
now = last-Next;
while (now != NULL)
{
if (now-Sts==RUN now-PID=0 now-Time=0)/*如果该进程是运行中的一般进程并已执行完毕*/
{
last-Next = now-Next;
free(now);
}
else
last = last-Next;
now = last-Next;
}
bt = BatchQueue;
now = ProcQueue;
while (bt != NULL now != NULL)
{
bt-pcb = now;
now-Sts = RUN;
bt = bt-Next;
now = now-Next;
}
while (now != NULL)
{
if (now-Sts == RUN)
{
now-Sts = SUSPEND;
}
now = now-Next;
}
return TRUE;
}
int PassSeconds(int n)
{
static int time = 0;
int i=0, ProcEnd = FALSE;
PCB *pcb = ProcQueue;
Batch *bt = BatchQueue;
if (bt == NULL) return FALSE;
time += n;
if (time=TIME)
{
i = time/TIME;/*经过多少时间段*/
time = time%TIME;
}
while (bt != NULL)/*更新进程运行时间*/
{
if (bt-pcb-PID=0)
{
bt-pcb-Time -= n;
if (bt-pcb-Time = 0)/*进程结束*/
{
ProcEnd = TRUE;
}
}
bt = bt-Next;
}
if (i 0)
{
while (pcb != NULL)/*更新进程优先权(动态优先权)*/
{
if (pcb-Sts == RUN pcb-PID=0)/*运行的进程优先权降低*/
{
pcb-Prior -= i;
if (pcb-Prior 0)
pcb-Prior = 0;
}
else if (pcb-Sts == SUSPEND pcb-PID=0)/*挂起的进程优先权升高*/
pcb-Prior += i;
pcb = pcb-Next;
}
}
if (i0)
SortProcQueue();/*如果优先级有变动则重新排序*/
if (ProcEnd || i0)
{
UpdateBatchs();/*更新多道进程*/
}
return TRUE;
}
/****************************************************************/
/*图形界面相关函数*/
/****************************************************************/
/*表格的单位宽度和高度*/
#define WIDTH 64
#define HEIGHT 12
void *black=NULL;/*背景色方格,使用它擦出表格中的图形*/
int InitGraph()/*初始化图形界面*/
{
int GraphDriver; /* The Graphics device driver */
int GraphMode; /* The Graphics mode value */
int ErrorCode;
GraphDriver = DETECT; /* Request auto-detection */
initgraph( GraphDriver, GraphMode, "" );
ErrorCode = graphresult(); /* Read result of initialization*/
if( ErrorCode != grOk )
{ /* Error occured during init */
printf(" Graphics System Error: %s\n", grapherrormsg( ErrorCode ) );
getch();
return FALSE;
}
cleardevice();
black = (void*)malloc(imagesize(1,1,WIDTH-1,HEIGHT-1));
getimage(1,1,WIDTH-1,HEIGHT-1,black);
DrawFrame();
DrawData();
return TRUE;
}
int DrawFrame()/*画边框和表头*/
{
settextjustify(CENTER_TEXT, CENTER_TEXT);
gprintf(320, HEIGHT/2-1, "Multi-Batch System Emulation");
settextjustify(LEFT_TEXT, TOP_TEXT);
moveto(0,HEIGHT);
lineto(0,479);
lineto(639,479);
lineto(639,HEIGHT);
lineto(0,HEIGHT);
line(WIDTH*4,HEIGHT,WIDTH*4,479);
line(WIDTH*7,HEIGHT,WIDTH*7,479);
line(0,HEIGHT*2,639,HEIGHT*2);
line(0,HEIGHT*3,639,HEIGHT*3);
gprintf(HEIGHT*0+2,HEIGHT*1+2,"System Batchs");/*系统多道列表头*/
gprintf(HEIGHT*0+2,HEIGHT*2+2,"Batch");
gprintf(WIDTH*1+2,HEIGHT*2+2,"ProcID");
gprintf(WIDTH*2+2,HEIGHT*2+2,"Time");
gprintf(WIDTH*3+2,HEIGHT*2+2,"Prior");
gprintf(WIDTH*4+2,HEIGHT*1+2,"Suspended Processes");/*挂起队列列表头*/
gprintf(WIDTH*4+2,HEIGHT*2+2,"ProcID");
gprintf(WIDTH*5+2,HEIGHT*2+2,"Time");
gprintf(WIDTH*6+2,HEIGHT*2+2,"Prior");
gprintf(WIDTH*7+2,HEIGHT*1+2,"Ready Processes");/*就绪队列列表头*/
gprintf(WIDTH*7+2,HEIGHT*2+2,"ProcID");
gprintf(WIDTH*8+2,HEIGHT*2+2,"Time");
gprintf(WIDTH*9+2,HEIGHT*2+2,"Prior");
}
int DrawData()/*绘制系统数据*/
{
int numRun=0, numSus=0, numRed=0;/*运行挂起和就绪的进程各有多少*/
PCB* now = ProcQueue;
int x=0, y=0;
while (now != NULL)
{
switch(now-Sts)
{
case RUN:
x = WIDTH*1;
y = HEIGHT*(3+(numRun++));
break;
case SUSPEND:
x = WIDTH*4;
y = HEIGHT*(3+(numSus++));
break;
case READY:
x = WIDTH*7;
y = HEIGHT*(3+(numRed++));
break;
}
if (now-Sts==RUN)/*该进程为正在运行的进程*/
{
putimage(x-WIDTH+1,y+1,black,COPY_PUT);
gprintf(x-WIDTH+2,y+2,"%d",numRun);
}
if (now-PID=0)/*该进程不是后备进程*/
{
putimage(x+1,y+1,black,COPY_PUT);
gprintf(x+2,y+2,"%d",now-PID);
putimage(x+1+WIDTH,y+1,black,COPY_PUT);
gprintf(x+WIDTH+2,y+2,"%d",now-Time);
putimage(x+1+WIDTH*2,y+1,black,COPY_PUT);
gprintf(x+WIDTH*2+2,y+2,"%d",now-Prior);
}
else
{
putimage(x+1,y+1,black,COPY_PUT);
putimage(x+1+WIDTH,y+1,black,COPY_PUT);
putimage(x+1+WIDTH*2,y+1,black,COPY_PUT);
gprintf(x+2,y+2,"system idle process");
}
now = now-Next;
}
}
int DlgGetNum(char *buf,int l,int t,int r,int b,int gettime)
{
char ch;
int pos=0;
bar(l,t,r,b);
gprintf(l+10,t+5,"Add new Process");
if (gettime)
gprintf(l+10,t+20,"input the time:");
else
gprintf(l+10,t+20,"input the priority:");
while (1)
{
ch = getch();
if (ch == ENTER)/*如果输入了回车键*/
{
if(pos!=0)/*如果位置不在第一位(回车键不准第一个输入)*/
{
buf[pos]='\0';
break;
}
}
else if (ch='0' ch='9')
{
buf[pos++]=ch;
buf[pos]='\0';
}
else if (ch == ESC)
{
return FALSE;
}
else/*其他按键均按BackSpace处理*/
{
--pos;
buf[pos]='\0';
}
if (pos0)
{ pos=0; buf[pos]='\0';}
else if (pos4)/*最多输入4位数*/
{ pos=4; buf[pos]='\0';}
bar(l,t+35,r,t+47);
gprintf(l+50,t+35,buf);
}
return TRUE;
}
int NewProcDlg()
{
int l=200,t=150,r=380,b=200,pos=0,prior=0,time=0;
char buf[5],ch;
PCB *pcb;
void* bg = (void*)malloc(imagesize(l,t,r,b));
getimage(l,t,r,b,bg);
setfillstyle(1,2);
flushall();
/*******输入优先级**********/
if (!DlgGetNum(buf,l,t,r,b,FALSE))
goto exit;
prior = atoi(buf);
/*******输入时间**********/
pos=0;
buf[pos]='\0';
if (!DlgGetNum(buf,l,t,r,b,TRUE))
goto exit;
time = atoi(buf);
InsertProc(prior, time);
exit:
putimage(l,t,bg,COPY_PUT);
free(bg);
return TRUE;
}
int gprintf( int xloc, int yloc, char *fmt, ... )/*图形系统中的格式输出*/
{
va_list argptr; /* Argument list pointer */
char str[140]; /* Buffer to build sting into */
int cnt; /* Result of SPRINTF for return */
va_start( argptr, format ); /* Initialize va_ functions */
cnt = vsprintf( str, fmt, argptr ); /* prints string to buffer */
outtextxy( xloc, yloc, str ); /* Send string in graphics mode */
va_end( argptr ); /* Close va_ functions */
return( cnt ); /* Return the conversion count */
}
/****************************************************************/
/*main函数*/
int main()
{
clock_t start=0, end=0;
char kb;
InitBatchs(3);/*初始化为3道系统*/
InitGraph();
while (1)
{
start = end = clock();
while (!kbhit())
{
start = clock();
if ((start-end)/18.2 = 1)/*时间过了一秒*/
{
start = end = clock();
PassSeconds(1);
cleardevice();
DrawFrame();
DrawData();
}
}
kb = getch();
switch (kb)
{
case ESC:
closegraph();
return 0;
case 'w':
AddBatch();
UpdateBatchs();
cleardevice();
DrawFrame();
DrawData();
break;
case 's':
DeleteBatch();
UpdateBatchs();
cleardevice();
DrawFrame();
DrawData();
break;
case 'i':
NewProcDlg();
UpdateBatchs();
DrawFrame();
DrawData();
break;
}
}
return 0;
}







