操作系统-体验Nachos下的并发程序设计

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

实验二体验Nachos下的并发程序设计一、实验人员:二、实验目的:实现Nachos的同步机制:锁和条件变量,并利用这些同步机制实现一些工具类。三、实验内容:1.实现锁机制和条件变量2.实现一个线程安全的表结构3.实现一个大小受限的缓冲区四、实验步骤:1.实现锁机制和条件变量1.1用Thread::Sleep实现锁机制和条件变量相关代码:Synch-sleep.h#includecopyright.h#includethread.h#includelist.h#includesystem.hclassLock{public:Lock(char*debugName);//initializelockbeFREE~Lock();char*getName(){returnname;}//debuggingassistvoidAcquire();//waituntilthelockisFREE,thensetittoBUSYvoidRelease();//setlocktobeFREE,wakingupathreadwaiting//inAcquireifnecessaryboolisHeldByCurrentThread();//trueifthecurrentthread//holdsthislock.UsefulforcheckinginRelease,andin//Conditionvariableopsbelowprivate:char*name;Thread*LockedThread;boolLockValue;List*queue;};classCondition{public:Condition(char*debugName);//initializeconditiontonoonewaiting~Condition();//析构函数char*getName(){returnname;}voidWait(Lock*conditionLock);//releasethelock,relinquishtheCPUuntilsignaled,thenre-acquirethelockvoidSignal(Lock*conditionLock);//wakeupathread,ifthereareanywaitingontheconditionvoidBroadcast(Lock*conditionLock);//wakeupallthreadswaitingontheconditionprivate:char*name;List*queue;};Synch-sleep.cc#includecopyright.h#includesystem.h#includethread.h#includelist.h#includesynch-sleep.hLock::Lock(char*debugName)//initializelockbeFREE{name=debugName;queue=newList;LockedThread=newThread(LockedThread);LockValue=0;}Lock::~Lock()//析构函数{deletequeue;deleteLockedThread;}voidLock::Acquire()//waituntilthelockisFREE,thensetittoBUSY{IntStatusoldLevel=interrupt-SetLevel(IntOff);while(LockValue){queue-Append((void*)currentThread);//putcurrentThreadattheendofthelistcurrentThread-Sleep();}LockedThread=currentThread;LockValue=1;(void)interrupt-SetLevel(oldLevel);}voidLock::Release()//setlocktobeFREE,wakingupathreadwaitinginAcquireifnecessary{Thread*thread;ASSERT(isHeldByCurrentThread());IntStatusoldLevel=interrupt-SetLevel(IntOff);LockedThread=NULL;thread=(Thread*)queue-Remove();if(thread!=NULL)scheduler-ReadyToRun(thread);LockValue=false;(void)interrupt-SetLevel(oldLevel);}boolLock::isHeldByCurrentThread()//trueifthecurrentthreadholdsthislock.UsefulforcheckinginRelease,//andinConditionvariableopsbelow{if(LockedThread==currentThread&&LockValue)returntrue;elsereturnfalse;}Condition::Condition(char*debugName)//initializeconditiontonoonewaiting{name=debugName;queue=newList;}Condition::~Condition()//析构函数{deletequeue;}voidCondition::Wait(Lock*conditionLock)//releasethelock,relinquishtheCPUuntilsignaled,thenre-acquirethelock{ASSERT(conditionLock-isHeldByCurrentThread());IntStatusoldLevel=interrupt-SetLevel(IntOff);queue-Append((void*)currentThread);conditionLock-Release();currentThread-Sleep();conditionLock-Acquire();(void)interrupt-SetLevel(oldLevel);}voidCondition::Signal(Lock*conditionLock)//wakeupathread,ifthereareanywaitingonthecondition{Thread*thread;ASSERT(conditionLock-isHeldByCurrentThread());IntStatusoldLevel=interrupt-SetLevel(IntOff);thread=(Thread*)queue-Remove();if(thread!=NULL)scheduler-ReadyToRun(thread);(void)interrupt-SetLevel(oldLevel);}voidCondition::Broadcast(Lock*conditionLock)//wakeupallthreadswaitingonthecondition{Thread*thread;ASSERT(conditionLock-isHeldByCurrentThread());IntStatusoldLevel=interrupt-SetLevel(IntOff);thread=(Thread*)queue-Remove();while(thread!=NULL){scheduler-ReadyToRun(thread);thread=(Thread*)queue-Remove();}(void)interrupt-SetLevel(oldLevel);}1.2用Semaphore实现锁机制和条件变量相关代码:Synch-sem.h#ifndefSYNCH_SEM_H#defineSYNCH_SEM_H#includecopyright.h#includethread.h#includelist.hclassSemaphore{public:Semaphore(char*debugName,intinitialValue);//设置信号量初值~Semaphore();//析构函数char*getName(){returnname;}//debuggingassistvoidP();//waitsuntilvalue0,thendecrementvoidV();//increment,wakingupathreadwaitinginP()ifnecessary//均为原子操作private:char*name;//usefulfordebuggingintvalue;//信号量的值,均为非负的List*queue;//threadswaitinginP()forthevaluetobe0};classLock{public:Lock(char*debugName);//initializelocktobeFREE~Lock();//析构函数char*getName(){returnname;}//debuggingassistvoidAcquire();//waituntilthelockisFREE,thensetittoBUSYvoidRelease();//setlocktobeFREE,wakingupathreadwaiting//均为原子操作boolisHeldByCurrentThread();//trueifthecurrentthread//holdsthislock.UsefulforcheckinginRelease,andin//Conditionvariableopsbelow.private:char*name;//fordebuggingThread*LockedThread;boolLockValue;Semaphore*semaphore;intthreadnum;};classCondition{public:Condition(char*debugName);//初始化条件变量为noonewaiting~Condition();//析构函数char*getName(){returnname;}//debuggingassistvoidWait(Lock*conditionLock);//releasethelock,relinquishtheCPUuntilsignaled,//thenre-acquirethelock//为原子操作voidSignal(Lock*conditionLock);//wakeupathread,//ifthereareanywaitingontheconditionvoidBroadcast(Lock*conditionLock);//wakeupallthreadswaitingontheconditionprivate:char*name;Semaphore*semaphore;intthreadnum;};#endifSynch-sem.cc#includecopyright.h#includesynch-sem.h#includesystem.hSemaphore::Semaphore(char*debugName,intinitialValue){name=debugName;value=initialValue;queue=newList;}Semaphore::~Semaphore(){deletequeue;}voidSemaphore::P(){IntStatusoldLevel=interrupt-SetLevel(IntO

1 / 23
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功