有c语言模拟调度算法吗?

   更新日期:2024.05.02
/*本c程序为按优先数的进程调度*/
#include<stdio.h>
#include<string.h>
#include<malloc.h>
#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<iostream>
#include<string>
#include<time.h>

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<<"进程名称"<<"\t"<<"优先数"<<"\t"<<"还需要时间"<<"\ t"<<"已运行时间"<<"\t"<<"状态:"<<endl;
}
else
cout<<"进程名称"<<"\t"<<"已运行时间"<<"\t"<<"还需要时间"<< "\t"<<"计数器"<<"\t"<<"时间片"<<"\t"<<"状态"<<endl;
}
void Prinft(int b,PCB * p)
{
if(b==1)
{
cout<<p->procname<<"\t\t"<<p->pri<<"\t"<<p->needOftime<<"\t\t"<<p->runtime<<"\t\t"<<p->state<<endl;
}
else
cout<<p->procname<<"\t\t"<<p->runtime<<"\t\t"<<p->needOftime<<"\t\t"<<p->Counter<<"\t"<<p->pieceOftime<<"\t"<<p->state<<endl;
}
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 <<"输入创建进程的数目:"<<endl;
cin >>n;
for(int j = 0;j < n; j++)
{
Node = new PCB;
if(Node==NULL)
return false;
else
{
cout <<"输入进程的名称,进程需CPU时间:"<<endl;
cin >>Node->procname>>Node->needOftime;
Node->runtime = 0;
Node->state ="就绪";
Node->pri =Node->needOftime;
cout <<"进程"<<Node->procname<<"创建完毕!"<<endl;
}
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->pri<ready->pri))
{
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 <<"输入创建进程的数目:"<<endl;
cin >>n;
cout <<"输入时间片:"<<endl;
cin >>m;
for(int j = 0;j < n; j++)
{
Node = new PCB;
if(Node==NULL)
return false;
else
{
cout <<"输入进程的名称,进程需CPU时间:"<<endl;
cin >>Node->procname>>Node->needOftime;
Node->runtime = 0;
Node->state ="就绪";
Node->Counter = 0;
Node->pieceOftime = m;
cout <<"进程"<<Node->procname<<"创建完毕!"<<endl;
}
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 <<"*******************************************"<<endl;
cout <<"* 1 优先数调度算法 2 循环时间片轮转算法*"<<endl;
cout <<"*************** 0 退出 *******************"<<endl;
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)<t);
}
}
void Dtime(int t)
{
time_t current_time;
time_t start_time;
time(&start_time);
do

  • 17020472324 :...采用“简单时间片轮转法”调度算法对五个进程进行调度。
    巢贸研2379 :答:int CREATE(LINK *,int); /*创建新的进程*/ void ROUNDSCH(LINK *); /*按时间片轮转法调度进程*/ void main() { LINK pcbs;int i;INIT(&pcbs);i=0;printf("创建5个进程\n\n");while(i<5) { if(CREATE...
  • 17020472324 :有c语言模拟调度算法吗?
    巢贸研2379 :答:/*本c程序为按优先数的进程调度*/ include<stdio.h> include<string.h> include<malloc.h> define NULL 0 define LEN sizeof(struct pcb)struct pcb { int pid;struct pcb *next;int time;int priority;char status...
  • 17020472324 :用C语言编程模拟处理机调度(实现一种算法)
    巢贸研2379 :答:include <stdlib.h> include <conio.h> define getpch(type) (type*)malloc(sizeof(type))define NULL 0 struct pcb { /* 定义进程控制块PCB */ char name[10];char state;int super;int ntime;int rtime;struct...
  • 17020472324 :时间片轮转算法和优先级调度算法 C语言模拟实现
    巢贸研2379 :答:进程调度是处理机管理的核心内容。本实验要求用高级语言编写模拟进程调度程序,以便加深理解有关进程控制快、进程队列等概念,并体会和了解优先数算法和时间片轮转算法的具体实施办法。二、实验内容1.设计进程控制块PCB的结构,通常应包括如下...
  • 17020472324 :急求 程序代码 c/c++ 操作系统中的 处理机调度算法
    巢贸研2379 :答:要求学生设计一个模拟处理机调度算法,以巩固和加深处理机调度的概念。设计要求:1)进程调度算法包括:时间片轮转法,短作业优先算法,先来先服务算法。2)可选择进程数量3)本程序包括三种算法,用C语言实现,执行时在主界面选择算法(可用函数...
  • 17020472324 :实时调度算法的课程设计 要用c语言
    巢贸研2379 :答:if(T%cycA==0&&T%cycB!=0){ A=T;printf("当T=%d时",T);printf("进程A%d产生 ",++a); //不可能与进程A竞争处理器 numa=0;if(numb<serveB) //进程B没有完成 if(B+cycB>A+cycA) //若...
  • 17020472324 :时间片轮转调度算法用C实现
    巢贸研2379 :答:printf(" %-10s%-10d%-10d%-10d %c\n",q->name,q->cputime,q->needtime,q->prio,q->state);else/*轮转法的输出*/ printf(" %-10s%-10d%-10d%-10d%-10d %-c\n",q->name,q->cputime,q->need...
  • 17020472324 :...如Java)编程实现对N个进程采用某种进程调度算法(如动态优先权调度...
    巢贸研2379 :答:PCB温度=新的PCB() BR />公共无效排队(PCB工艺){/ /排队算法 (指数== 5){ (“出界!”);返回 } PC [索引] =进程;指数+ +;} 公共:PCB DEQUEUE(){/ /出队算法(索引== 0)返回空;(INT I = ...
  • 17020472324 :C语言模拟操作系统进程调度和管理
    巢贸研2379 :答:/*进程调度函数*/ int proc_switch();/*进程等待函数*/ void proc_wait();/*进程唤醒函数*/ int proc_wakeup();/***以下是函数定义及注释***/ /*主函数*/ main(){ int i;/*初始化,创建进程3...
  • 17020472324 :求进程调度先来先服务算法,短进程优先算法完整c语言代码
    巢贸研2379 :答:/*(一)进程调度 进程调度算法有FIFO,优先数调度算法,时间片轮转调度算法,分级调度算法,输入:进程流文件,其中存储的是一系列要执行的进程,每个作业包括三个数据项:进程名 所需时间 优先数(0级最高)输出:进程执行...
  • 相关链接

    欢迎反馈与建议,请联系电邮
    2024 © 视觉网