P、V-原语的模拟实现

上传人:积*** 文档编号:121569990 上传时间:2022-07-19 格式:DOCX 页数:13 大小:55.52KB
收藏 版权申诉 举报 下载
P、V-原语的模拟实现_第1页
第1页 / 共13页
P、V-原语的模拟实现_第2页
第2页 / 共13页
P、V-原语的模拟实现_第3页
第3页 / 共13页
资源描述:

《P、V-原语的模拟实现》由会员分享,可在线阅读,更多相关《P、V-原语的模拟实现(13页珍藏版)》请在装配图网上搜索。

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

12、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)break;f2:printf(process2 cycle count=%dn,i); if(i6) return;eexit(1);process1()if (addr=a) goto a1

13、;if (addr=b) goto b1;if (addr=c) goto c1;for (i=1;i6; +i)printf(process1,calls P on semaphore2n);if(p(1,a) break; / * process 1 is biocked * /a1: printf(process 1 is execcuting on its cretical sectionn);if(timeint(b) break;b1: printf (s2=%dn, +s2); printf (process1 calls V on semapore2 and quit cret

14、ical sectionn);if(v(1,c) break; / * wake up a biocked process * /c1: printf(process1 cycien count=%dn,i); if(i6) return;eexit(2); main ( )int k;printf (* * * * process management * * * * nn);init();printf(s1=%d, s2=%dn, s1, s2);printf(process1, process2, process1 are all in ready ! n);for ( ; ;)if (k=scheduler()!=NIL)switch(k) case 0: process1(); break; case 1: process2(); break; case 2: process1(); break; default: printf(process identifer errorn); break;else break;printf (s1=%d, s2=%dn, s1, s2);printf (n * * * * END * * * * n);

展开阅读全文
温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!