求助各位大佬!操作系统——非抢占式的优先级调度算法

qq_37925974 2017-11-18 07:58:21
各位大佬!有木有人知道如何利用指针实现非抢占式的优先级调度算法啊?就是利用下面的时间片轮转调度算法代码修改成实现非抢占式的优先级调度算法功能的代码。我就是不太会用指针诶,用数组的话,这个的代码我是写出来了,可是它要求要用下面的代码修改成实现非抢占式的优先级调度算法功能的代码,我就有点懵了,操作了很久,就是不能完全对,想来求助吧里大佬,看看有谁能帮我看看的
这儿下面就是那个时间片轮转调度算法代码的main.cpp里的内容:
#define N 20
#include <stdio.h>
#include <conio.h>
typedef struct pcb /*进程控制块定义*/
{
char pname[N]; /*进程名*/
int runtime; /*运行时间*/
int arrivetime; /*到达时间*/
char state; /*进程状态*/
struct pcb *next; /*链接指针*/
}PCB;
PCB head_input;
PCB head_run;
PCB *pcb_input;
static char R='r',C='c';
unsigned long current; /*记录系统当前时间的变量*/
void inputprocess(); /*建立进程函数*/
int readyprocess(); /*建立就绪队列函数*/
int readydata(); /*判断进程是否就绪函数*/
int runprocess(); /*运行进程函数*/
FILE *f;
/*定义建立就绪队列函数*/
int readyprocess()
{
while(1)
{
if(readydata()==0)/*判断是否有就绪进程入队*/
return 1;
else
runprocess(); /*运行进程*/
}
}
/*定义判断就绪队列是否有进程*/
int readydata()
{
if(head_input.next==NULL)
{
if(head_run.next==NULL)
return 0;
else
return 1;
}


PCB *p1,*p2,*p3;
p1=head_run.next;
p2=&head_run;
while(p1!=NULL)
{ p2=p1; p1=p2->next; }
p1=p2;
p3=head_input.next;
p2=&head_input;
while(p3!=NULL)
{
if(((unsigned long)p3->arrivetime<=current)&&(p3->state==R))
{
printf("Time slice is%d(time%4d);Process %s start.\n",
current,(current+500)/1000,p3->pname);
fprintf(f,"Time slice is%8d(time%4d);Process%s start.\n",
current,(current+500)/1000,p3->pname);
p2->next=p3->next;
p3->next=p1->next;
p1->next=p3;
p3=p2;//???不应该是p3=p2->next???
}
p2=p3;
p3=p3->next;
}
return 1;
}

int runprocess() /*运行进程*/
{
PCB *p1,*p2;
if(head_run.next==NULL)
{
current++;
return 1;
}
else
{
p1=head_run.next;
p2=&head_run;
while(p1!=NULL)
{
p1->runtime--;
current++;
if(p1->runtime<=0)
{
printf("Time slice is%8d time%4d;Process%s end.\n",current,
(current+500)/1000,p1->pname);
fprintf(f,"Time slice is%8d time%4d;Process%s end.\n",current,
(current+500)/1000,p1->pname);
p1->state=C;
p2->next= p1->next;
delete p1;
p1=NULL;
}
else
{
p2=p1;
p1=p2->next;
}
}
return 1;
}
}
/*定义建立进程函数*/
void inputprocess()
{
PCB *p1,*p2;
int num;
/*要建立的进程数*/
unsigned long max=0;
printf("How many processes do you want to run:");
fprintf(f,"How many processes do you want to run:");
scanf("%d",&num);
fprintf(f,"%d\n",&num);
p1=&head_input;
p2=p1;
p1->next=new PCB;
p1=p1->next;
for(int i=0;i<num;i++)
{
printf("No.%3d process input pname:",i+1);
fprintf(f,"No.%3d process input pname:",i+1);
scanf("%s",p1->pname);
fprintf(f,"%s\n",p1->pname);
printf(" runtime:");
fprintf(f," runtime:");
scanf("%d",&(p1->runtime));
fprintf(f,"%d\n",&(p1->runtime));
printf(" arrivetime:");
fprintf(f," arrivetime:");
scanf("%d",&(p1->arrivetime));
fprintf(f,"%d\n",&(p1->arrivetime));
p1->runtime=(p1->runtime)*1000;
p1->arrivetime=(p1->arrivetime)*1000;
p1->state=R;
if((unsigned long)(p1->arrivetime)>max)
max=p1->arrivetime;
p1->next=new PCB;
p2=p1;
p1=p1->next;
}
delete p1;
p1=NULL;
p2->next=NULL;
}
/*一一定义建立进程函数*/
void inputprocess1()
{
PCB *p1,*p2;
int num; /*要建立的进程数*/
unsigned long max=0;
printf("How many processes do you want to run:");
fprintf(f,"How many processes do you want to run:");
scanf("%d",&num);
fprintf(f,"%d\n",&num);
pcb_input=new PCB;
p1=pcb_input;
for(int i=0;i<num;i++)
{
printf("No.%3d process input pname:",i+1);
fprintf(f,"No.%3d process input pname:",i+1);
scanf("%s",p1->pname);
fprintf(f,"%s\n",p1->pname);
printf(" runtime:");
fprintf(f," runtime:");
scanf("%d",&(p1->runtime));
fprintf(f,"%d\n",&(p1->runtime));
printf(" arrivetime:");
fprintf(f," arrivetime:");
scanf("%d,",&(p1->arrivetime));
fprintf(f,"%d\n",&(p1->arrivetime));
// p1->runtime=(p1->runtime):
// p1->arrivetime=(p1->arrivetime);
p1->runtime=(p1->runtime)*1000;
p1->arrivetime=(p1->arrivetime)*1000;
p1->state=R;
if(i!=num-1)
{ p1->next=new PCB; p1=p1->next;}
else
{ p1->next=pcb_input; }
}
p1=pcb_input;
while(p1->next!=pcb_input)
{
printf("Process name is%s\n",p1->pname);
fprintf(f,"process name is%s\n",p1->pname);
p1=p1->next;
}
printf("Proeess name is%s\n",p1->pname);
fprintf(f,"process name is%s\n",p1->pname);
}

