




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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<stdio.h>#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;j<1;j+)pcbj.id
4、=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;i<1;i+)for (j=0;j<3;j+)saveareaij='0'float random ( )int m; if (seed<0) m=-seed;
5、else m=seed;seed=(24151*seed+11839)%64416;return(m/12565.0);timeint(char addr)float x;x=random();if (x<0.11)&&(exe=0)return(FALSE);if (x<0.66)&&(exe=1)return(FALSE);if (x<1.0)&&(exe=2)return(FALSE);saveareaexe0=i;saveareaexe1=addr;pcbexe.status='t'printf(&quo
6、t;Times silce interrupt'nprocess%d enter into ready.n", exe+1);exe=NIL;return (TRUE);find()int j,pd=NIL, w=MAXPRI;for (j=0;j<1;j+)if (pcbj.status='r')if (pcbj.priority<w)w=pcbj.priority; pd=j; if (pd=NIL)for (j=0; j<1;j+)if (pcbj.status='t')if (pcbj.priority<w) w
7、=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<pcbexe.priority) pcbexe.status='r'printf("process%d
8、 enter into readyn", exe+1);pcbpd.status='e'exe=pd;printf ("process%d enter into readyn", exe+1);i=saveareaexe0;addr=saveareaexe1;return (exe);block(int se)int w;printf("process%d is blockedn", exe+1);pcbexe.status='w' pcbexe.nextwr=NIL;if (w=semse.firstwr)=N
9、IL)semse.firstwr=exe;else while(pcbw.nextwr!=NIL)w=pcbw.nextwr;pcbw.nextwr=exe;p(int se,char ad)if (-semse.value>=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
10、("process%d is waken upn",w+1);v(int se,char ad)if(+semse.value>0) 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
11、9;)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;i<6;i+) printf("process1 calls P on the semaphore1n"); if(p(0,'a')break; a1:printf("process1 is executing in the cretical section 1n")
12、; 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 cret
13、ical 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(i<6) return; eexit(0);process2()if(addr='a
14、9;)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;i<6;+i)printf("process2 calls P on the semaphore2n");if(p(1,'a')break;a2:printf("process2 is executing in the creti
15、cal 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 e
16、xecting 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(i<6) return;eexit(1);process1()if (addr=
17、'a') goto a1;if (addr='b') goto b1;if (addr='c') goto c1;for (i=1;i<6; +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
18、39;) 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(i<6) 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
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 发动机燃烧优化方案
- 职业教育产教融合平台建设方案
- 化学品检测实验室管理规程
- 2025重庆市万州区长坪乡人民政府非全日制公益性岗位招聘1人笔试备考题库及答案解析
- 2025中国光大银行股份有限公司昆明分行社会招聘笔试模拟试题及答案解析
- 内部审计流程规范
- 市场研究分析师的市场调研方法和数据分析
- 2025兴业银行总行国际业务部交易银行部招聘考试备考试题及答案解析
- 打造个性化营销赢得市场
- 2025土地估价师考试《管理基础》模拟真题及答案
- 不干胶贴标机设计学士学位论文
- 《劳动合同书》-河南省人力资源和社会保障厅劳动关系处监制(2016.11.15)
- 钢轨检测报告
- 战略管理:概念与案例
- GB/T 3505-2009产品几何技术规范(GPS)表面结构轮廓法术语、定义及表面结构参数
- GB/T 11186.1-1989涂膜颜色的测量方法第一部分:原理
- 09S304 卫生设备安装图集
- 自动化导论全套课件
- 微纳加工课件
- 危重病人紧急气道管理课件
- 跟痛症教学讲解课件
评论
0/150
提交评论