操作系統(tǒng)實驗報告3_第1頁
操作系統(tǒng)實驗報告3_第2頁
操作系統(tǒng)實驗報告3_第3頁
操作系統(tǒng)實驗報告3_第4頁
操作系統(tǒng)實驗報告3_第5頁
已閱讀5頁,還剩26頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(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. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論