當前位置:首頁 » 操作系統 » 模擬演算法

模擬演算法

發布時間: 2022-01-09 16:16:05

㈠ 時間片輪轉演算法的模擬

#include"stdio.h"
#include"stdlib.h"
#include"string.h"
#include"iostream.h"
typedef struct node
{
char name[10];
int prio;
int round;
int cputime;
int needtime;
int count;
char state;
struct node *next;
}PCB;
PCB *finish,*ready,*tail,*run;
int N;
void firstin()
{run=ready;<br/>run->state='R';<br/>ready=ready->next;<br/>}
void prt1(char a)
{if(toupper(a)=='p')<br/>cout<<" "<<endl;<br/>cout<<"進程名 佔用CPU時間 到完成還要的時間 優先順序 狀態"<<endl;<br/>}
void prt2(char a,PCB *q)
{if(toupper(a)=='P')<br/>cout<<q->name<<" "<<q->cputime<<" "<<q->needtime<<" "<<q->prio<<" "<<q->state<<endl;<br/>}
void prt(char algo)
{
PCB *p;
prt1(algo);
if(run!=NULL)
prt2(algo,run);
p=ready;
while(p!=NULL)
{prt2<br/>(algo,p);<br/>p=p->next;<br/>}
p=finish;
while(p!=NULL)
{prt2(algo,p);<br/>p=p->next;<br/>}
getchar();
}
/*
void insert(PCB *q)
{PCB *p1,*s,*r;<br/>int b;<br/>s=q;<br/>p1=ready;<br/>r=p1;<br/>b=1;<br/>while((p1!=NULL)&&b)<br/>if(p1->prio>=s->prio)<br/>{r=p1;<br/>p1=p1->next;<br/>}
else
b=0;
if(r!=p1)
{r->next=s;<br/>s->next=p1;<br/>}
else
{s->next=p1;<br/>ready=s;<br/>}
}
*/
void insert(PCB *q)
{PCB *p1,*s,*r;<br/>//int b;<br/>s=q;<br/>p1=ready;<br/>r=p1;<br/>//b=1;<br/>while(p1!=NULL)<br/>//if(p1->prio<s->prio)<br/>{r=p1;<br/>p1=p1->next;<br/><br/>}
//else
//b=0;
if(r!=p1)
{r->next=s;<br/>s->next=p1;<br/>}
else
{s->next=p1;<br/>ready=s;<br/>}
}

void create(char alg)
{
PCB *p;
int i,time;
char na[10];
ready=NULL;
finish=NULL;
run=NULL;
cout<<"輸入進程名及其需要運行的時間:"<<endl;
for(i=1;i<=N;i++)
{p=new PCB;<br/>cin>>na;<br/>cin>>time;<br/>strcpy(p->name,na);<br/>p->cputime=0;<br/>p->needtime=time;<br/>p->state='w';<br/>p->prio=i;<br/>if(ready!=NULL)<br/> insert(p);<br/>else<br/>{p->next=ready;<br/>ready=p;<br/>}
cout<<"輸入進程名及其需要運行的時間:"<<endl;
}
prt(alg);
run=ready;
ready=ready->next;
run->state='R';
}

㈡ 有c語言模擬調度演算法嗎

