谁能用c/c++实现操作系统中的进程管理问题!高手请进!!!100分,不够再加。

shixiaoxiang 2002-10-17 05:30:43
[目的]
1、熟练掌握各种进程调度的算法
2、熟练优先权法及时间片轮转法的具体实现方法
3、强化编写模块化程序的能力
[要求]
1、设计有n个进程的程序,其中每个进程控制块pcb中包含:
进程名(ID),进程优先数(Priority),进程进入cpu的时间(In),程序需要运行的时间(Alltime),队列指针。
2、调度算法一:采用基于静态优先级的立即抢占式调度算法。
进程的调度文件存放在jc.txt文件中(通过两种情况验证算法)
文件内容(1):
ID Priority In Alltime
1 2 8:11 120
2 3 9:00 50
3 4 9:20 30

文件内容(2):
ID Priority In Alltime
1 2 8:11 120
2 1 9:00 50
3 4 9:20 30

3、调度算法二:采用时间片轮转法,进程占用cpu的时间片有每个进程的优先数来确定。(约定:每个优先数对应10分钟)
4、系统显示各进程状态的变化情况,调度序列及每个进程获得cpu的调度时间。
5、编写中应将算法单独编写为尽量通用的独立模块。
...全文
384 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaoniaoleyuan 2002-12-28
  • 打赏
  • 举报
回复
mark
shixiaoxiang 2002-10-29
  • 打赏
  • 举报
回复
vc++6.0调试通过!
shixiaoxiang 2002-10-29
  • 打赏
  • 举报
回复
调度算法二:采用时间片轮转法
#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>
struct pcb_list
{
int ID;
int Priority;
int In1;
int In2;
int Alltime;
struct pcb_list *next;
}; //PCB结构体
pcb_list *CreatePcb()
{
FILE *fp;
pcb_list *head,*s,*r;
if((fp=fopen("jc.txt","r"))==NULL) //打开文件
{
cout<<"Can't open this file!"<<endl;
exit(0);
}
s=(pcb_list*)malloc(sizeof(pcb_list)); //分配节点空间
head=r=NULL;
fscanf(fp,"%d%d%d%d%d",&s->ID,&s->Priority,&s->In1,&s->In2,&s->Alltime); //读文件
while(s->ID!=3)
{
if(head==NULL)head=s;
else r->next=s;
r=s;
s=(pcb_list*)malloc(sizeof(pcb_list));
fscanf(fp,"%d%d%d%d%d",&s->ID,&s->Priority,&s->In1,&s->In2,&s->Alltime);
}
r->next=s;r=s;
if(r!=NULL)
r->next=NULL;
fclose(fp);
return head; //返回头指针
}


