當前位置:首頁 » 編程語言 » 調度演算法c語言

調度演算法c語言

發布時間: 2024-09-23 15:17:42

『壹』 時間片輪轉演算法和優先順序調度演算法 c語言模擬實現

一、目的和要求
進程調度是處理機管理的核心內容。本實驗要求用高級語言編寫模擬進程調度程序,以便加深理解有關進程式控制制快、進程隊列等概念,並體會和了解優先數演算法和時間片輪轉演算法的具體實施辦法。
二、實驗內容
1.設計進程式控制制塊PCB的結構,通常應包括如下信息:
進程名、進程優先數(或輪轉時間片數)、進程已佔用的CPU時間、進程到完成還需要的時間、進程的狀態、當前隊列指針等。
2.編寫兩種調度演算法程序:
優先數調度演算法程序
循環輪轉調度演算法程序
3.按要求輸出結果。
三、提示和說明
分別用兩種調度演算法對伍個進程進行調度。每個進程可有三種狀態;執行狀態(RUN)、就緒狀態(READY,包括等待狀態)和完成狀態(FINISH),並假定初始狀態為就緒狀態。
(一)進程式控制制塊結構如下:
NAME——進程標示符
PRIO/ROUND——進程優先數/進程每次輪轉的時間片數(設為常數2)
CPUTIME——進程累計佔用CPU的時間片數
NEEDTIME——進程到完成還需要的時間片數
STATE——進程狀態
NEXT——鏈指針
註:
1.為了便於處理,程序中進程的的運行時間以時間片為單位進行計算;
2.各進程的優先數或輪轉時間片數,以及進程運行時間片數的初值,均由用戶在程序運行時給定。
(二)進程的就緒態和等待態均為鏈表結構,共有四個指針如下:
RUN——當前運行進程指針
READY——就需隊列頭指針
TAIL——就需隊列尾指針
FINISH——完成隊列頭指針
(三)程序說明
1. 在優先數演算法中,進程優先數的初值設為:
50-NEEDTIME
每執行一次,優先數減1,CPU時間片數加1,進程還需要的時間片數減1。
在輪轉法中,採用固定時間片單位(兩個時間片為一個單位),進程每輪轉一次,CPU時間片數加2,進程還需要的時間片數減2,並退出CPU,排到就緒隊列尾,等待下一次調度。
2. 程序的模塊結構提示如下:
整個程序可由主程序和如下7個過程組成:
(1)INSERT1——在優先數演算法中,將尚未完成的PCB按優先數順序插入到就緒隊列中;
(2)INSERT2——在輪轉法中,將執行了一個時間片單位(為2),但尚未完成的進程的PCB,插到就緒隊列的隊尾;
(3)FIRSTIN——調度就緒隊列的第一個進程投入運行;
(4)PRINT——顯示每執行一次後所有進程的狀態及有關信息。
(5)CREATE——創建新進程,並將它的PCB插入就緒隊列;
(6)PRISCH——按優先數演算法調度進程;
(7)ROUNDSCH——按時間片輪轉法調度進程。
主程序定義PCB結構和其他有關變數。
(四)運行和顯示
程序開始運行後,首先提示:請用戶選擇演算法,輸入進程名和相應的NEEDTIME值。
每次顯示結果均為如下5個欄位:
name cputime needtime priority state
註:
1.在state欄位中,"R"代表執行態,"W"代表就緒(等待)態,"F"代表完成態。
2.應先顯示"R"態的,再顯示"W"態的,再顯示"F"態的。
3.在"W"態中,以優先數高低或輪轉順序排隊;在"F"態中,以完成先後順序排隊。

