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

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

实验二体验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"

class Lock

{

public:

Lock(char* debugName); //initialize lock be FREE

~Lock();

char* getName() { return name; } //debugging assist

void Acquire(); //wait until the lock is FREE, then set it to BUSY

void Release(); //set lock to be FREE, waking up a thread waiting

//in Acquire if necessary

bool isHeldByCurrentThread(); // true if the current thread

// holds this lock. Useful for checking in Release, and in

// Condition variable ops below

private:

char* name;

Thread *LockedThread;

bool LockValue;

List *queue;

};

class Condition

{

Condition(char* debugName); //initialize condition to "no one waiting"

~Condition();//析构函数

char* getName() { return name; }

void Wait(Lock *conditionLock);

//release the lock, relinquish the CPU until signaled,then re-acquire the lock void Signal(Lock *conditionLock);

//wake up a thread, if there are any waiting on the condition

void Broadcast(Lock *conditionLock);

//wake up all threads waiting on the condition

private:

char* name;

List *queue;

};

#include "copyright.h"

#include "system.h"

#include "thread.h"

#include "list.h"

#include "synch-sleep.h"

Lock::Lock(char* debugName) //initialize lock be FREE

{

name = debugName;

queue = new List;

LockedThread = new Thread("LockedThread");

LockValue = 0;

}

Lock::~Lock() //析构函数

{

delete queue;

delete LockedThread;

}

void

Lock::Acquire() //wait until the lock is FREE, then set it to BUSY

{

IntStatus oldLevel = interrupt->SetLevel(IntOff);

while ( LockValue )

{

queue->Append((void *)currentThread);

//put currentThread at the end of the list

currentThread->Sleep();

LockedThread = currentThread;

LockValue = 1;

(void) interrupt->SetLevel(oldLevel);

}

void

Lock::Release()

//set lock to be FREE, waking up a thread waiting in Acquire if necessary

{

Thread *thread;

ASSERT( isHeldByCurrentThread() );

IntStatus oldLevel = interrupt->SetLevel(IntOff);

LockedThread = NULL;

thread = (Thread *)queue->Remove();

if ( thread != NULL )

scheduler->ReadyToRun(thread);

LockValue=false;

(void) interrupt->SetLevel(oldLevel);

}

bool

Lock::isHeldByCurrentThread()

// true if the current thread holds this lock. Useful for checking in Release,

//and in Condition variable ops below

{

if ( LockedThread == currentThread && LockValue )

return true;

else

return false;

}

Condition::Condition(char* debugName) //initialize condition to "no one waiting" {

name = debugName;

queue = new List;

}

Condition::~Condition() //析构函数

{

delete queue;

}

void

相关文档
最新文档