2012-09-11 42 views
0

我写了下面的代码来实现循环调度程序。 代码如何工作:我要求用户输入进程名称(将用于识别进程的浮点数)和进程爆发时间(进程运行的总时间)'p'。有两个队列:一个工作队列总是包含当前运行的进程(即它始终只包含一个过程)和准备队列它包含了所有其他processes.There是没有等待队列使这里代码不太复杂。 A 定时器使用时间(NULL)函数和多线程来实现。每当用户进入一个新的进程时,它就进入就绪队列并调用rrscheduler。调度程序使用多线程编程

如果一个进程已经在运行并且'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 

过程进入而输出后循环 'PROCESS 1已经进入TIMER' 在输出 '时间量子完成了3.99296e-34' 和块。

我在做什么错误,它导致分段错误,为什么进程名称显示为3.99296e-34?

如果有人希望看到队列头文件,它是提前here

谢谢!

+1

arg :: name1被定义为float,那么还应该显示哪些内容?你到底在哪里得到一个段错误(gdb会告诉你)? – codeling

+0

可能会派上用场http://floating-point-gui.de/ – Indy9000

+0

为什么你使用浮点数来标识进程?这响起了很多警钟。如果您必须具有_xxx.yyy_形式的标识符,则可以在结构体或字符串中使用一对整数。 – Rook

回答

2

arg::name1被定义为float,那么还应该显示什么?你没有显示returnname的定义,但由于job.returnname是分配兼容的,我认为这也已经返回了一个浮点数或可转换的东西。

你可以找出你用gdb下运行的程序有一个段错误:

gdb your-program 

你应该-g先编译,当然你的程序。

+0

@nyarlathotep ... name1当然是一个浮点值,但它是由用户输入的,并且是自然数的形式。我没有改变它。那么,它是如何变成3.99296e-34的。进一步的returnname()定义在头文件中给出,我提供了链接。 – avinash