/*本c程序为按优先数的进程调度*/
#include
#include
#include
#define NULL 0
#define LEN sizeof(struct pcb)
struct pcb
{ int pid;
struct pcb *next;
int time;
int priority;
char status;
};
int n,m;
struct pcb *head,*runpcb;
main()
{ struct pcb *pcbi;
char str;
struct pcb *cshlist();
void insertproc(struct pcb *insproc);
void pintlist(struct pcb *L);
void execproc();
void delproc();
m=0;
head=cshlist();
printf("first linelist is:\n");
pintlist(head);
while(head!=NULL)
{ scanf("%c",&str);
runpcb=head;
head=runpcb->next;
runpcb->status='Z';
printf("output linelist is:\n");
printf("%d\n",m);
printf("%2d %5d %5d %3c\n",runpcb->pid,runpcb->time,runpcb->priority,runpcb->status);
pintlist(head);
printf("\n");
printf("\n");
execproc();
m=m+1;
if(runpcb->time==0)
delproc();
else insertproc(runpcb);
}
}
void pintlist(struct pcb *L)
{
struct pcb *p;
int i;
p=L;
if(L!=NULL)
do
{ printf("%2d %5d %5d %3c\n",p->pid,p->time,p->priority,p->status);
p=p->next;
}while (p!=NULL);
}
struct pcb *cshlist()
{
struct pcb *ql;
n=0;
ql=(struct pcb *)malloc(LEN);
ql->pid=n+1;
ql->status='R';
printf("enter time and priority:\n");
scanf("%ld,%d",&ql->time,&ql->priority);
head=NULL;
while(ql->time!=0)
{
n=n+1;
insertproc(ql);
ql=(struct pcb *)malloc(LEN);
printf("enter timeand priority:\n");
ql->pid=n+1;
ql->status='R';
}
return(head);
}
void insertproc(struct pcb *insproc)
{
struct pcb *p0,*p1,*p2;
int pri;
p1=head;
p0=insproc;
if(head==NULL)
{
head=p0;
p0->next=NULL;
}
else
{
pri=p0->priority;
if(p1->priority<=pri)
{
p0->next=head;
head=insproc;
}
else
{
while((p1->next!=NULL)&&(p1->priority>pri))
{ p2=p1;
p1=p1->next;
}
if((p1->next!=NULL)||(p1->priority<=pri))
{
p2->next=p0;
p0->next=p1;
}
else
{
p1->next=p0;
p0->next=NULL;
}
}
}
}
void execproc()
{
runpcb->time=runpcb->time-1;
runpcb->priority=runpcb->priority-1;
}
void delproc()
{
struct pcb *p;
p=runpcb;
p->status='E';
printf("process P");
printf("%d",p->pid);
printf(" is finish\n");
printf("\n");
free(runpcb);
}
优先数调度算法方面和时间片轮转调度算法(再给你个c++的)
#include
#include
#include
using namespace std;
int n;
class PCB
{
public:
int pri;//进程优先数
int runtime;//进程运行CPU时间
int pieceOftime;//轮转时间片
string procname;//进程名
string state;//进程状态
int needOftime;//还需要时间
int Counter;
PCB * next;
};
PCB * run = NULL;
PCB * ready = NULL;
PCB * finish = NULL;
PCB * tial = ready;
void Dtime(int t);
void Prinft(int a)
{
if(a==1)
{
cout<
}
else
cout<
}
void Prinft(int b,PCB * p)
{
if(b==1)
{
cout<procname<pri<needOftime<runtime<state<
}
else
cout<procname<runtime<needOftime<Counter<pieceOftime<state<
}
void display(int c)
{
PCB *p;
if(run!=NULL) /*如果运行指针不空*/
Prinft(c,run); /*输出当前正在运行的PCB*/
//Dtime(2);
p=ready; /*输出就绪队列PCB*/
while(p!=NULL)
{
Prinft(c,p);
p=p->next;
}
//Dtime(2);
p=finish; /*输出完成队列的PCB*/
while(p!=NULL)
{
Prinft(c,p);
p=p->next;
}
}
void insert(PCB *p)//插入就绪队列按Pri大小
{
PCB *S1,*S2;
if(ready==NULL)
{
p->next = NULL;
ready = p;
}
else
{
S1 = ready;
S2 = S1;
while(S1!=NULL)
{
if(S1->pri >= p->pri)
{
S2 = S1;
S1 = S1->next;
}
else
break;
}
if(S2->pri >= p->pri)
{
S2->next = p;
p->next = S1;
}
else
{
p->next = ready;
ready = p;
}
}
}
bool CTProcessOfPri()
{
PCB * Node;
cout <
cin >>n;
for(int j = 0;j < n; j++)
{
Node = new PCB;
if(Node==NULL)
return false;
else
{
cout <
cin >>Node->procname>>Node->needOftime;
Node->runtime = 0;
Node->state ="就绪";
Node->pri =Node->needOftime;
cout <procname<
}
insert(Node);
}
return true;
}
void priority(int i)
{
run = ready;
ready = ready->next;
run->state = "运行";
Prinft(i);
while(run!=NULL) /*当运行队列不空时,有进程正在运行*/
{
run->runtime=run->runtime+1;
run->needOftime=run->needOftime-1;
run->pri=run->pri-1; /*每运行一次优先数降低1个单位*/
if(run->needOftime==0) /*如所需时间为0将其插入完成队列*/
{
run->state = "完成";
run->next = finish;
finish = run;
run=NULL; /*运行队列头指针为空*/
if(ready!=NULL) /*如就绪队列不空*/
{
run = ready;
run->state = "运行";
ready = ready->next;
}
}
else if((ready!=NULL)&&(run->pripri))
{
run->state="就绪";
insert(run);
run = ready;
run->state = "运行";
ready = ready->next;
}
display(i); /*输出进程PCB信息*/
}
}
void queue(PCB *p)
{
if(ready==NULL)
{
p->next = NULL;
ready = p;
tial = p;
}
else
{
tial->next = p;
tial = p;
p->next = NULL;
}
}
bool CTProcessOfRuntime()
{
PCB * Node;
int m;
cout <
cin >>n;
cout <
cin >>m;
for(int j = 0;j < n; j++)
{
Node = new PCB;
if(Node==NULL)
return false;
else
{
cout <
cin >>Node->procname>>Node->needOftime;
Node->runtime = 0;
Node->state ="就绪";
Node->Counter = 0;
Node->pieceOftime = m;
cout <procname<
}
queue(Node);
}
return true;
}
void Runtime(int c)
{
run = ready;
ready = ready->next;
run->state = "运行";
Prinft(c);
while(run!=NULL)
{
run->runtime=run->runtime+1;
run->needOftime=run->needOftime-1;
run->Counter = run->Counter + 1;
if(run->needOftime==0)
{
run->state = "完成";
run->next = finish;
finish = run;
run = NULL;
if(ready!=NULL)
{
run = ready;
ready = ready->next;
}
}
else if(run->Counter == run->pieceOftime)
{
run->Counter = 0;
run->state = "就绪";
queue(run);
run=NULL;
if(ready!=NULL)
{
run = ready;
run->state = "运行";
ready = ready->next;
}
}
display(c);
}
}
int main()
{
int i;
cout <
cout <
cout <
cin >>i;
switch(i)
{
case 1:
CTProcessOfPri();
priority(i);
break;
case 2:
CTProcessOfRuntime();
Runtime(i);
break;
default:
break;
}
return 0;
{
time(& current_time);
}while((current_time-start_time)
}
}
void Dtime(int t)
{
time_t current_time;
time_t start_time;
time(&start_time);
do