操作系統(tǒng)實(shí)驗(yàn)報(bào)告一進(jìn)程同步機(jī)構(gòu)的模擬pv操作的實(shí)現(xiàn).doc
約7頁DOC格式手機(jī)打開展開
操作系統(tǒng)實(shí)驗(yàn)報(bào)告一進(jìn)程同步機(jī)構(gòu)的模擬pv操作的實(shí)現(xiàn),操作系統(tǒng)實(shí)驗(yàn)報(bào)告一進(jìn)程同步機(jī)構(gòu)的模擬pv操作的實(shí)現(xiàn) 全文7頁1309字 圖文并實(shí)驗(yàn)要求:模擬實(shí)現(xiàn)用同步機(jī)構(gòu)避免并發(fā)進(jìn)程執(zhí)行時(shí)可能出現(xiàn)的與時(shí)間有關(guān)的錯(cuò)誤。進(jìn)程是程序在一個(gè)數(shù)據(jù)集合上運(yùn)行的過程,進(jìn)程是并發(fā)執(zhí)行的,也即系統(tǒng)中的多個(gè)進(jìn)程輪流地占用處理器運(yùn)行。我們把如干個(gè)進(jìn)程都能進(jìn)行訪問和修改地那些變量成為公共變量。由于進(jìn)程是并...
內(nèi)容介紹
此文檔由會員 黃藥師 發(fā)布
操作系統(tǒng)實(shí)驗(yàn)報(bào)告一進(jìn)程同步機(jī)構(gòu)的模擬PV操作的實(shí)現(xiàn)
全文7頁1309字 圖文并
實(shí)驗(yàn)要求:
模擬實(shí)現(xiàn)用同步機(jī)構(gòu)避免并發(fā)進(jìn)程執(zhí)行時(shí)可能出現(xiàn)的與時(shí)間有關(guān)的錯(cuò)誤。
進(jìn)程是程序在一個(gè)數(shù)據(jù)集合上運(yùn)行的過程,進(jìn)程是并發(fā)執(zhí)行的,也即系統(tǒng)中的多個(gè)進(jìn)程輪流地占用處理器運(yùn)行。
我們把如干個(gè)進(jìn)程都能進(jìn)行訪問和修改地那些變量成為公共變量。由于進(jìn)程是并發(fā)執(zhí)行的,所以,如果對進(jìn)程訪問公共變量不加限制,那么就會產(chǎn)生“與時(shí)間有關(guān)”的錯(cuò)誤,即進(jìn)程執(zhí)行后,所得到的結(jié)果與訪問公共變量的時(shí)間有關(guān)。為了防止這類錯(cuò)誤,系統(tǒng)必須要用同步機(jī)構(gòu)來控制進(jìn)程對公共變量的訪問。一般說,同步機(jī)構(gòu)是由若干條原語——同步原語——所組成。本實(shí)驗(yàn)要求學(xué)生模擬PV操作同步機(jī)構(gòu)的實(shí)現(xiàn),模擬進(jìn)程的并發(fā)執(zhí)行,了解進(jìn)程并發(fā)執(zhí)行時(shí)同步機(jī)構(gòu)的作用。
解題思路(流程圖):
圖4-5處理器調(diào)度程序流程
實(shí)驗(yàn)結(jié)果(部分源碼):
#include
#include
#include
#include
#define INPUT "D:input.txt"//文件輸入流的地址,可適當(dāng)修改
int suanfa;
#define WAIT 'W'
#define RUN 'R'
typedef struct PCB_Prioriy
{
char name[25];
int priority;
int needTime;
int CPUTime;
char state;
struct PCB_Prioriy *next;
}PCB_Prioriy; //優(yōu)先數(shù)算法的pcb控制模塊
PCB_Prioriy *head_Prioriy = NULL; //頭指針
typedef struct PCB_RoundRobin
{
char name[25];
int CPUTime;
int needTime;
int count;
int round;
char state;
struct PCB_RoundRobin *next;
}PCB_RoundRobin; //時(shí)間片輪轉(zhuǎn)算法的pcb控制模塊
PCB_RoundRobin *head_RoundRobin = NULL; //頭指針
int len_PCB_RoundRobin; //pcb模塊的長度
void Insert_priority(PCB_Prioriy *p)//優(yōu)先數(shù)算法的插入函數(shù)
{
PCB_Prioriy *p1, *p2;
bool insertTail = true;
if( head_Prioriy==NULL || p->priority > head_Prioriy->priority ) // 插入隊(duì)首
{
p->next = head_Prioriy;
head_Prioriy = p;
}
else
{
p1 = head_Prioriy;
while(p1->next != NULL)
{
p1 = p1->next;
if( p1->priority < p->priority ) // 找到插入點(diǎn)
{
insertTail = false;
break;
}
}
if(insertTail) // 插入隊(duì)尾
p1->next = p;
else
{
p2 = head_Prioriy;
while(p2->next != p1)
p2 = p2->next;
p2->next = p;
p->next = p1;
}
}
}
void Insert_Tail(PCB_RoundRobin *p)//時(shí)間片輪轉(zhuǎn)算法的插入
{
PCB_RoundRobin *tail;
if(head_RoundRobin == NULL)
{
p->next = head_RoundRobin;
head_RoundRobin = p;
}
else
{
tail = head_RoundRobin;
while(tail->next != NULL)
tail = tail->next;
tail->next = p;
}
}
void OutputInfo_priority()//輸出優(yōu)先數(shù)算法的所有信息
{
PCB_Prioriy *p;
head_Prioriy->state = RUN;
p = head_Prioriy;
while(p != NULL)
{
printf("%st%dt%dt%dt%cn",p->name,p->CPUTime,p->needTime,p->priority,p->state);
p = p->next;
}
}
void OutputInfo_roundRobin()//輸出時(shí)間片轉(zhuǎn)算法的所有信息
{
PCB_RoundRobin *p;
head_RoundRobin->state = RUN;
p = head_RoundRobin;
while(p != NULL)
{
printf("%st%dt%dt%dt%dt%cn",p->name,p->CPUTime,p->needTime,p->count,p->round,p->state);
p = p->next;
}
}
void ReadWork_priority() //建立進(jìn)程控制塊函數(shù)_優(yōu)先數(shù)算法
{
ifstream fin(INPUT);
PCB_Prioriy *p = (PCB_Prioriy*)malloc(sizeof(PCB_Prioriy));
while(fin>> p->name >> p->needTime)
{
p->CPUTime = 0 ;
p->next = NULL;
p->priority = 50 - p->needTime; //初始值50-needTime
p->state = WAIT;
Insert_priority(p);//調(diào)用優(yōu)先數(shù)算法的插入函數(shù)
p = (PCB_Prioriy*)malloc(sizeof(PCB_Prioriy));
}
}
void ReadWork_roundRobin()//建立進(jìn)程控制塊函數(shù)_時(shí)間片轉(zhuǎn)算法
{
ifstream fin(INPUT);
PCB_RoundRobin *p = (PCB_RoundRobin*)malloc(sizeof(PCB_RoundRobin));
while(fin>> p->name >> p->needTime)
{
p->count = 0;
p->CPUTime = 0;
p->next = NULL;
p->round = 1;
p->state = WAIT;
Insert_Tail(p);//時(shí)間片輪轉(zhuǎn)算法的插入函數(shù)
p = (PCB_RoundRobin*)malloc(sizeof(PCB_RoundRobin));
}
}
void Processing_priority() //建立進(jìn)程查看函數(shù)
{
PCB_Prioriy *p_Running;
OutputInfo_priority();
p_Running = head_Prioriy;
head_Prioriy = p_Running->next;
p_Running->next = NULL;
p_Running->needTime--;//needTime--
if(p_Running->needTime==0)//當(dāng)needTime等于0時(shí),說明此進(jìn)程已完成
{
printf("進(jìn)程%s已完成!n",p_Running->name);
free(p_Running);
}
else
{
p_Running->priority -= 3;
p_Running->CPUTime++;
p_Running->state = WAIT;
Insert_priority(p_Running);
}
}
void Processing_roundRobin()//建立進(jìn)程查看函數(shù)
{
PCB_RoundRobin *p_Running;
OutputInfo_roundRobin();
p_Running = head_RoundRobin;
head_RoundRobin = p_Running->next;
p_Running->next = NULL;
p_Running->needTime -= 2;//needTime-=2
if(p_Running->needTime <= 0)//當(dāng)needTime此時(shí)已小于0,說明此進(jìn)程已完成
{
printf("進(jìn)程%s已完成!n",p_Running->name);
free(p_Running);
}
else
{
p_Running->count++;
p_Running->CPUTime += 2;
p_Running->state = WAIT;
Insert_Tail(p_Running);
}
}
int main()
{
int CurrentTime = 0;//當(dāng)前時(shí)間控制
printf("選擇算法:n"); //算法的選擇
printf("1.優(yōu)先數(shù)算法n");
printf("2.時(shí)間片輪轉(zhuǎn)算法n");
scanf("%d",&suanfa);
if(suanfa==1) //若選擇優(yōu)先數(shù)算法
{
ReadWork_priority(); //建立優(yōu)先數(shù)算法的進(jìn)程控制塊函數(shù)
while(head_Prioriy != NULL)
{
getchar(); //停住,防止在win98里不能察看
printf("nCurrentTime = %d :n",CurrentTime);
printf("NAME CPUTIME NEEDTIME PRIORITY STATEn");
CurrentTime++; //當(dāng)前時(shí)間增長
Processing_priority(); //調(diào)用優(yōu)先數(shù)算法的進(jìn)程查看函數(shù)
}
}
else if(suanfa==2) //若選擇時(shí)間片輪轉(zhuǎn)算法
{
ReadWork_roundRobin(); //建立時(shí)間片輪轉(zhuǎn)算法的進(jìn)程控制塊函數(shù)
PCB_RoundRobin *p = head_RoundRobin; //使輪轉(zhuǎn)的頭與pcb控制塊的指針一致
len_PCB_RoundRobin = 0; //長度先賦為0
while(p != NULL) //使P指向鏈尾,并記錄鏈長
{
len_PCB_RoundRobin++;
p = p->next;
}
int roundPCB = 0; //計(jì)數(shù)器先賦0
while(head_RoundRobin != NULL)
{
getchar(); //停住,防止在win98里不能察看
printf("nCurrentTime = %d :n",CurrentTime);
printf("NAME CPUTIME NEEDTIME COUNT ROUND STATEn");
CurrentTime += 2; //使當(dāng)前時(shí)間增二
if(roundPCB == len_PCB_RoundRobin)
{
p = head_RoundRobin;
len_PCB_RoundRobin = 0;
while(p != NULL)
{
p->round++;
len_PCB_RoundRobin++;
p = p->next;
}
roundPCB = 0;
}
roundPCB++; //計(jì)數(shù)器遞增
Processing_roundRobin(); //調(diào)用時(shí)間片輪轉(zhuǎn)的進(jìn)程查看函數(shù)
}
}
else //否則沒有相對應(yīng)的算法
{
printf("沒找到算法!n");
return 0;
}
return 0;
}
全文7頁1309字 圖文并
實(shí)驗(yàn)要求:
模擬實(shí)現(xiàn)用同步機(jī)構(gòu)避免并發(fā)進(jìn)程執(zhí)行時(shí)可能出現(xiàn)的與時(shí)間有關(guān)的錯(cuò)誤。
進(jìn)程是程序在一個(gè)數(shù)據(jù)集合上運(yùn)行的過程,進(jìn)程是并發(fā)執(zhí)行的,也即系統(tǒng)中的多個(gè)進(jìn)程輪流地占用處理器運(yùn)行。
我們把如干個(gè)進(jìn)程都能進(jìn)行訪問和修改地那些變量成為公共變量。由于進(jìn)程是并發(fā)執(zhí)行的,所以,如果對進(jìn)程訪問公共變量不加限制,那么就會產(chǎn)生“與時(shí)間有關(guān)”的錯(cuò)誤,即進(jìn)程執(zhí)行后,所得到的結(jié)果與訪問公共變量的時(shí)間有關(guān)。為了防止這類錯(cuò)誤,系統(tǒng)必須要用同步機(jī)構(gòu)來控制進(jìn)程對公共變量的訪問。一般說,同步機(jī)構(gòu)是由若干條原語——同步原語——所組成。本實(shí)驗(yàn)要求學(xué)生模擬PV操作同步機(jī)構(gòu)的實(shí)現(xiàn),模擬進(jìn)程的并發(fā)執(zhí)行,了解進(jìn)程并發(fā)執(zhí)行時(shí)同步機(jī)構(gòu)的作用。
解題思路(流程圖):
圖4-5處理器調(diào)度程序流程
實(shí)驗(yàn)結(jié)果(部分源碼):
#include
#include
#include
#include
#define INPUT "D:input.txt"//文件輸入流的地址,可適當(dāng)修改
int suanfa;
#define WAIT 'W'
#define RUN 'R'
typedef struct PCB_Prioriy
{
char name[25];
int priority;
int needTime;
int CPUTime;
char state;
struct PCB_Prioriy *next;
}PCB_Prioriy; //優(yōu)先數(shù)算法的pcb控制模塊
PCB_Prioriy *head_Prioriy = NULL; //頭指針
typedef struct PCB_RoundRobin
{
char name[25];
int CPUTime;
int needTime;
int count;
int round;
char state;
struct PCB_RoundRobin *next;
}PCB_RoundRobin; //時(shí)間片輪轉(zhuǎn)算法的pcb控制模塊
PCB_RoundRobin *head_RoundRobin = NULL; //頭指針
int len_PCB_RoundRobin; //pcb模塊的長度
void Insert_priority(PCB_Prioriy *p)//優(yōu)先數(shù)算法的插入函數(shù)
{
PCB_Prioriy *p1, *p2;
bool insertTail = true;
if( head_Prioriy==NULL || p->priority > head_Prioriy->priority ) // 插入隊(duì)首
{
p->next = head_Prioriy;
head_Prioriy = p;
}
else
{
p1 = head_Prioriy;
while(p1->next != NULL)
{
p1 = p1->next;
if( p1->priority < p->priority ) // 找到插入點(diǎn)
{
insertTail = false;
break;
}
}
if(insertTail) // 插入隊(duì)尾
p1->next = p;
else
{
p2 = head_Prioriy;
while(p2->next != p1)
p2 = p2->next;
p2->next = p;
p->next = p1;
}
}
}
void Insert_Tail(PCB_RoundRobin *p)//時(shí)間片輪轉(zhuǎn)算法的插入
{
PCB_RoundRobin *tail;
if(head_RoundRobin == NULL)
{
p->next = head_RoundRobin;
head_RoundRobin = p;
}
else
{
tail = head_RoundRobin;
while(tail->next != NULL)
tail = tail->next;
tail->next = p;
}
}
void OutputInfo_priority()//輸出優(yōu)先數(shù)算法的所有信息
{
PCB_Prioriy *p;
head_Prioriy->state = RUN;
p = head_Prioriy;
while(p != NULL)
{
printf("%st%dt%dt%dt%cn",p->name,p->CPUTime,p->needTime,p->priority,p->state);
p = p->next;
}
}
void OutputInfo_roundRobin()//輸出時(shí)間片轉(zhuǎn)算法的所有信息
{
PCB_RoundRobin *p;
head_RoundRobin->state = RUN;
p = head_RoundRobin;
while(p != NULL)
{
printf("%st%dt%dt%dt%dt%cn",p->name,p->CPUTime,p->needTime,p->count,p->round,p->state);
p = p->next;
}
}
void ReadWork_priority() //建立進(jìn)程控制塊函數(shù)_優(yōu)先數(shù)算法
{
ifstream fin(INPUT);
PCB_Prioriy *p = (PCB_Prioriy*)malloc(sizeof(PCB_Prioriy));
while(fin>> p->name >> p->needTime)
{
p->CPUTime = 0 ;
p->next = NULL;
p->priority = 50 - p->needTime; //初始值50-needTime
p->state = WAIT;
Insert_priority(p);//調(diào)用優(yōu)先數(shù)算法的插入函數(shù)
p = (PCB_Prioriy*)malloc(sizeof(PCB_Prioriy));
}
}
void ReadWork_roundRobin()//建立進(jìn)程控制塊函數(shù)_時(shí)間片轉(zhuǎn)算法
{
ifstream fin(INPUT);
PCB_RoundRobin *p = (PCB_RoundRobin*)malloc(sizeof(PCB_RoundRobin));
while(fin>> p->name >> p->needTime)
{
p->count = 0;
p->CPUTime = 0;
p->next = NULL;
p->round = 1;
p->state = WAIT;
Insert_Tail(p);//時(shí)間片輪轉(zhuǎn)算法的插入函數(shù)
p = (PCB_RoundRobin*)malloc(sizeof(PCB_RoundRobin));
}
}
void Processing_priority() //建立進(jìn)程查看函數(shù)
{
PCB_Prioriy *p_Running;
OutputInfo_priority();
p_Running = head_Prioriy;
head_Prioriy = p_Running->next;
p_Running->next = NULL;
p_Running->needTime--;//needTime--
if(p_Running->needTime==0)//當(dāng)needTime等于0時(shí),說明此進(jìn)程已完成
{
printf("進(jìn)程%s已完成!n",p_Running->name);
free(p_Running);
}
else
{
p_Running->priority -= 3;
p_Running->CPUTime++;
p_Running->state = WAIT;
Insert_priority(p_Running);
}
}
void Processing_roundRobin()//建立進(jìn)程查看函數(shù)
{
PCB_RoundRobin *p_Running;
OutputInfo_roundRobin();
p_Running = head_RoundRobin;
head_RoundRobin = p_Running->next;
p_Running->next = NULL;
p_Running->needTime -= 2;//needTime-=2
if(p_Running->needTime <= 0)//當(dāng)needTime此時(shí)已小于0,說明此進(jìn)程已完成
{
printf("進(jìn)程%s已完成!n",p_Running->name);
free(p_Running);
}
else
{
p_Running->count++;
p_Running->CPUTime += 2;
p_Running->state = WAIT;
Insert_Tail(p_Running);
}
}
int main()
{
int CurrentTime = 0;//當(dāng)前時(shí)間控制
printf("選擇算法:n"); //算法的選擇
printf("1.優(yōu)先數(shù)算法n");
printf("2.時(shí)間片輪轉(zhuǎn)算法n");
scanf("%d",&suanfa);
if(suanfa==1) //若選擇優(yōu)先數(shù)算法
{
ReadWork_priority(); //建立優(yōu)先數(shù)算法的進(jìn)程控制塊函數(shù)
while(head_Prioriy != NULL)
{
getchar(); //停住,防止在win98里不能察看
printf("nCurrentTime = %d :n",CurrentTime);
printf("NAME CPUTIME NEEDTIME PRIORITY STATEn");
CurrentTime++; //當(dāng)前時(shí)間增長
Processing_priority(); //調(diào)用優(yōu)先數(shù)算法的進(jìn)程查看函數(shù)
}
}
else if(suanfa==2) //若選擇時(shí)間片輪轉(zhuǎn)算法
{
ReadWork_roundRobin(); //建立時(shí)間片輪轉(zhuǎn)算法的進(jìn)程控制塊函數(shù)
PCB_RoundRobin *p = head_RoundRobin; //使輪轉(zhuǎn)的頭與pcb控制塊的指針一致
len_PCB_RoundRobin = 0; //長度先賦為0
while(p != NULL) //使P指向鏈尾,并記錄鏈長
{
len_PCB_RoundRobin++;
p = p->next;
}
int roundPCB = 0; //計(jì)數(shù)器先賦0
while(head_RoundRobin != NULL)
{
getchar(); //停住,防止在win98里不能察看
printf("nCurrentTime = %d :n",CurrentTime);
printf("NAME CPUTIME NEEDTIME COUNT ROUND STATEn");
CurrentTime += 2; //使當(dāng)前時(shí)間增二
if(roundPCB == len_PCB_RoundRobin)
{
p = head_RoundRobin;
len_PCB_RoundRobin = 0;
while(p != NULL)
{
p->round++;
len_PCB_RoundRobin++;
p = p->next;
}
roundPCB = 0;
}
roundPCB++; //計(jì)數(shù)器遞增
Processing_roundRobin(); //調(diào)用時(shí)間片輪轉(zhuǎn)的進(jìn)程查看函數(shù)
}
}
else //否則沒有相對應(yīng)的算法
{
printf("沒找到算法!n");
return 0;
}
return 0;
}