view plain to clipboardprint?
/*
操作系統實驗之時間片輪轉演算法和優先順序調度演算法
By Visual C++ 6.0
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node
{
char name[20]; /*進程的名字*/
int prio; /*進程的優先順序*/
int round; /*分配CPU的時間片*/
int cputime; /*CPU執行時間*/
int needtime; /*進程執行所需要的時間*/
char state; /*進程的狀態,W——就緒態,R——執行態,F——完成態*/
int count; /*記錄執行的次數*/
struct node *next; /*鏈表指針*/
}PCB;
PCB *ready=NULL,*run=NULL,*finish=NULL; /*定義三個隊列,就緒隊列,執行隊列和完成隊列*/
int num;
void GetFirst(); /*從就緒隊列取得第一個節點*/
void Output(); /*輸出隊列信息*/
void InsertPrio(PCB *in); /*創建優先順序隊列,規定優先數越小,優先順序越高*/
void InsertTime(PCB *in); /*時間片隊列*/
void InsertFinish(PCB *in); /*時間片隊列*/
void PrioCreate(); /*優先順序輸入函數*/
void TimeCreate(); /*時間片輸入函數*/
void Priority(); /*按照優先順序調度*/
void RoundRun(); /*時間片輪轉調度*/
int main(void)
{
char chose;
printf("請輸入要創建的進程數目:\n");
scanf("%d",&num);
getchar();
printf("輸入進程的調度方法:(P/R)\n");
scanf("%c",&chose);
switch(chose)
{
case 'P':
case 'p':
PrioCreate();
Priority();
break;
case 'R':
case 'r':
TimeCreate();
RoundRun();
break;
default:break;
}
Output();
return 0;
}
void GetFirst() /*取得第一個就緒隊列節點*/
{
run = ready;

if(ready!=NULL)
{
run ->state = 'R';
ready = ready ->next;
run ->next = NULL;
}
}
void Output() /*輸出隊列信息*/
{
PCB *p;
p = ready;
printf("進程名\t優先順序\t輪數\tcpu時間\t需要時間\t進程狀態\t計數器\n");
while(p!=NULL)
{
printf("%s\t%d\t%d\t%d\t%d\t\t%c\t\t%d\n",p->name,p->prio,p->round,p->cputime,p->needtime,p->state,p->count);
p = p->next;
}
p = finish;
while(p!=NULL)
{
printf("%s\t%d\t%d\t%d\t%d\t\t%c\t\t%d\n",p->name,p->prio,p->round,p->cputime,p->needtime,p->state,p->count);
p = p->next;
}
p = run;
while(p!=NULL)
{
printf("%s\t%d\t%d\t%d\t%d\t\t%c\t\t%d\n",p->name,p->prio,p->round,p->cputime,p->needtime,p->state,p->count);
p = p->next;
}
}
void InsertPrio(PCB *in) /*創建優先順序隊列,規定優先數越小,優先順序越低*/
{
PCB *fst,*nxt;
fst = nxt = ready;

if(ready == NULL) /*如果隊列為空,則為第一個元素*/
{
in->next = ready;
ready = in;
}
else /*查到合適的位置進行插入*/
{
if(in ->prio >= fst ->prio) /*比第一個還要大,則插入到隊頭*/
{
in->next = ready;
ready = in;
}
else
{
while(fst->next != NULL) /*移動指針查找第一個別它小的元素的位置進行插入*/
{
nxt = fst;
fst = fst->next;
}

if(fst ->next == NULL) /*已經搜索到隊尾,則其優先順序數最小,將其插入到隊尾即可*/
{
in ->next = fst ->next;
fst ->next = in;
}
else /*插入到隊列中*/
{
nxt = in;
in ->next = fst;
}
}
}
}
void InsertTime(PCB *in) /*將進程插入到就緒隊列尾部*/
{
PCB *fst;
fst = ready;

if(ready == NULL)
{
in->next = ready;
ready = in;
}
else
{
while(fst->next != NULL)
{
fst = fst->next;
}
in ->next = fst ->next;
fst ->next = in;
}
}
void InsertFinish(PCB *in) /*將進程插入到完成隊列尾部*/
{
PCB *fst;
fst = finish;

if(finish == NULL)
{
in->next = finish;
finish = in;
}
else
{
while(fst->next != NULL)
{
fst = fst->next;
}
in ->next = fst ->next;
fst ->next = in;
}
}
void PrioCreate() /*優先順序調度輸入函數*/
{
PCB *tmp;
int i;

printf("輸入進程名字和進程所需時間:\n");
for(i = 0;i < num; i++)
{
if((tmp = (PCB *)malloc(sizeof(PCB)))==NULL)
{
perror("malloc");
exit(1);
}
scanf("%s",tmp->name);
getchar(); /*吸收回車符號*/
scanf("%d",&(tmp->needtime));
tmp ->cputime = 0;
tmp ->state ='W';
tmp ->prio = 50 - tmp->needtime; /*設置其優先順序,需要的時間越多,優先順序越低*/
tmp ->round = 0;
tmp ->count = 0;
InsertPrio(tmp); /*按照優先順序從高到低,插入到就緒隊列*/
}
}
void TimeCreate() /*時間片輸入函數*/
{
PCB *tmp;
int i;

printf("輸入進程名字和進程時間片所需時間:\n");
for(i = 0;i < num; i++)
{
if((tmp = (PCB *)malloc(sizeof(PCB)))==NULL)
{
perror("malloc");
exit(1);
}
scanf("%s",tmp->name);
getchar();
scanf("%d",&(tmp->needtime));
tmp ->cputime = 0;
tmp ->state ='W';
tmp ->prio = 0;
tmp ->round = 2; /*假設每個進程所分配的時間片是2*/
tmp ->count = 0;
InsertTime(tmp);
}
}
void Priority() /*按照優先順序調度,每次執行一個時間片*/
{
int flag = 1;

GetFirst();
while(run != NULL) /*當就緒隊列不為空時,則調度進程如執行隊列執行*/
{
Output(); /*輸出每次調度過程中各個節點的狀態*/
while(flag)
{
run->prio -= 3; /*優先順序減去三*/
run->cputime++; /*CPU時間片加一*/
run->needtime--;/*進程執行完成的剩餘時間減一*/
if(run->needtime == 0)/*如果進程執行完畢,將進程狀態置為F,將其插入到完成隊列*/
{
run ->state = 'F';
run->count++; /*進程執行的次數加一*/
InsertFinish(run);
flag = 0;
}
else /*將進程狀態置為W,入就緒隊列*/
{
run->state = 'W';
run->count++; /*進程執行的次數加一*/
InsertTime(run);
flag = 0;
}
}
flag = 1;
GetFirst(); /*繼續取就緒隊列隊頭進程進入執行隊列*/
}
}
void RoundRun() /*時間片輪轉調度演算法*/
{

int flag = 1;

GetFirst();
while(run != NULL)
{
Output();
while(flag)
{
run->count++;
run->cputime++;
run->needtime--;
if(run->needtime == 0) /*進程執行完畢*/
{
run ->state = 'F';
InsertFinish(run);
flag = 0;
}
else if(run->count == run->round)/*時間片用完*/
{
run->state = 'W';
run->count = 0; /*計數器清零,為下次做准備*/
InsertTime(run);
flag = 0;
}
}
flag = 1;
GetFirst();
}

『貳』 用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(i<5) {
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!=NULL&&q->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);
}

