实验三-操作系统实验Nacho(共11页).doc
《实验三-操作系统实验Nacho(共11页).doc》由会员分享,可在线阅读,更多相关《实验三-操作系统实验Nacho(共11页).doc(11页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、精选优质文档-倾情为你奉上实验三 操作系统实验Nacho一、实验人员: 二、实验目的:本次实验的目的在于掌握使用nachos中的线程序解决较为复杂的并发问题。实验内容分三部分:实现事件栅栏原语并进行正确性测试;实现闹钟原语并进行正确性测试;利用事件栅栏和闹钟原语来解决电梯问题(详细内容请看nachos-labs.pdf)。三、实验内容:1.实现事件栅栏原语2.实现闹钟原语3. 解决电梯问题四、实验步骤:1. 实现事件栅栏原语EventBarrier.h#ifndef EVENTBARRIER_H#define EVENTBARRIER_H#include synch-sem.h#define
2、SIGNALED 1 #define UNSIGNALED 0 class EventBarrierpublic:EventBarrier();EventBarrier();void Wait();void Signal();void Complete();int Waiters();private:bool state;Condition *waits;Condition *waitc;Lock *barrier;Lock *inbarrier;int waiter;#endifEventBarrier.cc#include EventBarrier.h#include thread.hEv
3、entBarrier:EventBarrier()waits=new Condition(waitsignal);waitc=new Condition(waitcomplete);barrier=new Lock(barrier);inbarrier=new Lock(inbarrier);state=UNSIGNALED;waiter=0;EventBarrier:EventBarrier()delete waits;delete waitc;void EventBarrier:Wait()barrier-Acquire();waiter+;while(state=UNSIGNALED)w
4、aits-Wait(barrier);barrier-Release();void EventBarrier:Signal()barrier-Acquire();state=SIGNALED;waits-Broadcast(barrier);barrier-Release();inbarrier-Acquire();waitc-Wait(inbarrier);inbarrier-Release();state=UNSIGNALED;void EventBarrier:Complete()inbarrier-Acquire();waiter-;if(waiter=0)waitc-Broadcas
5、t(inbarrier);elsewaitc-Wait(inbarrier);inbarrier-Release();int EventBarrier:Waiters()return waiter;2. 实现闹钟原语Alarm.h#ifndef ALARM_H#define ALARM_H#include system.h#include list.hclass Alarmpublic:Alarm();Alarm();void Pause(int howLong);void Wakeup();int Getpausenum();private:List *queue;int pausenum;
6、int leftime;#endifAlarm.cc#include system.h#include thread.h#include Alarm.hextern Alarm *alarm;void check(int which)while(alarm-Getpausenum()!=0)currentThread-Yield();currentThread-Finish();Alarm:Alarm()queue=new List();pausenum=0;Alarm:Alarm()queue-List();void Alarm:Pause(int howLong)Thread *t;pau
7、senum+;if(pausenum=1)t=new Thread(forked thread);t-Fork(check,0);if(howLongtotalTicks+howLong*TimerTicks*10000;IntStatus oldlevel=interrupt-SetLevel(IntOff);queue-SortedInsert(currentThread,leftime);currentThread-Sleep();(void) interrupt-SetLevel(oldlevel);void Alarm:Wakeup()Thread *thread; int ptim
8、e=-1;IntStatus oldLevel = interrupt-SetLevel(IntOff); thread = (Thread *)queue-SortedRemove(&ptime); (void) interrupt-SetLevel(oldLevel); while( thread != NULL ) if(stats-totalTicks=ptime)scheduler-ReadyToRun(thread);pausenum-;oldLevel = interrupt-SetLevel(IntOff); thread = (Thread *)queue-SortedRem
9、ove(&ptime); (void) interrupt-SetLevel(oldLevel);continue; elseoldLevel = interrupt-SetLevel(IntOff); queue-SortedInsert(thread,ptime); (void) interrupt-SetLevel(oldLevel); break; intAlarm:Getpausenum()return pausenum;3.实现单个电梯Elevator.hclass Elevatorpublic:Elevator(char *debugname,int numfloors,int
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 实验 操作系统 Nacho 11
限制150内