操作系统体验Nachos下的并发程序设计.docx
- 文档编号:26898707
- 上传时间:2023-06-23
- 格式:DOCX
- 页数:37
- 大小:110.24KB
操作系统体验Nachos下的并发程序设计.docx
《操作系统体验Nachos下的并发程序设计.docx》由会员分享,可在线阅读,更多相关《操作系统体验Nachos下的并发程序设计.docx(37页珍藏版)》请在冰豆网上搜索。
操作系统体验Nachos下的并发程序设计
实验二体验Nachos下的并发程序设计
一、实验人员:
二、实验目的:
实现Nachos的同步机制:
锁和条件变量,并利用这些同步机制实现一些工具类。
三、实验内容:
1.实现锁机制和条件变量
2.实现一个线程安全的表结构
3.实现一个大小受限的缓冲区
四、实验步骤:
1.实现锁机制和条件变量
1.1用Thread:
:
Sleep实现锁机制和条件变量
相关代码:
Synch-sleep.h
#include"copyright.h"
#include"thread.h"
#include"list.h"
#include"system.h"
classLock
{
public:
Lock(char*debugName);//initializelockbeFREE
~Lock();
char*getName(){returnname;}//debuggingassist
voidAcquire();//waituntilthelockisFREE,thensetittoBUSY
voidRelease();//setlocktobeFREE,wakingupathreadwaiting
//inAcquireifnecessary
boolisHeldByCurrentThread();//trueifthecurrentthread
//holdsthislock.UsefulforcheckinginRelease,andin
//Conditionvariableopsbelow
private:
char*name;
Thread*LockedThread;
boolLockValue;
List*queue;
};
classCondition
{
public:
Condition(char*debugName);//initializeconditionto"noonewaiting"
~Condition();//析构函数
char*getName(){returnname;}
voidWait(Lock*conditionLock);
//releasethelock,relinquishtheCPUuntilsignaled,thenre-acquirethelock
voidSignal(Lock*conditionLock);
//wakeupathread,ifthereareanywaitingonthecondition
voidBroadcast(Lock*conditionLock);
//wakeupallthreadswaitingonthecondition
private:
char*name;
List*queue;
};
Synch-sleep.cc
#include"copyright.h"
#include"system.h"
#include"thread.h"
#include"list.h"
#include"synch-sleep.h"
Lock:
:
Lock(char*debugName)//initializelockbeFREE
{
name=debugName;
queue=newList;
LockedThread=newThread("LockedThread");
LockValue=0;
}
Lock:
:
~Lock()//析构函数
{
deletequeue;
deleteLockedThread;
}
void
Lock:
:
Acquire()//waituntilthelockisFREE,thensetittoBUSY
{
IntStatusoldLevel=interrupt->SetLevel(IntOff);
while(LockValue)
{
queue->Append((void*)currentThread);
//putcurrentThreadattheendofthelist
currentThread->Sleep();
}
LockedThread=currentThread;
LockValue=1;
(void)interrupt->SetLevel(oldLevel);
}
void
Lock:
:
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);
}
bool
Lock:
:
isHeldByCurrentThread()
//trueifthecurrentthreadholdsthislock.UsefulforcheckinginRelease,
//andinConditionvariableopsbelow
{
if(LockedThread==currentThread&&LockValue)
returntrue;
else
returnfalse;
}
Condition:
:
Condition(char*debugName)//initializeconditionto"noonewaiting"
{
name=debugName;
queue=newList;
}
Condition:
:
~Condition()//析构函数
{
deletequeue;
}
void
Condition:
:
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);
}
void
Condition:
:
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);
}
void
Condition:
:
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
#include"copyright.h"
#include"thread.h"
#include"list.h"
classSemaphore
{
public:
Semaphore(char*debugName,intinitialValue);//设置信号量初值
~Semaphore();//析构函数
char*getName(){returnname;}//debuggingassist
voidP();//waitsuntilvalue>0,thendecrement
voidV();//increment,wakingupathreadwaitinginP()ifnecessary
//均为原子操作
private:
char*name;//usefulfordebugging
intvalue;//信号量的值,均为非负的
List*queue;//threadswaitinginP()forthevaluetobe>0
};
classLock
{
public:
Lock(char*debugName);//initializelocktobeFREE
~Lock();//析构函数
char*getName(){returnname;}//debuggingassist
voidAcquire();//waituntilthelockisFREE,thensetittoBUSY
voidRelease();//setlocktobeFREE,wakingupathreadwaiting
//均为原子操作
boolisHeldByCurrentThread();//trueifthecurrentthread
//holdsthislock.UsefulforcheckinginRelease,andin
//Conditionvariableopsbelow.
private:
char*name;//fordebugging
Thread*LockedThread;
boolLockValue;
Semaphore*semaphore;
intthreadnum;
};
classCondition
{
public:
Condition(char*debugName);//初始化条件变量为"noonewaiting"
~Condition();//析构函数
char*getName(){returnname;}//debuggingassist
voidWait(Lock*conditionLock);//releasethelock,relinquishtheCPUuntilsignaled,
//thenre-acquirethelock
//为原子操作
voidSignal(Lock*conditionLock);//wakeupathread,
//ifthereareanywaitingonthecondition
voidBroadcast(Lock*conditionLock);
//wakeupallthreadswaitingonthecondition
private:
char*name;
Semaphore*semaphore;
intthreadnum;
};
#endif
Synch-sem.cc
#include"copyright.h"
#include"synch-sem.h"
#include"system.h"
Semaphore:
:
Semaphore(char*debugName,intinitialValue)
{
name=debugName;
value=initialValue;
queue=newList;
}
Semaphore:
:
~Semaphore()
{
deletequeue;
}
void
Semaphore:
:
P()
{
IntStatusoldLevel=interrupt->SetLevel(IntOff);//禁用中断
while(value==0){//semaphorenotavailable
queue->Append((void*)currentThread);//sogotosleep
currentThread->Sleep();
}
value--;//semaphoreavailable,
//consumeitsvalue
(void)interrupt->SetLevel(oldLevel);//恢复中断
}
void
Semaphore:
:
V()
{
Thread*thread;
IntStatusoldLevel=interrupt->SetLevel(IntOff);//禁用中断
thread=(Thread*)queue->Remove();
if(thread!
=NULL)//makethreadready,consumingtheVimmediately
scheduler->ReadyToRun(thread);
value++;
(void)interrupt->SetLevel(oldLevel);//恢复中断
}
Lock:
:
Lock(char*debugName)//initializelocktobeFREE
{
name=debugName;
LockedThread=newThread("LockedThread");
LockValue=0;
threadnum=0;
semaphore=newSemaphore("locksem",0);
}
Lock:
:
~Lock()//析构函数
{
deleteLockedThread;
deletesemaphore;
}
void
Lock:
:
Acquire()//waituntilthelockisFREE,thensetittoBUSY
{
ASSERT(!
isHeldByCurrentThread());//checkthelockifFREE
while(LockValue)
semaphore->P();
LockedThread=currentThread;
LockValue=1;
}
void
Lock:
:
Release()//setlocktobeFREE,wakingupathreadwaiting
{
ASSERT(isHeldByCurrentThread());//checkthelockifBUSY
LockedThread=NULL;
LockValue=0;
semaphore->V();
}
boolLock:
:
isHeldByCurrentThread()
{
if((LockedThread==currentThread)&&LockValue)
returntrue;
else
returnfalse;
}
Condition:
:
Condition(char*debugName)//initializeconditionto"noonewaiting"
{
name=debugName;
semaphore=newSemaphore("Conditionsem",0);
}
Condition:
:
~Condition()
{
deletesemaphore;
}
void
Condition:
:
Wait(Lock*conditionLock)
//releasethelock,relinquishtheCPUuntilsignaled,thenre-acquirethelock
{
ASSERT(conditionLock->isHeldByCurrentThread());//checkthelockifheld
conditionLock->Release();
semaphore->P();
threadnum++;
conditionLock->Acquire();
}
void
Condition:
:
Signal(Lock*conditionLock)//wakeupathread,ifthereareanywaitingonthecondition
{
ASSERT(conditionLock->isHeldByCurrentThread());
if(threadnum>0)
{
semaphore->V();
threadnum--;
}
}
void
Condition:
:
Broadcast(Lock*conditionLock)//wakeupallthreadswaitingonthecondition
{
ASSERT(conditionLock->isHeldByCurrentThread());
while(threadnum>0)
{
semaphore->V();
threadnum--;
}
}
1.4用锁机制和条件变量修改双向有序链表
修改了dllist.h,dllist.cc,加入了锁和条件锁,为了方便测试,在threadtest.cc里加入一个变量flag,flag=0时不使用锁,flag=1时使用锁。
修改部分主要为dllist.cc的Remove和SortedInsert函数
void*DLList:
:
Remove(int*keyPtr)//removefromheadoflist
{
DLLElement*element;
if(flag)
lock->Acquire();
if(IsEmpty())
{
if(flag)
lock->Release();
returnNULL;
}
void*retitem;
element=first;
*keyPtr=first->key;
if(err_type==1)
{
currentThread->Yield();
}
retitem=element->item;
if(first==last)
{
first=NULL;
last=NULL;
}
else
{
if(err_type==1)
{
currentThread->Yield();
}
first=element->next;
first->prev=NULL;
}
deleteelement;
if(flag)
lock->Release();
returnretitem;
}
voidDLList:
:
SortedInsert(void*item,intsortKey)//routinestoput/getitemson/offlistinorder(sortedbykey)
{
DLLElement*insertItem=newDLLElement(item,sortKey);
DLLElement*ptr=first;
if(flag)
lock->Acquire();
if(IsEmpty())
{
first=insertItem;
if(err_type==2)
{
currentThread->Yield();
}
last=insertItem;
}
else
{
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 操作系统 体验 Nachos 并发 程序设计