void Scheduling(struct pcb_list *head) //时间片轮转法调度
{ pcb_list *p,*q,*r;
p=head; q=p->next;r=q->next;//头节点
int flag1=0; //进程是否调度完的标志,0-未完,1-完
int flag2=0;
int flag3=0;
int time1=0;int time2=0;int time3=0;//时间累加器
int t1=p->In1*60+p->In2;
int t2=q->In1*60+q->In2;
int t3=r->In1*60+r->In2;

while(t1<t2&&flag1==0)
{ cout<<t1/60<<":"<<t1%60<<"~";
time1+=p->Priority*10;
t1+=p->Priority*10;
cout<<t1/60<<":"<<t1%60<<" "<<p->Priority*10<<" "<<p->ID<<endl;
if(time1<p->Alltime) flag1=0;
else {flag1=1;cout<<"Process 1 is completed!"<<endl;}
}

while(t1<t3&&flag2==0)
{ cout<<t1/60<<":"<<t1%60<<"~";
time2+=q->Priority*10;
t1+=q->Priority*10;
cout<<t1/60<<":"<<t1%60<<" "<<q->Priority*10<<" "<<q->ID<<endl;
if(time2<q->Alltime) flag2=0;
else {flag2=1;cout<<"Process 2 is completed!"<<endl;}
}

while(t1>=(r->In1*60+r->In2)&&flag3==0)
{ cout<<t1/60<<":"<<t1%60<<"~";
time3+=r->Priority*10;
t1+=r->Priority*10;
cout<<t1/60<<":"<<t1%60<<" "<<r->Priority*10<<" "<<r->ID<<endl;
if(time3<r->Alltime) flag3=0;
else {flag3=1;cout<<"Process 3 is completed!"<<endl;}
}

while(flag1!=1||flag2!=1||flag3!=1)
{ if(flag1!=1)
{cout<<t1/60<<":"<<t1%60<<"~";
time1+=p->Priority*10;
t1+=p->Priority*10;
cout<<t1/60<<":"<<t1%60<<" "<<p->Priority*10<<" "<<p->ID<<endl;
if(time1<p->Alltime) flag1=0;
else {flag1=1;cout<<"Process 1 is completed!"<<endl;}
}
if(flag2!=1)
{cout<<t1/60<<":"<<t1%60<<"~";
time2+=q->Priority*10;
t1+=q->Priority*10;
cout<<t1/60<<":"<<t1%60<<" "<<q->Priority*10<<" "<<q->ID<<endl;
if(time2<q->Alltime) flag2=0;
else {flag2=1;cout<<"Process 2 is completed!"<<endl;}
}
if(flag3!=1)
{cout<<t1/60<<":"<<t1%60<<"~";
time3+=r->Priority*10;
t1+=r->Priority*10;
cout<<t1/60<<":"<<t1%60<<" "<<r->Priority*10<<" "<<r->ID<<endl;
if(time3<r->Alltime) flag3=0;
else {flag3=1;cout<<"Process 3 is completed!"<<endl;}
}
}
}//scheduling



void main()
{pcb_list *h;
h=CreatePcb();
Scheduling(h);
}
上面两个程序都能达到基本要求
有谁能改进,送分!
shixiaoxiang 2002-10-29
  • 打赏
  • 举报
回复
我自己的程序:
调度算法一:采用基于静态优先级的立即抢占式调度算法
#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>
struct pcb_list
{
int ID;
int Priority;
int In1;
int In2;
int Alltime;
struct pcb_list *next;
}; //PCB结构体
pcb_list *CreatePcb()
{
FILE *fp;
pcb_list *head,*s,*r;
if((fp=fopen("jc.txt","r"))==NULL) //打开文件
{
cout<<"Can't open this file!"<<endl;
exit(0);
}
s=(pcb_list*)malloc(sizeof(pcb_list)); //分配节点空间
head=r=NULL;
fscanf(fp,"%d%d%d%d%d",&s->ID,&s->Priority,&s->In1,&s->In2,&s->Alltime); //读文件
while(s->ID!=3)
{
if(head==NULL)head=s;
else r->next=s;
r=s;
s=(pcb_list*)malloc(sizeof(pcb_list));
fscanf(fp,"%d%d%d%d%d",&s->ID,&s->Priority,&s->In1,&s->In2,&s->Alltime);
}
r->next=s;r=s;
if(r!=NULL)
r->next=NULL;
fclose(fp);
return head; //返回头指针
}


