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

上传人:b****5 文档编号:14484916 上传时间:2023-06-23 格式:DOCX 页数:37 大小:110.24KB
下载 相关 举报
操作系统体验Nachos下的并发程序设计.docx_第1页
第1页 / 共37页
操作系统体验Nachos下的并发程序设计.docx_第2页
第2页 / 共37页
操作系统体验Nachos下的并发程序设计.docx_第3页
第3页 / 共37页
操作系统体验Nachos下的并发程序设计.docx_第4页
第4页 / 共37页
操作系统体验Nachos下的并发程序设计.docx_第5页
第5页 / 共37页
操作系统体验Nachos下的并发程序设计.docx_第6页
第6页 / 共37页
操作系统体验Nachos下的并发程序设计.docx_第7页
第7页 / 共37页
操作系统体验Nachos下的并发程序设计.docx_第8页
第8页 / 共37页
操作系统体验Nachos下的并发程序设计.docx_第9页
第9页 / 共37页
操作系统体验Nachos下的并发程序设计.docx_第10页
第10页 / 共37页
操作系统体验Nachos下的并发程序设计.docx_第11页
第11页 / 共37页
操作系统体验Nachos下的并发程序设计.docx_第12页
第12页 / 共37页
操作系统体验Nachos下的并发程序设计.docx_第13页
第13页 / 共37页
操作系统体验Nachos下的并发程序设计.docx_第14页
第14页 / 共37页
操作系统体验Nachos下的并发程序设计.docx_第15页
第15页 / 共37页
操作系统体验Nachos下的并发程序设计.docx_第16页
第16页 / 共37页
操作系统体验Nachos下的并发程序设计.docx_第17页
第17页 / 共37页
操作系统体验Nachos下的并发程序设计.docx_第18页
第18页 / 共37页
操作系统体验Nachos下的并发程序设计.docx_第19页
第19页 / 共37页
操作系统体验Nachos下的并发程序设计.docx_第20页
第20页 / 共37页
亲,该文档总共37页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

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

《操作系统体验Nachos下的并发程序设计.docx》由会员分享,可在线阅读,更多相关《操作系统体验Nachos下的并发程序设计.docx(37页珍藏版)》请在冰点文库上搜索。

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

操作系统体验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

{

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 农林牧渔 > 林学

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2