模拟进程调度算法和c代码(进程调度的模拟实现)
admin 发布:2022-12-19 18:39 109
今天给各位分享模拟进程调度算法和c代码的知识,其中也会对进程调度的模拟实现进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、操作系统的作业,用C语言模拟先来先服务的方法的进程调度,请大神帮忙看看我这个代码有什么问题
- 2、用C语言编程模拟处理机调度(实现一种算法)
- 3、用C语言编写并调试一个模拟的进程调度程序,采用“简单时间片轮转法”调度算法对五个进程进行调度。
- 4、进程调度算法模拟程序设计
操作系统的作业,用C语言模拟先来先服务的方法的进程调度,请大神帮忙看看我这个代码有什么问题
假设你的系统是win7,而你参照的代码是在xp上面写的【就是调用xp的底层的接口】,有些会出现这种问题。。。你就行你安装一个不符合这个系统版本的软件,不是也是会提示类似的错误?
用C语言编程模拟处理机调度(实现一种算法)
#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 pcb* link;
}*ready=NULL,*p;
typedef struct pcb PCB;
void sort() /* 建立对进程进行优先级排列函数*/
{
PCB *first, *second;
int insert=0;
if((ready==NULL)||((p-super)(ready-super))) /*优先级最大者,插入队首*/
{
p-link=ready;
ready=p;
}
else /* 进程比较优先级,插入适当的位置中*/
{
first=ready;
second=first-link;
while(second!=NULL)
{
if((p-super)(second-super)) /*若插入进程比当前进程优先数大,*/
{ /*插入到当前进程前面*/
p-link=second;
first-link=p;
second=NULL;
insert=1;
}
else /* 插入进程优先数最低,则插入到队尾*/
{
first=first-link;
second=second-link;
}
}
if(insert==0) first-link=p;
}
}
void input() /* 建立进程控制块函数*/
{
int i,num;
system("cls"); /*清屏*/
printf("\n 请输入进程数: ");
scanf("%d",num);
for(i=1;i=num;i++)
{
printf("\n 进程号No.%d:\n",i);
p=getpch(PCB);
printf("\n 输入进程名:");
scanf("%s",p-name);
printf("\n 输入进程优先数:");
scanf("%d",p-super);
printf("\n 输入进程运行时间:");
scanf("%d",p-ntime);
printf("\n");
p-rtime=0;p-state='W';
p-link=NULL;
sort(); /* 调用sort函数*/
}
}
int space()
{
int l=0;
PCB* pr=ready;
while(pr!=NULL)
{
l++;
pr=pr-link;
}
return(l);
}
void disp(PCB * pr) /*建立进程显示函数,用于显示当前进程*/
{
printf("\n 进程名\t 状态\t 优先数\t 需要运行时间\t 已经运行时间\n");
printf("|%s\t",pr-name);
printf("|%c\t",pr-state);
printf("|%d\t",pr-super);
printf("|%d\t\t",pr-ntime);
printf("|%d\t",pr-rtime);
printf("\n");
}
void check() /* 建立进程查看函数 */
{
PCB* pr;
printf("\n **** 当前正在运行的进程是:\n"); /*显示当前运行进程*/
disp(p);
pr=ready;
printf("\n **** 当前就绪队列状态为:\n"); /*显示就绪队列状态*/
while(pr!=NULL)
{
disp(pr);
pr=pr-link;
}
}
void destroy() /*建立进程撤消函数(进程运行结束,撤消进程)*/
{
printf("\n 进程 [%s] 已完成.\n",p-name);
free(p);
}
void running() /* 建立进程就绪函数(进程运行时间到,置就绪状态*/
{
(p-rtime)++;
if(p-rtime==p-ntime)
destroy(); /* 调用destroy函数*/
else
{
(p-super)--;
p-state='W';
sort(); /*调用sort函数*/
}
}
void main() /*主函数*/
{
int len,h=0;
char ch;
input();
len=space();
while((len!=0)(ready!=NULL))
{
ch=getchar();
h++;
printf("-----------------------------------------------------");
printf("\n 现在是第%d次运行: \n",h);
p=ready;
ready=p-link;
p-link=NULL;
p-state='R';
check();
running();
printf("\n 按任意键继续......\n");
}
printf("\n\n 进程已经完成.\n");
}
用C语言编写并调试一个模拟的进程调度程序,采用“简单时间片轮转法”调度算法对五个进程进行调度。
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
struct PCB {
char NAME[10]; /*进程名*/
int ROUND; /*进程轮转时间片*/
int REACHTIME; /*进程到达时间*/
int CPUTIME; /*进程占用CPU时间*/
int COUNT; /*计数器*/
int NEEDTIME; /*进程完成还要的CPU时间*/
char STATE; /*进程的状态*/
struct PCB *NEXT; /*链指针*/
};
struct LINK { /*PCB的链结构*/
struct PCB *RUN; /*当前运行进程指针*/
struct PCB *READY; /*就绪队列头指针*/
struct PCB *TAIL; /*就绪队列尾指针*/
struct PCB *FINISH; /*完成队列头指针*/
};
void INIT(LINK *); /*对PCB的链结构初始化*/
void INSERT(LINK *); /*将执行了一个单位时间片数且还未完成的进程的PCB插到就绪队列的队尾*/
void FIRSTIN(LINK *); /*将就绪队列中的第一个进程投入运行*/
void PRINT(LINK *); /*打印每执行一个时间片后的所有进程的状态*/
void PR(PCB *); /*打印一个进程的状态*/
int CREATE(LINK *,int); /*创建新的进程*/
void ROUNDSCH(LINK *); /*按时间片轮转法调度进程*/
void main() {
LINK pcbs;
int i;
INIT(pcbs);
i=0;
printf("创建5个进程\n\n");
while(i5) {
if(CREATE(pcbs,i+1)==1) {
printf("进程已创建\n\n");
i++;
}
else
printf("进程创建失败\n\n");
}
FIRSTIN(pcbs);
ROUNDSCH(pcbs);
}
void ROUNDSCH(LINK *p) {
PCB *pcb;
while(p-RUN!=NULL) {
pcb=(PCB *)malloc(sizeof(PCB));
strcpy(pcb-NAME,p-RUN-NAME);
pcb-ROUND=p-RUN-ROUND;
pcb-REACHTIME=p-RUN-REACHTIME;
pcb-CPUTIME=p-RUN-CPUTIME;
pcb-COUNT=p-RUN-COUNT;
pcb-NEEDTIME=p-RUN-NEEDTIME;
pcb-STATE=p-RUN-STATE;
pcb-NEXT=p-RUN-NEXT;
pcb-CPUTIME++;
pcb-NEEDTIME--;
pcb-COUNT++;
if(pcb-NEEDTIME==0) {
pcb-NEXT=p-FINISH-NEXT;
p-FINISH-NEXT=pcb;
pcb-STATE='F';
p-RUN=NULL;
if(p-READY!=p-TAIL)
FIRSTIN(p);
}
else {
p-RUN=pcb;
if(pcb-COUNT==pcb-ROUND) {
pcb-COUNT=0;
if(p-READY!=p-TAIL) {
pcb-STATE='W';
INSERT(p);
FIRSTIN(p);
}
}
}
PRINT(p);
}
}
void INIT(LINK *p) {
p-RUN=NULL;
p-TAIL=p-READY=(PCB *)malloc(sizeof(PCB));
p-READY-NEXT=NULL;
p-FINISH=(PCB *)malloc(sizeof(PCB));
p-FINISH-NEXT=NULL;
}
int CREATE(LINK *p,int n) {
PCB *pcb,*q;
pcb=(PCB *)malloc(sizeof(PCB));
flushall();
printf("请输入第%d个进程的名称:\n",n);
gets(pcb-NAME);
printf("请输入第%d个进程的轮转时间片数:\n",n);
scanf("%d",(pcb-ROUND));
printf("请输入第%d个进程的到达时间:\n",n);
scanf("%d",(pcb-REACHTIME));
pcb-CPUTIME=0;
pcb-COUNT=0;
printf("请输入第%d个进程需运行的时间片数:\n",n);
scanf("%d",(pcb-NEEDTIME));
pcb-STATE='W';
pcb-NEXT=NULL;
if(strcmp(pcb-NAME,"")==0||pcb-ROUND=0||pcb-NEEDTIME=0) /*输入错误*/
return 0;
q=p-READY;
while(q-NEXT!=NULLq-NEXT-REACHTIME=pcb-REACHTIME)
q=q-NEXT;
pcb-NEXT=q-NEXT;
q-NEXT=pcb;
if(pcb-NEXT==NULL)
p-TAIL=pcb;
return 1;
}
void FIRSTIN(LINK *p) {
PCB *q;
q=p-READY-NEXT;
p-READY-NEXT=q-NEXT;
q-NEXT=NULL;
if(p-READY-NEXT==NULL)
p-TAIL=p-READY;
q-STATE='R';
p-RUN=q;
}
void INSERT(LINK *p) {
PCB *pcb;
pcb=(PCB *)malloc(sizeof(PCB));
strcpy(pcb-NAME,p-RUN-NAME);
pcb-ROUND=p-RUN-ROUND;
pcb-REACHTIME=p-RUN-REACHTIME;
pcb-CPUTIME=p-RUN-CPUTIME;
pcb-COUNT=p-RUN-COUNT;
pcb-NEEDTIME=p-RUN-NEEDTIME;
pcb-STATE=p-RUN-STATE;
pcb-NEXT=p-RUN-NEXT;
p-TAIL-NEXT=pcb;
p-TAIL=pcb;
p-RUN=NULL;
pcb-STATE='W';
}
void PRINT(LINK *p) {
PCB *pcb;
printf("执行一个时间片后的所有进程的状态:\n\n");
if(p-RUN!=NULL)
PR(p-RUN);
if(p-READY!=p-TAIL) {
pcb=p-READY-NEXT;
while(pcb!=NULL) {
PR(pcb);
pcb=pcb-NEXT;
}
}
pcb=p-FINISH-NEXT;
while(pcb!=NULL) {
PR(pcb);
pcb=pcb-NEXT;
}
}
void PR(PCB *p) {
printf("进程名:%s\n",p-NAME);
printf("进程轮转时间片:%d\n",p-ROUND);
printf("进程到达时间:%d\n",p-REACHTIME);
printf("进程占用CPU时间:%d\n",p-CPUTIME);
printf("计数器:%d\n",p-COUNT);
printf("进程完成还要的CPU时间:%d\n",p-NEEDTIME);
printf("进程的状态:%c\n\n",p-STATE);
}
进程调度算法模拟程序设计
public class PrivilegeProcess {
public static void main(String[] args) {
MyQueue myqueue = new MyQueue();//声明队列
PCB[] pcb = {new PCB(001,8,1),new PCB(002,7,9),new PCB(003,3,8),new PCB(004,1,7),new PCB(005,7,4)};
PCB para = new PCB();
for(int i=0;ipcb.length;i++){//初始化后首先执行一次排序,这里使用的是选择排序,优先级高的先入队
for(int j=i;jpcb.length;j++){
if(pcb[i].privilege pcb[j].privilege){
para = pcb[i];
pcb[i] = pcb[j];
pcb[j] = para;
}
}
}
System.out.println("初次入队后各进程的顺序:");
for(int i=0;ipcb.length;i++){
System.out.println("初次入队后 # processname : " + pcb[i].name + " totaltime : " + pcb[i].totaltime + " privilege :" + pcb[i].privilege);
}
System.out.println();
myqueue.start(pcb);
}
}
class MyQueue {
int index = 0;
PCB[] pc = new PCB[5];
PCB[] pc1 = new PCB[4];
PCB temp = new PCB();
public void enQueue(PCB process){//入队算法
if(index==5){
System.out.println("out of bounds !");
return;
}
pc[index] = process;
index++;
}
public PCB deQueue(){//出队算法
if(index==0)
return null;
for(int i=0;ipc1.length;i++){
pc1[i] = pc[i+1];
}
index--;
temp = pc[0];
for(int i=0;ipc1.length;i++){
pc[i] = pc1[i];
}
return temp;
}
public void start(PCB[] pc){//显示进程表算法
while(pc[0].isNotFinish==true||pc[1].isNotFinish==true||pc[2].isNotFinish==true||pc[3].isNotFinish==true||pc[4].isNotFinish==true){
//*注意:||运算符,所有表达式都为false结果才为false,否则为true
for(int i=0;ipc.length;i++){
pc[i].run(this);
}
System.out.println();
for(int i=0;ipc.length;i++){//所有进程每执行完一次时间片长度的运行就重新按优先级排列一次
for(int j=i;jpc.length;j++){
if(pc[i].privilege pc[j].privilege){
temp = pc[i];
pc[i] = pc[j];
pc[j] = temp;
}
}
}
}
}
}
class PCB {//声明进程类
int name,totaltime,runtime,privilege;
boolean isNotFinish;
public PCB(){
}
public PCB(int name, int totaltime, int privilege){
this.name = name;//进程名
this.totaltime = totaltime;//总时间
this.privilege = privilege;//优先级别
this.runtime = 2;//时间片,这里设值为2
this.isNotFinish = true;//是否执行完毕
System.out.println("初始值: processname : " + name + " totaltime : " + totaltime + " privilege :" + privilege );
System.out.println();
}
public void run (MyQueue mq){//进程的基于时间片的执行算法
if(totaltime1){
totaltime-=runtime;//在总时间大于1的时候,总时间=总时间-时间片
privilege--;
System.out.println(" processname : " + name + " remaintime : " + totaltime + " privilege :" + privilege );
}else if(totaltime==1){
totaltime--;//在总时间为1时,执行时间为1
privilege--;
System.out.println(" processname : " + name + " remaintime : " + totaltime + " privilege :" + privilege );
}else{
isNotFinish = false;//总时间为0,将isNotFinish标记置为false
}
if(isNotFinish==true){
mq.deQueue();
mq.enQueue(this);
}
}
}
关于模拟进程调度算法和c代码和进程调度的模拟实现的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
版权说明:如非注明,本站文章均为 AH站长 原创,转载请注明出处和附带本文链接;
相关推荐
- 05-14网站怎么设计,网站怎么设计怎么实现的
- 05-09网页代码,网页代码快捷键
- 05-06单页网站的代码(完整的网页代码)[20240506更新]
- 05-06个人主页图片代码(个人主页图片代码怎么弄)[20240506更新]
- 05-06提取微信名片代码(微信名片信息提取)[20240506更新]
- 05-06php后台权限管理代码(php管理员权限)[20240506更新]
- 05-06付费观看代码php(付费观看代码)[20240506更新]
- 05-06在线html执行代码(html怎么运行)[20240506更新]
- 05-06源代码管理资源管理器(资源管理器运行代码)[20240506更新]
- 05-06代码源软件库(程序代码库)[20240506更新]
取消回复欢迎 你 发表评论:
- 标签列表
- 最近发表
- 友情链接