版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
《操作系統(tǒng)》實驗報告實驗序號:3實驗項目名稱:作業(yè)調(diào)度的實現(xiàn)學(xué)號1209030109姓名陶爽專業(yè)、班信管1201實驗地點文波331指導(dǎo)教師屈振新時間2013.12.24一、實驗?zāi)康募耙笤趌inux環(huán)境下,完成驗證性實驗——作業(yè)調(diào)度的實現(xiàn)。二、實驗設(shè)備(環(huán)境)及要求(1)操作系統(tǒng):Linux(2)開發(fā)環(huán)境:Linux終端(3)1G以上的CPU處理器,512MB以上的內(nèi)存,10G的自由硬盤空間三、實驗內(nèi)容與步驟程序算法如下:job.c#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<sys/time.h>
#include<sys/wait.h>
#include<string.h>
#include<signal.h>
#include<fcntl.h>
#include<time.h>
#include"job.h"
intjobid=0;
intsiginfo=1;
intfifo;
intglobalfd;
structwaitqueue*head=NULL;
structwaitqueue*next=NULL,*current=NULL;
voidschedule()
{
structjobinfo*newjob=NULL;
structjobcmdcmd;
intcount=0;
bzero(&cmd,DATALEN);
if((count=read(fifo,&cmd,DATALEN))<0)
error_sys("readfifofailed");
#ifdefDEBUG
if(count){
printf("cmdcmdtype\t%d\n"
"cmddefpri\t%d\n"
"cmddata\t%s\n",
cmd.type,cmd.defpri,cmd.data);
}else
printf("nodataread\n");
#endif
switch(cmd.type){
caseENQ:
do_enq(newjob,cmd);
break;
caseDEQ:
do_deq(cmd);
break;
caseSTAT:
do_stat(cmd);
break;
default:
break;
}
/*Updatejobsinwaitqueue*/
updateall();
/*selectthehighestpriorityjobtorun*/
next=jobselect();
/*stopcurrentjob,runnextjob*/
jobswitch();
}
intallocjid()
{
return++jobid;
}
voidupdateall()
{
structwaitqueue*p;
/*updaterunningjob'srun_time*/
if(current)
current->job->run_time+=1;/*add1represent100ms*/
/*updatereadyjob'swait_time*/
for(p=head;p!=NULL;p=p->next){
p->job->wait_time+=100;
if(p->job->wait_time>=1000&&p->job->curpri<3)
p->job->curpri++;
}
}
structwaitqueue*jobselect()
{
structwaitqueue*p,*prev,*select,*selectprev;
inthighest=-1;
select=NULL;
selectprev=NULL;
if(head){
for(prev=head,p=head;p!=NULL;prev=p,p=p->next)
if(p->job->curpri>highest){
select=p;
selectprev=prev;
highest=p->job->curpri;
}
selectprev->next=select->next;
if(select==selectprev)
head=NULL;
}
returnselect;
}
voidjobswitch()
{
structwaitqueue*p;
inti;
if(current&¤t->job->state==DONE){/*currentjobfinished*/
/*jobhasbeendone,removeit*/
for(i=0;(current->job->cmdarg)[i]!=NULL;i++){
free((current->job->cmdarg)[i]);
(current->job->cmdarg)[i]=NULL;
}
free(current->job->cmdarg);
free(current->job);
free(current);
current=NULL;
}
if(next==NULL&¤t==NULL)/*nojobtorun*/
return;
elseif(next!=NULL&¤t==NULL){/*startnewjob*/
//printf("beginstartnewjob\n");
current=next;
next=NULL;
current->job->state=RUNNING;
kill(current->job->pid,SIGCONT);
return;
}elseif(next!=NULL&¤t!=NULL){/*doswitch*/
//printf("beginswitch\n");
kill(current->job->pid,SIGSTOP);
current->job->curpri=current->job->defpri;
current->job->wait_time=0;
current->job->state=READY;
/*movebacktothequeue*/
if(head){
for(p=head;p->next!=NULL;p=p->next);
p->next=current;
}else{
head=current;
}
current=next;
next=NULL;
current->job->state=RUNNING;
kill(current->job->pid,SIGCONT);
return;
}else{/*next==NULL&¤t!=NULL,noswitch*/
return;
}
}
voidsig_handler(intsig,siginfo_t*info,void*notused)
{
intstatus;
intret;
switch(sig){
caseSIGVTALRM:
schedule();
return;
caseSIGCHLD:
ret=waitpid(-1,&status,WNOHANG);
if(ret==0)
return;
if(WIFEXITED(status)){
current->job->state=DONE;
printf("normaltermation,exitstatus=%d\n",WEXITSTATUS(status));
}/*elseif(WIFSIGNALED(status)){*/
/*printf("abnormaltermation,signalnumber=%d\n",WTERMSIG(status));*/
/*}elseif(WIFSTOPPED(status)){*/
/*printf("childstopped,signalnumber=%d\n",WSTOPSIG(status));*/
/*}*/
return;
default:
return;
}
}
voiddo_enq(structjobinfo*newjob,structjobcmdenqcmd)
{
structwaitqueue*newnode,*p;
inti=0,pid;
char*offset,*argvec,*q;
char**arglist;
sigset_tzeromask;
sigemptyset(&zeromask);
/*filljobinfostruct*/
newjob=(structjobinfo*)malloc(sizeof(structjobinfo));
newjob->jid=allocjid();
newjob->defpri=enqcmd.defpri;
newjob->curpri=enqcmd.defpri;
newjob->ownerid=enqcmd.owner;
newjob->state=READY;
newjob->wait_time=0;
newjob->run_time=0;
arglist=(char**)malloc(sizeof(char*)*(enqcmd.argnum+1));
newjob->cmdarg=arglist;
offset=enqcmd.data;
argvec=enqcmd.data;
while(i<enqcmd.argnum){
if(*offset==':'){
*offset++='\0';
q=(char*)malloc(offset-argvec);
strcpy(q,argvec);
arglist[i++]=q;
argvec=offset;
}else
offset++;
}
arglist[i]=NULL;
newjob->create_time=time(NULL);
#ifdefDEBUG
printf("enqcmdargnum%d\n",enqcmd.argnum);
for(i=0;i<enqcmd.argnum;i++)
printf("parseenqcmd:%s\n",arglist[i]);
#endif
/*addnewjobtothequeue*/
newnode=(structwaitqueue*)malloc(sizeof(structwaitqueue));
newnode->next=NULL;
newnode->job=newjob;
if(head){
for(p=head;p->next!=NULL;p=p->next);
p->next=newnode;
}else
head=newnode;
/*createprocessforthejob*/
if((pid=fork())<0)
error_sys("enqforkfailed");
/*Inchildprocess*/
if(pid==0){
newjob->pid=getpid();
/*blockthechildwaitforrun*/
raise(SIGSTOP);
#ifdefDEBUG
printf("beginrunning\n");
for(i=0;arglist[i]!=NULL;i++)
printf("arglist%s\n",arglist[i]);
#endif
/*duptheglobalfiledescriptortostdout*/
//dup2(globalfd,1);
if(execv(arglist[0],arglist)<0)
printf("execfailed\n");
exit(1);
}else{
newjob->pid=pid;
}
}
/*bugtofix*/
voiddo_deq(structjobcmddeqcmd)
{
intdeqid,i;
structwaitqueue*p,*prev,*select,*selectprev;
deqid=atoi(deqcmd.data);
#ifdefDEBUG
printf("deqjid%d\n",deqid);
#endif
/*currentjodid==deqid,terminatecurrentjob*/
if(current&¤t->job->jid==deqid){
printf("terminatecurrentjob\n");
kill(SIGTERM,current->job->pid);
for(i=0;(current->job->cmdarg)[i]!=NULL;i++){
free((current->job->cmdarg)[i]);
(current->job->cmdarg)[i]=NULL;
}
free(current->job->cmdarg);
free(current->job);
free(current);
current=NULL;
}else{/*maybeinwaitqueue,searchit*/
select=NULL;
selectprev=NULL;
if(head){
for(prev=head,p=head;p!=NULL;prev=p,p=p->next)
if(p->job->jid==deqid){
select=p;
selectprev=prev;
break;
}
selectprev->next=select->next;
if(select==selectprev)
head=NULL;
}
if(select){
for(i=0;(select->job->cmdarg)[i]!=NULL;i++){
free((select->job->cmdarg)[i]);
(select->job->cmdarg)[i]=NULL;
}
free(select->job->cmdarg);
free(select->job);
free(select);
select=NULL;
}
}
}
voiddo_stat(structjobcmdstatcmd)
{
structwaitqueue*p;
intstatfd;
/*
*Printjobstatisticsofalljobs:
*1.jobid
*2.jobpid
*3.jobowner
*4.jobruntime
*5.jobwaittime
*6.jobcreatetime
*7.jobstate
*/
if((statfd=open("/tmp/stat",O_WRONLY))<0)
error_sys("openfifofailed");
if(current){
write(statfd,current->job,sizeof(structjobinfo));
}
for(p=head;p!=NULL;p=p->next){
write(statfd,p->job,sizeof(structjobinfo));
}
close(statfd);
}
intmain()
{
structtimevalinterval;
structitimervalnew,old;
structstatstatbuf;
structsigactionnewact,oldact1,oldact2;
if(stat("/tmp/server",&statbuf)==0){
/*iffifofileexists,removeit*/
if(remove("/tmp/server")<0)
error_sys("removefailed");
}
if(mkfifo("/tmp/server",0666)<0)
error_sys("mkfifofailed");
/*openfifoinnonblockmode*/
if((fifo=open("/tmp/server",O_RDONLY|O_NONBLOCK))<0)
error_sys("openfifofailed");
/*openglobalfileforjoboutput*/
if((globalfd=open("/dev/null",O_WRONLY))<0)
error_sys("openglobalfilefailed");
/*setupsigna
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年滬科版必修3英語上冊月考試卷含答案
- 2025年外研版2024選修2地理上冊階段測試試卷
- 二零二五版門衛(wèi)值班人員設(shè)備維護聘用合同4篇
- 2025年度新能源汽車電池回收與利用分包合同4篇
- 二零二五年度智能物流解決方案內(nèi)部銷售承包合同4篇
- 二零二五年度木門行業(yè)環(huán)保標準采購合同2篇
- 《包裝設(shè)計》 案例賞析 第4章 香生記品牌包裝設(shè)計
- 2025版內(nèi)退員工勞動合同范本:食品行業(yè)專用4篇
- 2025年度影視基地租賃合同范本及知識產(chǎn)權(quán)保護協(xié)議3篇
- 2025年農(nóng)場農(nóng)業(yè)廢棄物回收利用服務(wù)合同4篇
- 平安產(chǎn)險陜西省地方財政生豬價格保險條款
- 銅礦成礦作用與地質(zhì)環(huán)境分析
- 30題紀檢監(jiān)察位崗位常見面試問題含HR問題考察點及參考回答
- 詢價函模板(非常詳盡)
- 《AI營銷畫布:數(shù)字化營銷的落地與實戰(zhàn)》
- 麻醉藥品、精神藥品、放射性藥品、醫(yī)療用毒性藥品及藥品類易制毒化學(xué)品等特殊管理藥品的使用與管理規(guī)章制度
- 一個28歲的漂亮小媳婦在某公司打工-被老板看上之后
- 乘務(wù)培訓(xùn)4有限時間水上迫降
- 2023年低年級寫話教學(xué)評語方法(五篇)
- DB22T 1655-2012結(jié)直腸外科術(shù)前腸道準備技術(shù)要求
- GB/T 16474-2011變形鋁及鋁合金牌號表示方法
評論
0/150
提交評論