void Scheduling(struct pcb_list *head) //基于静态优先级的立即抢占式调度
{ pcb_list *p,*q,*r;
p=head; q=p->next;r=q->next;//头节点

if(p->Priority<q->Priority) //第一个进程的优先级小于第二个进程的优先级
{ int time1=(q->In1*60+q->In2)-(p->In1*60+p->In2);
cout<<p->In1<<":"<<p->In2<<"~"<<q->In1<<":"<<q->In2<<'0'<<" "<<time1<<" "<<p->ID<<endl;
if(q->Priority<r->Priority) //第二个进程的优先级小于第三个进程的优先级
{ int time2=(r->In1*60+r->In2)-(q->In1*60+q->In2);
int t1=(r->In1*60+r->In2+r->Alltime)/60;
int t2=(r->In1*60+r->In2+r->Alltime)%60;
cout<<q->In1<<":"<<q->In2<<'0'<<"~"<<r->In1<<":"<<r->In2<<" "<<time2<<" "<<q->ID<<endl;
cout<<r->In1<<":"<<r->In2<<"~"<<t1<<":"<<t2<<" "<<r->Alltime<<" "<<r->ID<<endl;
int t5=q->Alltime-time2;
int t3=(t1*60+t2+t5)/60;
int t4=(t1*60+t2+t5)%60;
cout<<t1<<":"<<t2<<"~"<<t3<<":"<<t4<<" "<<t5<<" "<<q->ID<<endl;
int t6=(p->In1*60+p->In2+p->Alltime+q->Alltime+r->Alltime)/60;
int t7=(p->In1*60+p->In2+p->Alltime+q->Alltime+r->Alltime)%60;
cout<<t3<<":"<<t4<<"~"<<t6<<":"<<t7<<" "<<p->Alltime-time1<<" "<<p->ID<<endl;
}//if
else
{ //第二个进程的优先级大于第三个进程的优先级
int q1=(q->In1*60+q->In2+q->Alltime)/60;
int q2=(q->In1*60+q->In2+q->Alltime)%60;
//打印第二个进程的完整信息
cout<<q->In1<<":"<<q->In2<<'0'<<"~"<<q1<<":"<<q2<<" "<<q->Alltime<<" "<<q->ID<<endl;
if(p->Priority>r->Priority)
{ //第一个进程的优先级大于第三个进程的优先级
int m1=(q1*60+q2+p->Alltime-time1)/60;
int m2=(q1*60+q2+p->Alltime-time1)%60;
cout<<q1<<":"<<q2<<"~"<<m1<<":"<<m2<<" "<<p->Alltime-time1<<" "<<p->ID<<endl;
int m3=(m1*60+m2+r->Alltime)/60;
int m4=(m1*60+m2+r->Alltime)%60;
cout<<m1<<":"<<m2<<"~"<<m3<<":"<<m4<<" "<<r->Alltime<<" "<<r->ID<<endl;
}
else
{ //第一个进程的优先级小于第三个进程的优先级
int q3=(q1*60+q2+r->Alltime)/60;
int q4=(q1*60+q2+r->Alltime)%60;
cout<<q1<<":"<<q2<<"~"<<q3<<":"<<q4<<" "<<r->Alltime<<" "<<r->ID<<endl;
int t6=(p->In1*60+p->In2+p->Alltime+q->Alltime+r->Alltime)/60;
int t7=(p->In1*60+p->In2+p->Alltime+q->Alltime+r->Alltime)%60;
cout<<q3<<":"<<q4<<"~"<<t6<<":"<<t7<<" "<<p->Alltime-time1<<" "<<p->ID<<endl;

}
}//else

}//if
else if(p->Priority<r->Priority) //第一个进程的优先级大于第二个进程的优先级
{ // 且第一个进程的优先级小于第三个进程的优先级
int time3=r->In1*60+r->In2-p->In1*60-p->In2;//69
cout<<p->In1<<":"<<p->In2<<"~"<<r->In1<<":"<<r->In2<<" "<<time3<<" "<<p->ID<<endl;
int a1=(r->In1*60+r->In2+r->Alltime)/60;
int a2=(r->In1*60+r->In2+r->Alltime)%60;
cout<<r->In1<<":"<<r->In2<<"~"<<a1<<":"<<a2<<" "<<r->Alltime<<" "<<r->ID<<endl;
int a3=(a1*60+a2+p->Alltime-time3)/60;
int a4=(a1*60+a2+p->Alltime-time3)%60;
cout<<a1<<":"<<a2<<"~"<<a3<<":"<<a4<<" "<<p->Alltime-time3<<" "<<p->ID<<endl;
int a5=(p->In1*60+p->In2+p->Alltime+q->Alltime+r->Alltime)/60;
int a6=(p->In1*60+p->In2+p->Alltime+q->Alltime+r->Alltime)%60;
cout<<a3<<":"<<a4<<"~"<<a5<<":"<<a6<<" "<<q->Alltime<<" "<<q->ID<<endl;
}
else
{ //第一个进程的优先级最大
int p1=(p->In1*60+p->In2+p->Alltime)/60;
int p2=(p->In1*60+p->In2+p->Alltime)%60;
//打印第一个进程的完整信息
cout<<p->In1<<":"<<p->In2<<"~"<<p1<<":"<<p2<<" "<<p->Alltime<<" "<<p->ID<<endl;
if(q->Priority>r->Priority) //第二个进程的优先级大于第三个进程的优先级
{
//打印第二个进程的完整信息
int r1=(p1*60+p2+q->Alltime)/60;
int r2=(p1*60+p2+q->Alltime)%60;
cout<<p1<<":"<<p2<<"~"<<r1<<":"<<r2<<" "<<q->Alltime<<" "<<q->ID<<endl;
//打印第三个进程的完整信息
int r3=(p->In1*60+p->In2+p->Alltime+q->Alltime+r->Alltime)/60;
int r4=(p->In1*60+p->In2+p->Alltime+q->Alltime+r->Alltime)%60;
cout<<r1<<":"<<r2<<"~"<<r3<<":"<<r4<<" "<<r->Alltime<<" "<<r->ID<<endl;

}
else
{ //第二个进程的优先级小于第三个进程的优先级
int p3=(p1*60+p2+r->Alltime)/60;
int p4=(p1*60+p2+r->Alltime)%60;
//打印第三个进程的完整信息
cout<<p1<<":"<<p2<<"~"<<p3<<":"<<p4<<" "<<r->Alltime<<" "<<r->ID<<endl;
int p5=(p->In1*60+p->In2+p->Alltime+q->Alltime+r->Alltime)/60;
int p6=(p->In1*60+p->In2+p->Alltime+q->Alltime+r->Alltime)%60;
//打印第二个进程的完整信息
cout<<p3<<":"<<p4<<"~"<<p5<<":"<<p6<<" "<<q->Alltime<<" "<<q->ID<<endl;

}
}


}//Scheduling


