我寫了下面的代碼來實現循環調度程序。 代碼如何工作:我要求用戶輸入進程名稱(將用於識別進程的浮點數)和進程爆發時間(進程運行的總時間)'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
謝謝!
arg :: name1被定義爲float,那麼還應該顯示哪些內容?你到底在哪裏得到一個段錯誤(gdb會告訴你)? – codeling
可能會派上用場http://floating-point-gui.de/ – Indy9000
爲什麼你使用浮點數來標識進程?這響起了很多警鐘。如果您必須具有_xxx.yyy_形式的標識符,則可以在結構體或字符串中使用一對整數。 – Rook