(2020年整理)P、V 原语的模拟实现.doc

上传人:摩西的****12 文档编号:136468871 上传时间:2020-06-28 格式:DOC 页数:8 大小:67.30KB
返回 下载 相关 举报
(2020年整理)P、V 原语的模拟实现.doc_第1页
第1页 / 共8页
(2020年整理)P、V 原语的模拟实现.doc_第2页
第2页 / 共8页
(2020年整理)P、V 原语的模拟实现.doc_第3页
第3页 / 共8页
(2020年整理)P、V 原语的模拟实现.doc_第4页
第4页 / 共8页
亲,该文档总共8页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《(2020年整理)P、V 原语的模拟实现.doc》由会员分享,可在线阅读,更多相关《(2020年整理)P、V 原语的模拟实现.doc(8页珍藏版)》请在金锄头文库上搜索。

1、P、V 原语的模拟实现一、实验目的本课题实习的目的是,加深对进程概念及进程管理各部分内容的理解;熟悉进程管理中主要数据结构的设计及进程调度算法,进程控制机构、同步结构、通迅机构的实施。要求设计一个允许n个进程并发运行的进程管理模拟糸统。该糸统包括有简单的进程控制、同步及通迅机构,其进程调度算法可任意选择。每个进程用一个PCB表示,其内容可根据具体情况设置。各进程之间应有一定的同步关糸。糸统在运行过程中应能显示或打印各进程的状态及有关参数的变化情况,以便观察诸进程的运行过程及糸统的管理过程。1) 理解信号量相关理论;2) 掌握记录型信号量结构;3) 掌握 P、V 原语实现机制。二、实验要求1)

2、输入给定代码;2) 进行功能测试并得出正确结果。3) 分析P和V函数功能模块;4) 在实验报告中画出P和V函数流程图;5) 撰写实验报告。三、实验内容本实验针对操作系统中信号量相关理论进行实验,要求实验者输入实验指导书提供的代码并进行测试。代码主要模拟信号量的 P 操作和 V 操作。1) 信号量信号量也称为信号锁,主要应用于进程间的同步和互斥,在用于互斥时,通常作为资源锁。信号量通常通过两个原子操作P和 V来访问。P操作使信号量的值+1,V操作使信号量的值-1。2) 记录型信号量记录型信号量采用了“让权等待”的策略,存在多个进程等待访问同一临界资源的情况,所以记录型信号量需要一个等待链表来存放

3、等待该信号量的进程控制块或进程号。在本实验中,使用记录型信号量。四、功能测试五#include#define TRUE 1#define FALSE 0#define MAXPRI 100#define NIL -1struct int id;char status;int nextwr;int priority;pcb1;struct int value;int firstwr; sem2;char savearea13,addr;int i,s1,s2,seed,exe=NIL;init( ) int j; for (j=0;j1;j+)pcbj.id=j;pcbj.status=r;pc

4、bj.nextwr=NIL;printf(n process%d priority?,j+1);scanf (%d,&i);pcbj.priority=i;sem0.value=1;sem0.firstwr=NIL;sem1.value=1;sem1.firstwr=NIL;for (i=1;i1;i+)for (j=0;j3;j+)saveareaij=0;float random ( )int m; if (seed0) m=-seed;else m=seed;seed=(24151*seed+11839)%64416;return(m/12565.0);timeint(char addr

5、)float x;x=random();if (x0.11)&(exe=0)return(FALSE);if (x0.66)&(exe=1)return(FALSE);if (x1.0)&(exe=2)return(FALSE);saveareaexe0=i;saveareaexe1=addr;pcbexe.status=t;printf(Times silce interruptnprocess%d enter into ready.n, exe+1);exe=NIL;return (TRUE);find()int j,pd=NIL, w=MAXPRI;for (j=0;j1;j+)if (

6、pcbj.status=r)if (pcbj.priorityw)w=pcbj.priority; pd=j; if (pd=NIL)for (j=0; j1;j+)if (pcbj.status=t)if (pcbj.priorityw) w=pcbj.priority; pd=j; return(pd) ;scheduler() int pd; if(pd=find()=NIL& exe=NIL)return(NIL); if (pd!=NIL) if(exe=NIL)pcbpd.status=e;exe=pd;printf(process%d is executing.n, exe+1)

7、;else if (pcbpd.priority=0) return(FALSE);block(se);saveareaexe0=i;saveareaexe1=ad;exe=NIL;return(TRUE);wakeup(int se)int w;w=semse.firstwr;if(w!=NIL) semse.firstwr=pcbw.nextwr; pcbw.status=r; printf(process%d is waken upn,w+1);v(int se,char ad)if(+semse.value0) return(FALSE);wakeup(se);saveareaexe1

8、=ad;saveareaexe0=i;return(TRUE);eexit(int n)pcbn.status=c;printf(process%d is completed!n, n+1); exe=NIL;processl() if(addr=a)goto a1; if(addr=b)goto b1; if(addr=c)goto c1; if(addr=d)goto d1; if(addr=e)goto e1; if(addr=f)goto f1;for(i=1;i6;i+) printf(process1 calls P on the semaphore1n); if(p(0,a)br

9、eak; a1:printf(process1 is executing in the cretical section 1n); if(timeint(b) break; b1:printf(s1=%dn,+s1); printf(process1 calls V on semaphorel and quit cretical section1n); if(v(0,c)break; c1:printf(process1 calls P on esemaphorel 2n); if(p(1,d)break; d1:printf(process1 is execting cretical sec

10、tion 2n); if(timeint(e)break; e1:printf(s2=%dn,+s2); printf(process1 calls V on semaphore2 and quit cretical section2n);if(v(1,f)break; f1:printf(process1 cycle count=%dn,i); if(i6) return; eexit(0);process2()if(addr=a)goto a2;if(addr=b)goto b2;if(addr=c)goto c2;if(addr=d)goto d2;if(addr=e)goto e2;i

11、f(addr=f)goto f2;for(i=1;i6;+i)printf(process2 calls P on the semaphore2n);if(p(1,a)break;a2:printf(process2 is executing in the cretical section2n);if(timeint(b)break;b2:printf(s2=%dn,+s2);printf(process2 calls V on semaphore2 and quit cretical section2.n);if(v(1,c)break;c2:printf(process2 calls P on esemaphore1.n);if(p(0,d)break;d2:printf(process2 is execting cretical section1.n);if(timeint(e)break;e2:printf(s1=%dn,+s1);printf(process2 calls V on semaphore1 and quit cretical section1.n);if(v(0,f)br

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 中学教育 > 其它中学文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号