void main()
{pcb_list *h;
h=CreatePcb();
Scheduling(h);
}
hellomartin 2002-10-28
  • 打赏
  • 举报
回复
是南大00级计算机的实验课吧?不是已经解决了吗?找同学问一下不久行了吗?
970361 2002-10-28
  • 打赏
  • 举报
回复
哈哈看看linux的代码,或者是别人的分析
shixiaoxiang 2002-10-22
  • 打赏
  • 举报
回复
楼上的同志说的我不解,可以详细一些吗?
solar 2002-10-22
  • 打赏
  • 举报
回复
可以用多线程模拟多个进程,主线程作调度功能;也可以多个进程来模拟,主进程与各被调度进程通过IPC通信
iamltd 2002-10-22
  • 打赏
  • 举报
回复
最简单的就是一个循环
判断子程序运行条件,在这儿使用调度算法

在子程序中进行绘图,出效果
shixiaoxiang 2002-10-22
  • 打赏
  • 举报
回复
拜托,帮帮忙啊!!!
rainy14f 2002-10-21
  • 打赏
  • 举报
回复
哈哈,我们去年学操作系统时也是这样的上机题,不过是用公共汽车来代表进程,显示的时候就很形象。还是自己写吧,我的源代码估计现在只有老师那里有了
LvYou 2002-10-21
  • 打赏
  • 举报
回复
我也做过,不过要求稍微简单一点,我不是用C++写的,而是用JScript在网页中做的,如果觉得有价值的话可以参考:
http://www.gamefps.com/~gamefps/lvyou/ScriptOrigin/products/ProcessRoute.html
myall2002 2002-10-18
  • 打赏
  • 举报
回复
是做个模拟吧!
以前我做过!代码都不知放那了!
详细理解一下它的真正含义就可以做啦!
这样对你有好处呀!
shixiaoxiang 2002-10-17
  • 打赏
  • 举报
回复
没有高手吗???

如果提供正确的原代码,立马送100分!
shixiaoxiang 2002-10-17
  • 打赏
  • 举报
回复
是啊
老师布置的作业
暂时想听听高手的意见
lbaby 2002-10-17
  • 打赏
  • 举报
回复
去看一下linux的源代码分析
zhujiaqi 2002-10-17
  • 打赏
  • 举报
回复
哥们,你是不是也在上操作系统啊?
我也是,课上讲的很模糊,希望能探讨一下。
sjd163 2002-10-17
  • 打赏
  • 举报
回复
向你学习

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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