操作系统实验报告一进程同步机构的模拟pv操作的实现.doc
约7页DOC格式手机打开展开
操作系统实验报告一进程同步机构的模拟pv操作的实现,操作系统实验报告一进程同步机构的模拟pv操作的实现 全文7页1309字 图文并实验要求:模拟实现用同步机构避免并发进程执行时可能出现的与时间有关的错误。进程是程序在一个数据集合上运行的过程,进程是并发执行的,也即系统中的多个进程轮流地占用处理器运行。我们把如干个进程都能进行访问和修改地那些变量成为公共变量。由于进程是并...
内容介绍
此文档由会员 黄药师 发布
操作系统实验报告一进程同步机构的模拟PV操作的实现
全文7页1309字 图文并
实验要求:
模拟实现用同步机构避免并发进程执行时可能出现的与时间有关的错误。
进程是程序在一个数据集合上运行的过程,进程是并发执行的,也即系统中的多个进程轮流地占用处理器运行。
我们把如干个进程都能进行访问和修改地那些变量成为公共变量。由于进程是并发执行的,所以,如果对进程访问公共变量不加限制,那么就会产生“与时间有关”的错误,即进程执行后,所得到的结果与访问公共变量的时间有关。为了防止这类错误,系统必须要用同步机构来控制进程对公共变量的访问。一般说,同步机构是由若干条原语——同步原语——所组成。本实验要求学生模拟PV操作同步机构的实现,模拟进程的并发执行,了解进程并发执行时同步机构的作用。
解题思路(流程图):
图4-5处理器调度程序流程
实验结果(部分源码):
#include
#include
#include
#include
#define INPUT "D:input.txt"//文件输入流的地址,可适当修改
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; //优先数算法的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; //时间片轮转算法的pcb控制模块
PCB_RoundRobin *head_RoundRobin = NULL; //头指针
int len_PCB_RoundRobin; //pcb模块的长度
void Insert_priority(PCB_Prioriy *p)//优先数算法的插入函数
{
PCB_Prioriy *p1, *p2;
bool insertTail = true;
if( head_Prioriy==NULL || p->priority > head_Prioriy->priority ) // 插入队首
{
p->next = head_Prioriy;
head_Prioriy = p;
}
else
{
p1 = head_Prioriy;
while(p1->next != NULL)
{
p1 = p1->next;
if( p1->priority < p->priority ) // 找到插入点
{
insertTail = false;
break;
}
}
if(insertTail) // 插入队尾
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)//时间片轮转算法的插入
{
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()//输出优先数算法的所有信息
{
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()//输出时间片转算法的所有信息
{
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() //建立进程控制块函数_优先数算法
{
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);//调用优先数算法的插入函数
p = (PCB_Prioriy*)malloc(sizeof(PCB_Prioriy));
}
}
void ReadWork_roundRobin()//建立进程控制块函数_时间片转算法
{
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);//时间片轮转算法的插入函数
p = (PCB_RoundRobin*)malloc(sizeof(PCB_RoundRobin));
}
}
void Processing_priority() //建立进程查看函数
{
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)//当needTime等于0时,说明此进程已完成
{
printf("进程%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()//建立进程查看函数
{
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)//当needTime此时已小于0,说明此进程已完成
{
printf("进程%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;//当前时间控制
printf("选择算法:n"); //算法的选择
printf("1.优先数算法n");
printf("2.时间片轮转算法n");
scanf("%d",&suanfa);
if(suanfa==1) //若选择优先数算法
{
ReadWork_priority(); //建立优先数算法的进程控制块函数
while(head_Prioriy != NULL)
{
getchar(); //停住,防止在win98里不能察看
printf("nCurrentTime = %d :n",CurrentTime);
printf("NAME CPUTIME NEEDTIME PRIORITY STATEn");
CurrentTime++; //当前时间增长
Processing_priority(); //调用优先数算法的进程查看函数
}
}
else if(suanfa==2) //若选择时间片轮转算法
{
ReadWork_roundRobin(); //建立时间片轮转算法的进程控制块函数
PCB_RoundRobin *p = head_RoundRobin; //使轮转的头与pcb控制块的指针一致
len_PCB_RoundRobin = 0; //长度先赋为0
while(p != NULL) //使P指向链尾,并记录链长
{
len_PCB_RoundRobin++;
p = p->next;
}
int roundPCB = 0; //计数器先赋0
while(head_RoundRobin != NULL)
{
getchar(); //停住,防止在win98里不能察看
printf("nCurrentTime = %d :n",CurrentTime);
printf("NAME CPUTIME NEEDTIME COUNT ROUND STATEn");
CurrentTime += 2; //使当前时间增二
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++; //计数器递增
Processing_roundRobin(); //调用时间片轮转的进程查看函数
}
}
else //否则没有相对应的算法
{
printf("没找到算法!n");
return 0;
}
return 0;
}
全文7页1309字 图文并
实验要求:
模拟实现用同步机构避免并发进程执行时可能出现的与时间有关的错误。
进程是程序在一个数据集合上运行的过程,进程是并发执行的,也即系统中的多个进程轮流地占用处理器运行。
我们把如干个进程都能进行访问和修改地那些变量成为公共变量。由于进程是并发执行的,所以,如果对进程访问公共变量不加限制,那么就会产生“与时间有关”的错误,即进程执行后,所得到的结果与访问公共变量的时间有关。为了防止这类错误,系统必须要用同步机构来控制进程对公共变量的访问。一般说,同步机构是由若干条原语——同步原语——所组成。本实验要求学生模拟PV操作同步机构的实现,模拟进程的并发执行,了解进程并发执行时同步机构的作用。
解题思路(流程图):
图4-5处理器调度程序流程
实验结果(部分源码):
#include
#include
#include
#include
#define INPUT "D:input.txt"//文件输入流的地址,可适当修改
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; //优先数算法的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; //时间片轮转算法的pcb控制模块
PCB_RoundRobin *head_RoundRobin = NULL; //头指针
int len_PCB_RoundRobin; //pcb模块的长度
void Insert_priority(PCB_Prioriy *p)//优先数算法的插入函数
{
PCB_Prioriy *p1, *p2;
bool insertTail = true;
if( head_Prioriy==NULL || p->priority > head_Prioriy->priority ) // 插入队首
{
p->next = head_Prioriy;
head_Prioriy = p;
}
else
{
p1 = head_Prioriy;
while(p1->next != NULL)
{
p1 = p1->next;
if( p1->priority < p->priority ) // 找到插入点
{
insertTail = false;
break;
}
}
if(insertTail) // 插入队尾
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)//时间片轮转算法的插入
{
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()//输出优先数算法的所有信息
{
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()//输出时间片转算法的所有信息
{
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() //建立进程控制块函数_优先数算法
{
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);//调用优先数算法的插入函数
p = (PCB_Prioriy*)malloc(sizeof(PCB_Prioriy));
}
}
void ReadWork_roundRobin()//建立进程控制块函数_时间片转算法
{
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);//时间片轮转算法的插入函数
p = (PCB_RoundRobin*)malloc(sizeof(PCB_RoundRobin));
}
}
void Processing_priority() //建立进程查看函数
{
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)//当needTime等于0时,说明此进程已完成
{
printf("进程%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()//建立进程查看函数
{
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)//当needTime此时已小于0,说明此进程已完成
{
printf("进程%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;//当前时间控制
printf("选择算法:n"); //算法的选择
printf("1.优先数算法n");
printf("2.时间片轮转算法n");
scanf("%d",&suanfa);
if(suanfa==1) //若选择优先数算法
{
ReadWork_priority(); //建立优先数算法的进程控制块函数
while(head_Prioriy != NULL)
{
getchar(); //停住,防止在win98里不能察看
printf("nCurrentTime = %d :n",CurrentTime);
printf("NAME CPUTIME NEEDTIME PRIORITY STATEn");
CurrentTime++; //当前时间增长
Processing_priority(); //调用优先数算法的进程查看函数
}
}
else if(suanfa==2) //若选择时间片轮转算法
{
ReadWork_roundRobin(); //建立时间片轮转算法的进程控制块函数
PCB_RoundRobin *p = head_RoundRobin; //使轮转的头与pcb控制块的指针一致
len_PCB_RoundRobin = 0; //长度先赋为0
while(p != NULL) //使P指向链尾,并记录链长
{
len_PCB_RoundRobin++;
p = p->next;
}
int roundPCB = 0; //计数器先赋0
while(head_RoundRobin != NULL)
{
getchar(); //停住,防止在win98里不能察看
printf("nCurrentTime = %d :n",CurrentTime);
printf("NAME CPUTIME NEEDTIME COUNT ROUND STATEn");
CurrentTime += 2; //使当前时间增二
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++; //计数器递增
Processing_roundRobin(); //调用时间片轮转的进程查看函数
}
}
else //否则没有相对应的算法
{
printf("没找到算法!n");
return 0;
}
return 0;
}