我编写了以下代码来实现循环调度程序。代码如何工作:我要求用户在按下 'p' 后输入进程名称(将用于识别进程的浮点数)和进程突发时间(进程运行的总时间)。有两个队列:一个总是包含当前正在运行的进程的作业队列(即它总是只包含一个进程)和一个包含所有其他进程的就绪队列。没有等待队列功能和多线程。每当用户进入一个新进程时,它就会被放入就绪队列并调用 rrscheduler。,因此代码不会太复杂。使用time(NULL) 实现计时器
如果一个进程已经在运行并且按下了“p”,那么 rrscheduler 在将新进程添加到就绪队列后继续该进程(但是,计时器再次启动)。否则,启动就绪队列中的前端进程。
returnname()->返回队列前面的进程名 peek()->返回队列前面的进程完成的剩余时间 deletecell()-删除队列前面的进程 append()-将进程添加到队列末尾
队列中的每个进程都以其名称和剩余时间为特征。
即使队列为空,peek() 也会返回 0,用于检查队列是否为空。
代码:
queue<float> job(0);
queue<float> ready(0);
queue<float> waiting(0);
char c;
time_t quantum;
time_t quantrem, timerem;
pthread_t thread1=0;
/////////////////////////////////////////////////
struct arg //this is the argument passed to the timer function
{
time_t time1;
float name1;
time_t quantum1;
};
//////////////////////////////////////////////////
void* timer(void *);
void rrscheduler(queue<float> &ready, queue<float> &job)
{
if(thread1!=0) //when 'p' was pressed, another process was executing
pthread_cancel(thread1);
thread1=0;
if(job.peek()==0 && ready.peek()!=0) //if there are no processes currently running or the process' time or quantum has finished
{
cout<<"on the front end of ready queue is process "<<ready.returnname()<<endl;
float a=ready.peek();
float b=ready.returnname();
ready.deletecell(); //remove a process ffrom ready queue and put it in job queue
job.append(a,b);
cout<<"Process "<<b<<"left the ready queue and joined the job queue"<<endl;
}
arg arg1;
arg1.time1=job.peek();
arg1.name1=job.returnname();
arg1.quantum1=quantum;
pthread_create(&thread1, NULL, timer, (void*)(&arg1)); //call the timer function thread
}//rrscheduler ends
///////////////////////////////////////////////////////
void* timer(void *argum)
{
arg *arg1= (arg*)argum;
cout<<"PROCESS "<<arg1->name1<<" HAS ENTERED THE TIMER"<<endl;
time_t d, timejob;
d=arg1->quantum1 + time(NULL);
timejob=arg1->time1 + time(NULL);
while((quantrem=d-time(NULL))>0 && (timerem=timejob-time(NULL))>0)
{
//execute till either the process time or the process quantum gets finished
}
if(timerem>0)
{
cout<<"Time quantum finished for "<<arg1->name1<<endl;
job.deletecell();
cout<<"JOB DELETED"<<endl;
ready.append(timerem, arg1->name1);
}
else
{
cout<<"Process "<<arg1->name1<<" finished"<<endl;
job.deletecell();
}
rrscheduler(ready, job);
}
///////////////////////////////////////////////////////////
int main()
{
cout<<"Enter time quantum"<<endl;
cin>>quantum;
cout<<"Press p for entering a new process "<<endl;
while(1)
{
c=cin.get();
switch(c)
{
case 'p':
if(thread1!=0) //if when 'p' is pressed, another process was executing
{
float n = job.returnname();
job.deletecell();
job.append(timerem, n);
pthread_cancel(thread1);
}
thread1=0;
cout<<"Enter the process number and time"<<endl;
float a, timeini;
cin>>a >>timeini;
ready.append(timeini, a);
rrscheduler(ready, job);
break;
default:
break;
}//switch ends here
}//while finishes
}//main finishes
在输出“过程 1 已进入计时器”并在输出“时间量子完成 3.99296e-34”处阻塞后,该过程进入 while 循环。
我做错了什么导致分段错误,为什么进程名称显示为 3.99296e-34?
如果有人想查看队列头文件,它就在这里
提前致谢!!