#include "stdafx.h"
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
#define TIMEQUANTUM 1;
#define MAXPROCTIME 100;
struct SortList
{
int pid;
int arrivalTime;
int burstTime;
SortList *nxt;
};
struct JobList {
int pid;
int ct_arrival;
int ct_service;
int vt_served;
int vt_residence;
int vt_wait;
int vt_completion;
JobList *nxt;
};
bool initJobList(JobList *&,int *,int);
bool deinit(JobList *&);
bool doFCFS (JobList *&);
bool doRR (JobList *&);
bool doSJF(JobList *&, SortList *&);
bool doNPSJF (JobList *&);
bool doPSJF (JobList *&);
bool sortJob (JobList *&, SortList *&);
int _tmain(int argc, _TCHAR* argv[])
{
JobList *ptrJobStart;
// arrivaltime , bursttime
int arrJobs[][2] = {
{0,7},
{2,4},
{4,1},
{5,4}
};
SortList *ptrList;
initJobList (ptrJobStart,(int *)&arrJobs, (sizeof(arrJobs) / (sizeof(int)*2)));
printf("\ndoFCFS\nCPU\t\tPROCESS\n");
doFCFS (ptrJobStart);
deinit(ptrJobStart);
initJobList (ptrJobStart,(int *)&arrJobs, (sizeof(arrJobs) / (sizeof(int)*2)));
printf("\n\ndoSJF\nCPU\t\tPROCESS\n");
doSJF(ptrJobStart, ptrList);
deinit(ptrJobStart);
initJobList (ptrJobStart,(int *)&arrJobs, (sizeof(arrJobs) / (sizeof(int)*2)));
printf("\ndoRR\nCPU\t\tPROCESS\n");
doRR(ptrJobStart);
deinit(ptrJobStart);
initJobList (ptrJobStart,(int *)&arrJobs, (sizeof(arrJobs) / (sizeof(int)*2)));
printf("\ndoPSJF\nCPU\t\tPROCESS\n");
doRR(ptrJobStart);
deinit(ptrJobStart);
getch();
return 0;
}
bool doRR(JobList *&ptrJobStart)
{
if(ptrJob==NULL) return false;
JobList *ptrJobStart, *tempJobList;
ptrJobStart = ptrJob;
int maxTime = MAXPROCTIME;
int timeQuantum = TIMEQUANTUM;
bool flag = false;
ptrJobStart->vt_served = 0;
for(int processTime = 0; (processTime < maxTime) && !flag ;)
{
//check if all jobs are finished
flag = true;
tempJobList = ptrJob;
while(tempJobList!=NULL)
{
if(tempJobList->vt_served!=tempJobList->ct_service)
{
flag = false;
}
tempJobList = tempJobList->nxt;
}
}
//logic
return true;
}
bool doSJF(JobList *&ptrJobStart, SortList *&ptrList)
{
if(ptrJobStart==NULL) return false;
sortJob(ptrJobStart, ptrList);
doFCFS (ptrJobStart);
return true;
}
bool sortJob (JobList *&ptrJob, SortList *&ptrList)
{
JobList *ptrJobStart = ptrJob;
if(ptrJobStart==NULL) return false;
int maxTime = MAXPROCTIME;
SortList *newList;
ptrList = NULL;
for(; ptrJobStart!=NULL; ptrJobStart = ptrJobStart->nxt) // accumulate all arrival time = 0
{
if(ptrJobStart->ct_arrival==0)
{
if(ptrList==NULL)
{
ptrList = new SortList;
ptrList->pid = ptrJobStart->pid;
ptrList->arrivalTime = ptrJobStart->ct_arrival;
ptrList->burstTime = ptrJobStart->ct_service;
ptrList->nxt = NULL;
}
else
{
newList = new SortList;
newList->pid = ptrJobStart->pid;
newList->arrivalTime = ptrJobStart->ct_arrival;
newList->burstTime = ptrJobStart->ct_service;
newList->nxt = NULL;
ptrList->nxt = newList;
}
}
}
SortList *tempList;
tempList = ptrList;
for(int processTime = 1 ; processTime < maxTime ; processTime++) // accumulate all burst time from 1 to 100
{
ptrJobStart = ptrJob;
do
{
if((ptrJobStart->ct_arrival!=0) && (processTime == ptrJobStart->ct_service))
{
newList = new SortList;
newList->pid = ptrJobStart->pid;
newList->arrivalTime = ptrJobStart->ct_arrival;
newList->burstTime = ptrJobStart->ct_service;
newList->nxt = NULL;
tempList->nxt = newList;
tempList = tempList->nxt;
}
ptrJobStart = ptrJobStart->nxt;
}while(ptrJobStart!=NULL);
}
//sort jobs
ptrJobStart = ptrJob;
JobList *tempJobList;
tempJobList = ptrJobStart;
for(; ptrList!=NULL; ptrList = ptrList->nxt)
{
tempJobList->pid = ptrList->pid;
tempJobList->ct_arrival = ptrList->arrivalTime;
tempJobList->ct_service = ptrList->burstTime;
tempJobList = tempJobList->nxt;
}
return true;
}
bool doFCFS (JobList *& ptrJob)
{
JobList *ptrJobStart = ptrJob;
if(ptrJobStart==NULL) return false;
int maxTime = MAXPROCTIME;
ptrJobStart->vt_served = 0;
for(int processTime = 0; processTime < maxTime; processTime++)
{
if(ptrJobStart->vt_served!=ptrJobStart->ct_service)
{
ptrJobStart->vt_served+=1;
}
else
{
ptrJobStart->vt_completion = processTime;
ptrJobStart->vt_residence = processTime - ptrJobStart->ct_arrival;
ptrJobStart->vt_wait = processTime - ptrJobStart->vt_served;
cout<<"\n/* vt_served:"<<ptrJobStart->vt_served<<"\tvt_residence:"<<ptrJobStart->vt_residence
<<"\tvt_wait:"<<ptrJobStart->vt_wait<<"\tvt_completion:"<<ptrJobStart->vt_completion<<" */";
ptrJobStart = ptrJobStart->nxt;
if(ptrJobStart==NULL) break;
else
ptrJobStart->vt_served = 1;
}
cout<<"\n "<<processTime<<"\t>>\t P"<<ptrJobStart->pid;
}
return true;
}
bool deinit (JobList *& ptrJobStart) {
JobList *ptrJobDel = NULL;
do {
ptrJobDel = ptrJobStart;
ptrJobStart = ptrJobStart->nxt;
delete ptrJobDel;
}while (ptrJobStart != NULL);
return true;
}
bool initJobList(JobList*& ptrJobStart,int *pJob,int nJobs){
JobList * ptrJobs = NULL;
int i;
int j=1;
for (i=0; i<nJobs*2; i+=2) {
if (ptrJobs == NULL) {
ptrJobs = new JobList;
memset(ptrJobs,0,sizeof(JobList));
ptrJobStart = ptrJobs;
}
else {
ptrJobs->nxt = new JobList;
memset(ptrJobs->nxt,0,sizeof(JobList));
ptrJobs = ptrJobs->nxt;
}
ptrJobs->ct_arrival = *(pJob+i);
ptrJobs->ct_service = *(pJob+(i+1));
ptrJobs->pid = j;
ptrJobs->nxt = NULL;
j++;
}
return true;
此代码用于调度算法。最短的Job Next(SJN)/ Shortest Job First(SJF)(即doSJN)正在运行。但是当我加入doRR(做Round Robin)时,会发生很多错误。
错误C2065:'ptrJob':未声明的标识符 错误C2082:重新定义形式参数'ptrJobStart 错误C2065:'ptrJob':未声明的标识符 错误C2065:'ptrJob':未声明的标识符 看到'getch'的声明
我该如何解决这个问题?
答案 0 :(得分:1)
在doRR
中,参数名为ptrJobStart
。它应该是ptrJob
。
您收到的错误表明,在doRR
的第一行,ptrJob
尚未声明。
答案 1 :(得分:1)
第一个错误(重新定义形式参数'ptrJobStar')出现在这里:
bool doRR(JobList *&ptrJobStart)
{
if(ptrJob==NULL) return false;
JobList *ptrJobStart, *tempJobList;
您正在定义参数ptrJobStart,然后在代码中声明同名参数。
我相信第二个错误('ptrJob':未声明的标识符)出现在这里:
bool doRR(JobList *&ptrJobStart)
{
if(ptrJob==NULL) return false;
那是因为'ptrJob'似乎没有在doRR()中声明。
希望有所帮助。