P、V-原语的模拟实现.docx_第1页
P、V-原语的模拟实现.docx_第2页
P、V-原语的模拟实现.docx_第3页
P、V-原语的模拟实现.docx_第4页
P、V-原语的模拟实现.docx_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

P、V 原语的模拟实现一、实验目的本课题实习的目的是,加深对进程概念及进程管理各部分内容的理解;熟悉进程管理中主要数据结构的设计及进程调度算法,进程控制机构、同步结构、通迅机构的实施。要求设计一个允许n个进程并发运行的进程管理模拟糸统。该糸统包括有简单的进程控制、同步及通迅机构,其进程调度算法可任意选择。每个进程用一个PCB表示,其内容可根据具体情况设置。各进程之间应有一定的同步关糸。糸统在运行过程中应能显示或打印各进程的状态及有关参数的变化情况,以便观察诸进程的运行过程及糸统的管理过程。1) 理解信号量相关理论;2) 掌握记录型信号量结构;3) 掌握 P、V 原语实现机制。二、实验要求1) 输入给定代码;2) 进行功能测试并得出正确结果。3) 分析P和V函数功能模块;4) 在实验报告中画出P和V函数流程图;5) 撰写实验报告。三、实验内容本实验针对操作系统中信号量相关理论进行实验,要求实验者输入实验指导书提供的代码并进行测试。代码主要模拟信号量的 P 操作和 V 操作。1) 信号量信号量也称为信号锁,主要应用于进程间的同步和互斥,在用于互斥时,通常作为资源锁。信号量通常通过两个原子操作P和 V来访问。P操作使信号量的值+1,V操作使信号量的值-1。2) 记录型信号量记录型信号量采用了“让权等待”的策略,存在多个进程等待访问同一临界资源的情况,所以记录型信号量需要一个等待链表来存放等待该信号量的进程控制块或进程号。在本实验中,使用记录型信号量。四、功能测试五#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;pcbj.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)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 (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);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=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)break; 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 section 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;if(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)break;f2:printf(process2 cycle count=%dn,i); if(i6) return;eexit(1);process1()if (addr=a) goto a1;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 cretical 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()

温馨提示

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

评论

0/150

提交评论