『叄』 頁面調度先進先出演算法(FIFO) C語言描述

7,0,1 //先把三個頁面放入主存 0,1,2//缺少頁面2,把2放入,頁面7出 1,2,0 2,0,3//缺少頁面3,放入3,頁面1出 2.3.0 3,0,4//缺少頁面4,放入4,頁面2出 0,4,2//缺少頁面2,放入2,頁面3出 4,2,3//缺少頁面3,放入3,頁面0出 2,3,0//缺少頁面0,放入0,頁面4出 2,0,3 0,3,2 3,2,1//缺少頁面1,放入1,頁面0出 3,1,2

『肆』 用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語言(或其它語言,如Java)編程實現對N個進程採用某種進程調度演算法(如動態優先權調度

公眾:類PrivilegeProcess {
公共靜態無效的主要(字串[] args){

MyQueue的MyQueue的新MyQueue的();/ /聲明隊列

印刷電路板[PCB = {新的PCB(001 ,8,1),新的PCB(002,7,9),新的PCB(003,3,8),新的PCB(004,1,7),新的PCB(005,7,4)};
> PCB段=新的PCB();

(INT I = 0; <pcb.length; + +){/ /初始化先進行排序,選擇排序這里使用的是高優先順序的一線隊

(J =我; <pcb.length; J + +){

(PCB [I]。特權<PCB [J]。特權){

段= PCB [1];

PCB [I] = PCB [J];

PCB [J] =段;

}

}

}

體系。通過out.println(「入隊後第一時間的進程的順序:」);

(INT I = 0; <pcb.length; + +){

的System.out調用println(第一次入隊#程序名稱:「+ PCB [我]。名稱+ totaltime:」+ PCB [I]。totaltime +「的」特權「+ PCB [我]。特權); }

();

myqueue.start(PCB);

}

}

類MyQueue的{

INT指數= 0;

PCB [] PC =新的PCB [5];

PCB [] PC1 =新的PCB [4];

PCB溫度=新的PCB() BR />公共無效排隊(PCB工藝){/ /排隊演算法

(指數== 5){

(「出界!」);

返回

}

PC [索引] =進程;

指數+ +;

}

公共:PCB DEQUEUE(){/ /出隊演算法(索引== 0)

返回空;

(INT I = 0; <pc1.length; + +){

PC1 [I] = PC [ +1];

}

指數 -

溫度= PC [0];

(INT I = 0; <pc1.length; + +){ BR /> PC [I] = PC1 [I];

}

回報條件;

}

公共無效啟動(PCB [] PC){/ /進程表演算法

(PC [0]。isNotFinish ==真| | PC [1 isNotFinish ==真| | PC [2 isNotFinish ==真| | PC [3]。時isNotFinish ==真| | PC [4]。isNotFinish ==){

/ / *註:| |運算符都是假的,所有的表達式結果為假,否則真

(INT I = 0; <PC長度; + +){

PC [I]。運行(這一點); />} 的System.out.println();

(INT I = 0; <pc.length; + +){/ /處理每個運行一次運行的時間片的長度重新排序優先一旦

(J =我; <pc.length; J + +){

如果(PC [I]特權<PC [J]。特權){

溫度= PC [I];

PC [I] = PC [J];

PC [J] =溫度;

}

}

}

}

}

}

類PCB {/ /聲明過程級

和int名,totaltime ,運行時特權;

布爾isNotFinish的;

公眾PCB(){

}

公開PCB(名稱,詮釋totaltime特權){

this.name =的名稱;/ /進程名

this.totaltime = totaltime ;/ /

this.privilege =特權;/ /總時間優先 this.runtime = 2 ;/ /時間片值是2

this.isNotFinish =真;/ /是否執行完成

(「初始值:程序名稱:」+名+「totaltime:」+ totaltime +「特權」+特權);

System.out的。調用println();

}

MyQueue的MQ公共無效的run(){/ /處理的基礎上實施的時間片演算法

(totalTime> 1){ totaltime =運行;/ /總時間大於1,總時間=總時間 - 時間片

特權 -

(「程序名稱:」+姓名+「 remaintime:「+ +」特權「+特權); totaltime

的} else if(totaltime == 1){

totaltime - ;/ /總時間為1時,執行時間為1
>特權 -

(「程序名稱:」+姓名+「remaintime:」+ totaltime +「特權」+特權);

}其他{

isNotFinish =假;/ / 0,將isNotFinish標志設置為假

}

如果(isNotFinish ==真){br mq.deQueue();

mq.enQueue(本);

}

}
}

『陸』 怎麼樣實現分頁管理的缺頁調度clock演算法C語言代碼

這個程序我做過,現在給你!!寫了很久的!!

#include<stdafx.h>
#include<stdlib.h>
#include<stdio.h>
#define n 64//實驗中假定主存的長度
#define m 4//實驗中假定每個作業分得主存塊塊數
int p[m];//定義頁
int head=0;
struct
{
short int lnumber;//頁號
short int flag;//表示該頁是否在主存,"1"表示在主存,"0"表示不在主存
short int pnumber;//該頁所在主存塊的塊號
short int write;//該頁是否被修改過,"1"表示修改過,"0"表示沒有修改過
short int dnumber;//該頁存放在磁碟上的位置,即磁碟塊號
short int times;//被訪問的次數,用於LRU演算法
}page[n];//定義頁表
//各個函數的實現如下:
void computer()
{
int i;
for(i=0;i<n;i++)
{
page[i].lnumber = i;
page[i].flag = 0;
page[i].pnumber = 10000;//用10000表示為空
page[i].write = 0;
page[i].dnumber = i;
page[i].times = 0;
}//初始化頁表

for(i=0;i<m;i++)
{
page[i].pnumber = i;
}

for(i=0;i<m;i++)
{
p[i] = i;
page[i].flag = 1;
}//初始化頁
}
void showpagelist()
{
int i;
printf("\n頁號\t是否在主存中\t塊 號\t是否被修改過\t磁碟塊號\t訪問次數\n");
for(i=0;i<n;i++)
{
printf("%d\t%d\t\t%d\t\t%d\t\t%d\t\t%d\n",page[i].lnumber,page[i].flag,page[i].pnumber,page[i].write,page[i].dnumber,page[i].times);
}
}
void showpage()
{
int i;
for(i=0;i<m;i++)
{
printf("\t%d\n",p[i]);
}
}
void transformation() //缺頁中斷處理
{
unsigned logicAddress,logicNumber,innerAddress,physicsAddress,physicsNumber;
int i, fail = 0;
int method,temppage=0;
short int times = 10000;
printf("請輸入一個邏輯地址(四位十六進制數):");
scanf("%x",&logicAddress);//讀入邏輯地址
logicNumber = logicAddress >> 10;//得到頁號
printf("頁號為:%ld\n",logicNumber);
innerAddress = logicAddress & 0x03ff;//得到頁內地址
printf("頁內地址為:%ld\n",innerAddress);
for(i=0;i<n;i++)
{
if(logicNumber==(unsigned)page[i].lnumber)
{
if(page[i].flag == 1)
{
printf("請求的頁面在主存中!\n");
page[i].times++;
physicsNumber = page[i].pnumber;//由頁號得到塊號
printf("請求的主存塊號為:%ld\n",physicsNumber);
physicsAddress = physicsNumber << 10 |innerAddress;//得到物理地址
printf("請求的物理地址為:%ld",physicsAddress);//輸出物理地址
break;
}
else
{
printf("請求的頁面不在主存中! 將進行缺頁中斷處理!\n請選擇演算法!\n");
printf("1.先進先出\n2.最近最少用\n請選擇置換演算法:");
scanf("%d",&method);
if(method == 1) //採用先進先出演算法
{
printf("採用先進先出演算法!\n");
fail = p[head];
printf("第%d頁將被替換!\n",fail);
p[head] = logicNumber;
head = (head+1) % m;
if(page[fail].write == 1)
printf("第%d頁曾被修改過!\n",fail);
page[fail].flag = 0;
page[logicNumber].flag = 1;
page[logicNumber].write = 0;
page[logicNumber].pnumber = page[fail].pnumber;
page[fail].pnumber = 10000;
page[logicNumber].times++;
break;
}
else if(method == 2) //採用最近最少用演算法
{
printf("採用最近最少用演算法!\n");
for(i=0;i<n;i++)
{
if(page[i].flag == 1)
{
if(page[i].times<times)
{
times = page[i].times;
temppage = page[i].lnumber;
}
}
}
printf("第%d頁將被替換!\n",temppage);
for(i=0;i<m;i++)
{
if(p[i] == temppage)
{
p[i] = logicNumber;
}
}
if(page[temppage].write == 1)
printf("第%d頁曾被修改過!\n",temppage);
page[temppage].flag = 0;
page[logicNumber].flag = 1;
page[logicNumber].write = 0;
page[logicNumber].pnumber = page[temppage].pnumber;
page[temppage].pnumber = 10000;
page[logicNumber].times++;
break;
}
else
{
printf("你輸入有誤,即將退出!");
exit(1);
}
}
}
}
}
void main()
{
char c,d,flag='y';
printf("頁表正在初始化中...,3秒鍾後為你顯示頁和頁表!\n");
computer();
showpage();
showpagelist();
while(flag == 'y' || flag == 'Y')
{
transformation();
printf("是否顯示頁和頁表?(Y/N)");
c = getchar();
c = getchar();
if(c=='Y'||c=='y')
{
showpage();
showpagelist();
}
else
{
while(c=='N'||c=='n')
{
printf("\n是否繼續進行請求分頁?(Y/N)");
d = getchar();
d = getchar();
if(d=='Y'||d=='y')
{
transformation();
printf("\n是否顯示頁和頁表?(Y/N)");
c = getchar();
c = getchar();
if(c=='Y'||c=='y')
{
showpage();
showpagelist();
}
}
else if (d=='N'||d=='n')
exit(1);
else
printf("輸入錯誤!\n");
}
}
printf("\n是否繼續進行請求分頁?(Y/N)");
flag = getchar();
flag = getchar();

}
}

『柒』 求進程調度先來先服務演算法,短進程優先演算法完整c語言代碼

/*(一)進程調度

進程調度演算法有FIFO,優先數調度演算法,時間片輪轉調度演算法,分級調度演算法,

輸入:進程流文件,其中存儲的是一系列要執行的進程,
每個作業包括三個數據項:
進程名 所需時間 優先數(0級最高)
輸出:
進程執行流 等待時間 平均等待時間

本程序包括:FIFO,優先數調度演算法,時間片輪轉調度演算法

進程流文件process_stream.txt
測試數據:
p0 16 2
p1 5 1
p2 4 3
p3 8 0
p4 9 4
p5 7 6

VC++調試通過
*/
#include <stdio.h>
#include <string.h>
#include <iostream.h>
#include <stdlib.h>

const int Quatum=2;//定義時間片的長度為2秒
const int MAXPCB=100;//定義最大進程數

//定義進程結構體
typedef struct node
{
char name[20];//進程名
int time; //進程運行時間
int privilege;//進程優先順序(靜態)
int finished;//進程完成標志,0-未完成,1-已完成
int wait_time;//進程等待時間
}pcb;

pcb pcbs[MAXPCB];
int quantiry;//進程流文件中的進程總數

void initial()
{
int i;
for (i=0;i<MAXPCB;i++)
{
strcpy(pcbs[i].name,"");
pcbs[i].time=0;
pcbs[i].privilege=0;
pcbs[i].finished=0;
pcbs[i].wait_time=0;
}
quantiry=0;
}

int readData()
{
FILE *fp;
char fname[20];
int i;
cout<<"請輸入進程流文件名:"<<endl;
cin>>fname;
if ((fp=fopen(fname,"r"))==NULL)
{
cout<<"錯誤,文件打不開,請檢查文件名"<<endl;
}
else
{
while (!feof(fp))
{
fscanf(fp,"%s %d %d %d",pcbs[quantiry].name,
&pcbs[quantiry].time,&pcbs[quantiry].privilege);
quantiry++;
}
//輸出所讀入得數據
cout<<"輸出所讀入的數據"<<endl;
cout<<"進程流文件中的進程總數="<<quantiry<<endl;
cout<<"進程名 所需時間 優先數"<<endl;

for (i=0;i<quantiry;i++)
{
cout<<" "<<pcbs[i].name<<" "<<pcbs[i].time<<" "<<pcbs[i].privilege<<endl;
}

return 1;
}

return 0;
}

//重置數據,以供另一個演算法使用
void init()
{
int i;
for (i=0;i<MAXPCB;i++)
{
pcbs[i].finished=0;
pcbs[i].wait_time=0;
}
}

void FIFO()
{
int i,j;

int total;
//輸出FIFO演算法執行流
cout<<endl<<"---------------------------------------------------------------"<<endl;
cout<<"FIFO演算法執行流:"<<endl;
cout<<"進程名 等待時間"<<endl;

for (i=0;i<quantiry;i++)
{
cout<<" "<<pcbs[i].name<<" "<<pcbs[i].wait_time<<endl;
for (j=i+1;j<quantiry;j++)
{
pcbs[j].wait_time+=pcbs[i].time;
}
}

total=0;
for (i=0;i<quantiry;i++)
{
total+=pcbs[i].wait_time;
}
cout<<"總等待時間:"<<total<<" "<<"平均等待時間:"<<total/quantiry<<endl;
}

//優先度調度演算法
void privilege()
{
int i,j,p;
int passed_time=0;
int total;

int queue[MAXPCB];
int current_privielege=1000;

for (i=0;i<quantiry;i++)
{
current_privielege=1000;
for (j=0;j<quantiry;j++)
{
if ((pcbs[j].finished==0)&&(pcbs[j].privilege<current_privielege))
{
p=j;
current_privielege=pcbs[j].privilege;
}
}
queue[i]=p;
pcbs[p].finished=1;
pcbs[p].wait_time+=passed_time;
passed_time+=pcbs[p].time;

}
//輸出優先數調度執行流
cout<<endl<<"-----------------------------------------"<<endl;
cout<<"優先數調度執行流:"<<endl;
cout<<"進程名 等待時間"<<endl;

for (i=0;i<quantiry;i++)
{
cout<<" "<<pcbs[queue[i]].name<<" "<<pcbs[queue[i]].wait_time<<"--"<<queue[i]<<endl;
}

total=0;
for (i=0;i<quantiry;i++)
{
total+=pcbs[i].wait_time;
}

cout<<"總等待時間:"<<total<<" 平均等待時間:"<<total/quantiry<<endl;
}

//時間片輪轉調度演算法
void timer()
{
int i,j,sum,flag=1;
int passed_time=0;
int max_time=0;
int round=0;

int queue[1000];
int total=0;

while(flag==1)
{
flag=0;
for (i=0;i<quantiry;i++)
{
if (pcbs[i].finished==0)
{
flag=1;
queue[total]=i;
total++;
if (pcbs[i].time<=Quatum*(round+1))
pcbs[i].finished=1;
}
}
round++;
}

cout<<endl<<"---------------------------------------------------------------"<<endl;
cout<<"時間片輪轉調度執行流:";
for(i=0;i<total;i++)
{
cout<<pcbs[queue[i]].name<<" ";
}
cout<<endl;
cout<<"進程名 結束時間 運行時間 等待時間"<<endl;

sum=0;

for (i=0;i<quantiry;i++)
{
for(j=total-1;j>=0;j--)//從輪轉調度執行流序列由後往前比較,找到同名進程即可計算其完成時間
{
if (strcmp(pcbs[queue[j]].name,pcbs[i].name)==0)
{
cout<<" "<<pcbs[i].name<<" "<<(j+1)*Quatum<<" ";
cout<<pcbs[i].time<<" "<<(j+1)*Quatum-pcbs[i].time<<endl;
sum+=(j+1)*Quatum-pcbs[i].time;
break;
}
}
}

cout<<"總等待時間:"<<sum<<" "<<"平均等待時間:"<<sum/quantiry<<endl;
}

//顯示版權信息函數
void version()
{
cout<<endl<<endl;

cout<<" ┏━━━━━━━━━━━━━━━━━━━━━━━┓"<<endl;
cout<<" ┃ 進程調度模擬系統 ┃"<<endl;
cout<<" ┠───────────────────────┨"<<endl;
cout<<" ┃ version 2011 ┃"<<endl;
cout<<" ┗━━━━━━━━━━━━━━━━━━━━━━━┛"<<endl;
cout<<endl<<endl;
}
//主函數

int main()
{
int flag;
version();
initial();
flag=readData();
if(flag==1){
FIFO();
init();
privilege();
init();
timer();
}
cout<<endl;
system("pause");
return 0;
}

『捌』 權重輪詢調度演算法(Weighted Round-Robin Scheling) [C語言實現]

weight[i+1] = a % weight[i+1];
這句話導致後面的weight為0

熱點內容
微信支付進去手勢密碼哪裡改 發布:2024-09-23 17:02:08 瀏覽:327
我的世界2g伺服器內存 發布:2024-09-23 16:57:55 瀏覽:581
正則表達式預編譯html案例 發布:2024-09-23 16:53:22 瀏覽:939
文章腳本 發布:2024-09-23 16:48:20 瀏覽:758
sna2008演算法 發布:2024-09-23 16:36:49 瀏覽:504
哥倫比亞大學訪問學者 發布:2024-09-23 16:08:19 瀏覽:571
devc怎麼配置gcc編譯環境 發布:2024-09-23 15:52:26 瀏覽:446
血族第二季ftp 發布:2024-09-23 15:49:58 瀏覽:528
清楚手機瀏覽器緩存 發布:2024-09-23 15:47:24 瀏覽:518
伺服器一般用什麼遠程 發布:2024-09-23 15:47:22 瀏覽:238