void runprocessl() /*定义运行进程函数*/
{
pcb *pre,*cur;
if(pcb_input==NULL)
return ;
else
{
cur=pcb_input;
pre=cur->next;
while(pre->next!=cur) // find the last node in the list
{ pre=pre->next; }
while(cur->runtime>=0||(cur->next!=cur))
// while(1)
//if p1->next!=p1,it means that there are more than one pcb jobs
//if p1->next==p1 and p1->runtime<=O means a11 jobs have done;
{
if(current<(unsigned long)cur->arrivetime)
{ pre=cur; cur=cur->next;}
else
{
if( current==(unsigned long)cur->arrivetime)
{
printf("Time slice is %d(time&4d);Process %s start,\n",
current,(current+500)/1000,cur->pname);
fprintf(f,"Time slice is %8d(time%4d);Process %s start,\n",
current,(current+500)/1000,cur->pname);
}
cur->runtime--;
if(cur->runtime<0) //means the job have ended.
{
printf("Time slice is %8d time %4d;Process %s end.\n",
current,(current+500)/1000,cur->pname);
fprintf(f,"Time slice is %8d time %4d:Process %s end.\n",
current,(current+500)/1000,cur->pname);
if(cur==cur->next) //delete the last job then break;
{
delete cur;
cur=NULL;
//break;
return;
}
else
{
pre->next=cur->next;
pcb *tmp=cur;
delete tmp;
cur=pre->next;
}
}
else
{
cur->runtime--;
pre=cur;
cur=cur->next;
}
}
current++;
}}}
// To Do set one cycle link to solve the problem
int main()
{
f=fopen("result.txt","w");
printf("\ntime 1=1000 time slice\n");
fprintf(f,"\ntime 1=1000 time slice\n");
current=0;
inputprocess();
readyprocess();
// inputprocessl();
// runprocessl();
getch();
fclose(f);
}
...全文
857 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2017-11-21
  • 打赏
  • 举报
回复
百度搜相关关键字。
### 课程介绍业务安全的基石是APP安全,APP安全的基石是操作系统安全,操作系统安全的基石是SecureBoot安全。不理解SecureBoot安全无法深入理解业务的安全性,也无法进行良好的安全业务设计。 SecureBOOT安全涉及方方面面的知识,掌握Secureboot后,对整个大系统就有了一个宏观的概念,对整个大系统的其它模块学习都有帮助。Secureboot也是一个高门槛的知识点,涉及到密码学基础知识、ARM硬件基础知识、SOC硬件基础知识(如SOC内集成的efuse、crypto engine、RPMB等)、BootRom的实现、emmc/ufs/nand/nor等知识、各种规范和标准、ATF(TF-A)等软件、安全业务的设计模.....所以当我们充分理解了Secureboot,再回头学习其它知识点是,就会变得比较容易. 本课程包含但不限于以下章节1、课程介绍2、基础知识点扫盲(密码学算法,efuse,RPMB等)3、启动流程bl1--bl314、一种超简单的secureboot(不带证书)5、一种官方推荐的secureboot(不带证书)6、官方推荐的secureboot--代码导读7、引入encrypted image后8、efuse也不安全怎么办9、Android Verified Boot(AVB)10、TEE验证TA程序的设计11、总结### 你将收获1、掌握和深刻理解整个Secureboot基本原理;2、能够自行进行SecureBoot的设计;3、掌握安全基础知识;### 适用人群1、学生、入门级人群2、资深工程师,立志在arm领域、芯片领域、系统领域、嵌入领域、安全领域、TEE领域长期发展的; 3、也适合行业大佬专家,听一听我的剖析和见解,查缺补漏4、对于嵌入领域/linux kernel领域资深工程师,也常适合,学习一下主流软件框架。5、也适合ASIC硬件同学,该课程涉及很多方面的软硬件结合。

64,652

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