/*本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

㈢ 遺傳演算法、數值演算法、爬山演算法、模擬退火 各自的優缺點

遺傳演算法:其優點是能很好地處理約束,跳出局部最優,最終得到全局最優解。缺點是收斂速度慢,局部搜索能力弱,運行時間長,容易受到參數的影響。

模擬退火:具有局部搜索能力強、運行時間短的優點。缺點是全局搜索能力差,容易受到參數的影響。

爬山演算法:顯然爬山演算法簡單、效率高,但在處理多約束大規模問題時,往往不能得到較好的解決方案。

數值演算法:這個數值演算法的含義太寬泛了,指的是哪種數值演算法,陣列演算法與爬山演算法一樣,各有優缺點。

(3)模擬演算法擴展閱讀:

注意事項:

遺傳演算法的機制比較復雜,在Matlab中已經用工具箱中的命令進行了打包,通過調用可以非常方便的使用遺傳演算法。

函數GA:[x,Fval,reason]=GA(@fitnessfun,Nvars,options)x為最優解,Fval為最優值,@Fitnessness為目標函數,Nvars為自變數個數,options為其他屬性設置。系統的默認值是最小值,所以函數文檔中應該加上一個減號。

要設置選項,您需要以下函數:options=GaOptimset('PropertyName1','PropertyValue1','PropertyName2','PropertyName3','PropertyValue3'…)通過該函數,可以確定一些遺傳演算法的參數。

㈣ 用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語言採用模擬DFA演算法編寫一個掃描器(詞法分析器)

(1)濾掉源程序中的無用成分,如空格;
這個」源程序「是指?不是只要識別像
bbbbaa+1,
aa-1
這樣的字元串么?

㈥ 本人初二 , 學習c++一年,學會了基本的語法 。 正在准備自學演算法和數據結構 。 請問模擬演算法是什麼東東。

推薦先看數據結構C++版,嚴蔚敏的,再看演算法導論,你演算法就算過關了,消息來源華夏聯盟

㈦ 進程調度模擬演算法,發到郵箱[email protected]

#include "stdio.h"

#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 qname\tstate\tsuper\tndtime\truntime\n");

printf(" %s\t",pr->name);

printf("%c\t",pr->state);

printf("%d\t",pr->super);

printf("%d\t",pr->ntime);

printf("%d\t",pr->rtime);

printf("\n");

}
void check() /* 建立進程查看函數 */

{

PCB* pr;

printf("\n **** 當前正在運行的進程是:%s",p->name); /*顯示當前運行進程*/

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("\n The execute number:%d \n",h);

p=ready;

ready=p->link;

p->link=NULL;

p->state='R';

check();

running();

printf("\n 按任一鍵繼續......");

ch=getchar();

}

printf("\n\n 進程已經完成.\n");

ch=getchar();

}

㈧ 編程模擬作業調度演算法 拜託了···· 下午就要交了··· 求編程高手幫助···

float arry[4][2];
arry[0][0]=8.00;
arry[0][1]=2.00;
arry[1][0]=8.50;
arry[1][1]=0.50;
arry[2][0]=9.00;
arry[2][1]=0.10
arry[3][0]=9.50
arry[3][1]=0..20
//用fcfs演算法
int shunxu[4][1];
for(int i =0;i<4;i++)
shunxu[i][0]=i;
for(int i =0;i<4;i++){
for(int j =i;j<4;j++)
{
if(arry[shunxu[j][0]][0]<arry[shunxu[i][0]][0])
{
int temp =shunxu[j][0];
shunxu[j][0]=shunxu[i][1];
shunxu[i][1]=temp;
}
}
}
//順序是
for(int i =0;i<4;i++)
printf("%d\n",&shunxu[i][0]);

㈨ 進程調度演算法模擬程序設計

這么有技術含量的問題,應該要更有技術含量的回答,你給個30分懸賞,高手都不睬你~!

熱點內容
linux下ntp伺服器搭建 發布:2024-09-08 08:26:46 瀏覽:742
db2新建資料庫 發布:2024-09-08 08:10:19 瀏覽:171
頻率計源碼 發布:2024-09-08 07:40:26 瀏覽:778
奧迪a6哪個配置帶後排加熱 發布:2024-09-08 07:06:32 瀏覽:101
linux修改apache埠 發布:2024-09-08 07:05:49 瀏覽:209
有多少個不同的密碼子 發布:2024-09-08 07:00:46 瀏覽:566
linux搭建mysql伺服器配置 發布:2024-09-08 06:50:02 瀏覽:995
加上www不能訪問 發布:2024-09-08 06:39:52 瀏覽:811
銀行支付密碼器怎麼用 發布:2024-09-08 06:39:52 瀏覽:513
蘋果手機清理瀏覽器緩存怎麼清理緩存 發布:2024-09-08 06:31:32 瀏覽:554