文档库 最新最全的文档下载
当前位置:文档库 › 操作系统实验代码2

操作系统实验代码2

#include
#include
#include
#include
#include
#include
#include

typedef struct pcbStruct
{
int pid;
char name[10];
int status;
int type;
int res;
int totalTime;
int runTime;
int count;
int prio;
struct pcbStruct *next;
}PCB;

typedef struct resStruct
{
int pid;
int free;
}Resource;

Resource resource;
PCB *finish,*ready,*tail,*run,*wait,*head;

int N;
int timeSlice=2;
int hodeUpTime=3;
int randomPrio(double from,double to)
{
return 1+(int)((to)*rand()/(RAND_MAX+from));
}

runIn()
{
run=ready;
run->status=1;
ready=ready->next;
}

void print1()
{
printf("---------------------\n");
printf("pid name status type prio res totalTime count runTime\n");
}

void print2(PCB *q)
{
printf("%d|&-4s|%-4d|%-6d|%-4d|%-4d|%-8d|%-5d|%-d\n",q->pid,q->name,q->status,q->type,q->prio,q->res,q->totalTime,q->count,q->runTime);
}

void print()
{
PCB *p;
if(run!=NULL)
{
printf("Running...............\n");
print2(run);
}
p=ready;
if(p!=NULL)printf("Ready............\n");
while(p!=NULL)
{
print2(p);
p=p->next;
}
p=wait;
if(p!=NULL)printf("Waiting.........................\n");
while(p!=NULL)
{
print2(p);
p=p->next;
}
p=finish;
if(p!=NULL)printf("Finished.............\n");
while(p!=NULL)
{
print2(p);
p=p->next;
}
print1();
}

insertReady(PCB *p2)
{
tail->next=p2;
tail=p2;
p2->next=NULL;
}

insertWait(PCB *p2)
{
head->next=p2;
head=p2;
p2->next=NULL;
}

void creat()
{
PCB *p;
int i,time;
char na[10];
ready=NULL;
finish=NULL;
run=NULL;
wait=NULL;
printf("enter name and run time of each process:(eg.pidl[press enter]100):\n");
for(i=1;i{
p=malloc(sizeof(PCB));
p->pid=1000+i;
scanf("%s",na);
scanf("%d",&time);
strcpy(p->name,na);
p->status=2;
if(i%2==0)
{
p->type=0;
p->prio=randomPrio(1.0,9.0);
}
else
{
p->type=1;
p->prio=randomPrio(11.0,19.0);
}
p->res=time/2;
p->totalTime=time;
p->count=0;
p->runTime=0;
if(ready!=NULL)
insertReady(p);
else
{
p->next=ready;
ready=p;
tail=p;
}
}
printf("*******调度开始*********\n");
print1();
print();
run=ready;
ready=ready->next;
run->status=1;
}

timeRoundRun()
{
while(run!=NULL)
{
if(run->res==run->runTime)
{
if(resource.free==1)
{
resource.pid=run->pid;
resource.free=0;
}
else
{
run->count=0;
run->status=3;
PCB *p=run;
if(wait!=NULL)
insertWait(p);
else
{
p->next=wait;
wait=p;
head=p;
}
runIn();
}
}
run->runTime=run->runTime+1;
run->count=run->count+1;
sleep(1);
if((run->runTime-run->res)>=hodeUpTime)
{
resource.free=1;
if(wait!=NULL)
{
PCB *p=wait;
wait=wait->next;
p->

status=2;
insertReady(p);
}
}
if(run->runTime>=run->totalTime)
{
if(run->pid==resource.pid)
{
resource.free=1;
if(wait!=NULL)
{
PCB *p=wait;
wait=wait->next;
p->status=2;
insertReady(p);
}
}
run->next=finish;
finish=run;
run->status=0;
run=NULL;
if(ready!=NULL)
runIn();
}
else if(run->count==timeSlice)
{
run->count=0;
if(resource.free==1)
{
if(wait!=NULL)
{
PCB *p=wait;
wait=wait->next;
p->status=2;
insertReady(p);
}
}
if(ready!=NULL)
{
run->status=2;
insertReady(run);
runIn();
}
}
print();
}
}

int main(){
resource.free=1;
printf("Enter process number\n");
scanf("%d",&N);
creat();
prioChangerun();
}

相关文档