版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、精選優(yōu)質(zhì)文檔-傾情為你奉上實驗二 體驗Nachos下的并發(fā)程序設(shè)計一、實驗人員: 二、實驗目的:實現(xiàn)Nachos的同步機制:鎖和條件變量,并利用這些同步機制實現(xiàn)一些工具類。三、實驗內(nèi)容:1.實現(xiàn)鎖機制和條件變量2.實現(xiàn)一個線程安全的表結(jié)構(gòu)3.實現(xiàn)一個大小受限的緩沖區(qū)四、實驗步驟:1.實現(xiàn)鎖機制和條件變量1.1用Thread:Sleep實現(xiàn)鎖機制和條件變量相關(guān)代碼:Synch-sleep.h#include copyright.h #include thread.h #include list.h #include system.h class Lockpublic: Lock(char* de
2、bugName); /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 th
3、read / 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 public: Condition(char* debugName); /initialize condition to no one waiting Condition();/析構(gòu)函數(shù) char* getName() return n
4、ame; 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
5、: char* name; List *queue; Synch-sleep.cc#include copyright.h#include system.h#include thread.h#include list.h#include synch-sleep.hLock:Lock(char* debugName) /initialize lock be FREE name = debugName; queue = new List; LockedThread = new Thread(LockedThread); LockValue = 0; Lock:Lock() /析構(gòu)函數(shù) delete
6、 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; L
7、ockValue = 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();
8、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
9、) return true; else return false; Condition:Condition(char* debugName) /initialize condition to no one waiting name = debugName; queue = new List; Condition:Condition() /析構(gòu)函數(shù) delete queue; void Condition:Wait(Lock *conditionLock) /release the lock, relinquish the CPU until signaled,then re-acquire t
10、he lock ASSERT(conditionLock-isHeldByCurrentThread(); IntStatus oldLevel = interrupt-SetLevel(IntOff); queue-Append(void *)currentThread); conditionLock-Release(); currentThread-Sleep(); conditionLock-Acquire(); (void) interrupt-SetLevel(oldLevel); void Condition:Signal(Lock *conditionLock) /wake up
11、 a thread, if there are any waiting on the condition Thread *thread; ASSERT( conditionLock-isHeldByCurrentThread() ); IntStatus oldLevel = interrupt-SetLevel(IntOff); thread = (Thread *)queue-Remove(); if ( thread != NULL ) scheduler-ReadyToRun(thread); (void) interrupt-SetLevel(oldLevel); void Cond
12、ition:Broadcast(Lock* conditionLock) /wake up all threads waiting on the condition Thread *thread; ASSERT( conditionLock-isHeldByCurrentThread(); IntStatus oldLevel = interrupt-SetLevel(IntOff); thread = (Thread *)queue-Remove(); while ( thread != NULL ) scheduler-ReadyToRun(thread); thread = (Threa
13、d *)queue-Remove(); (void) interrupt-SetLevel(oldLevel); 1.2用Semaphore實現(xiàn)鎖機制和條件變量相關(guān)代碼:Synch-sem.h#ifndef SYNCH_SEM_H#define SYNCH_SEM_H#include copyright.h #include thread.h #include list.h class Semaphore public: Semaphore(char* debugName, int initialValue); /設(shè)置信號量初值 Semaphore(); /析構(gòu)函數(shù) char* getName
14、() return name; /debugging assist void P(); /waits until value 0, then decrement void V(); /increment, waking up a thread waiting in P() if necessary /均為原子操作 private: char *name; /useful for debugging int value; /信號量的值,均為非負的 List *queue; /threads waiting in P() for the value to be 0 ; class Lock pub
15、lic: Lock(char* debugName); /initialize lock to be FREE Lock(); /析構(gòu)函數(shù) 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 /均為原子操作 bool isHeldByCurrentThread(); / true if the
16、current thread / holds this lock. Useful for checking in Release, and in / Condition variable ops below. private: char* name; /for debugging Thread *LockedThread; bool LockValue; Semaphore *semaphore; int threadnum; ; class Condition public: Condition(char* debugName); /初始化條件變量為no one waiting Condit
17、ion(); /析構(gòu)函數(shù) char* getName() return name; /debugging assist 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 *cond
18、itionLock); /wake up all threads waiting on the condition private: char* name; Semaphore *semaphore; int threadnum; ; #endifSynch-sem.cc#include copyright.h #include synch-sem.h #include system.h Semaphore:Semaphore(char* debugName, int initialValue) name = debugName; value = initialValue; queue = n
19、ew List; Semaphore:Semaphore() delete queue; void Semaphore:P() IntStatus oldLevel = interrupt-SetLevel(IntOff);/禁用中斷 while (value = 0) / semaphore not available queue-Append(void *)currentThread);/ so go to sleep currentThread-Sleep(); value-; / semaphore available, / consume its value (void) inter
20、rupt-SetLevel(oldLevel);/恢復中斷 void Semaphore:V() Thread *thread; IntStatus oldLevel = interrupt-SetLevel(IntOff);/禁用中斷 thread = (Thread *)queue-Remove(); if (thread != NULL) / make thread ready, consuming the V immediately scheduler-ReadyToRun(thread); value+; (void) interrupt-SetLevel(oldLevel);/恢復
21、中斷 Lock:Lock(char* debugName) /initialize lock to be FREE name = debugName; LockedThread = new Thread(LockedThread); LockValue = 0; threadnum = 0; semaphore = new Semaphore(locksem,0); Lock:Lock() /析構(gòu)函數(shù) delete LockedThread; delete semaphore; void Lock:Acquire() /wait until the lock is FREE, then set
22、 it to BUSY ASSERT(!isHeldByCurrentThread() ); /check the lock if FREE while ( LockValue ) semaphore-P(); LockedThread = currentThread; LockValue = 1; void Lock:Release() /set lock to be FREE, waking up a thread waiting ASSERT( isHeldByCurrentThread(); /check the lock if BUSY LockedThread = NULL; Lo
23、ckValue = 0; semaphore-V(); bool Lock:isHeldByCurrentThread()if(LockedThread=currentThread)&LockValue)return true;elsereturn false; Condition:Condition(char* debugName) /initialize condition to no one waiting name = debugName; semaphore = new Semaphore(Conditionsem,0); Condition:Condition() delete s
24、emaphore; void Condition:Wait(Lock* conditionLock) /release the lock, relinquish the CPU until signaled,then re-acquire the lock ASSERT( conditionLock-isHeldByCurrentThread(); /check the lock if held conditionLock-Release(); semaphore-P(); threadnum+; conditionLock-Acquire(); void Condition:Signal(L
25、ock *conditionLock) /wake up a thread,if there are any waiting on the condition ASSERT( conditionLock-isHeldByCurrentThread(); if ( threadnum 0 ) semaphore-V(); threadnum-; void Condition:Broadcast(Lock *conditionLock) /wake up all threads waiting on the condition ASSERT( conditionLock-isHeldByCurre
26、ntThread(); while ( threadnum 0 ) semaphore-V(); threadnum-; 1.4用鎖機制和條件變量修改雙向有序鏈表修改了dllist.h , dllist.cc,加入了鎖和條件鎖,為了方便測試,在threadtest.cc里加入一個變量flag,flag=0時不使用鎖,flag=1時使用鎖。修改部分主要為dllist.cc的Remove和SortedInsert函數(shù)void *DLList:Remove(int *keyPtr) / remove from head of listDLLElement *element;if(flag)lock-
27、Acquire();if (IsEmpty()if(flag) lock-Release();return NULL;void *retitem;element=first;*keyPtr=first-key;if (err_type=1)currentThread-Yield();retitem=element-item;if (first=last)first=NULL;last=NULL;elseif (err_type=1)currentThread-Yield();first=element-next;first-prev=NULL;delete element;if(flag)lo
28、ck-Release();return retitem;void DLList:SortedInsert(void *item, int sortKey) / routines to put/get items on/off list in order (sorted by key)DLLElement *insertItem=new DLLElement(item,sortKey);DLLElement *ptr=first;if(flag)lock-Acquire();if (IsEmpty()first=insertItem;if (err_type=2)currentThread-Yi
29、eld();last=insertItem;elsefor (;ptr!=NULL; ptr=ptr-next)if (ptr-keysortKey) break;if (err_type=3)currentThread-Yield();if (ptr=NULL)insertItem-prev=last;last-next=insertItem;last=insertItem;last-next=NULL;elseif (ptr=first)insertItem-next=first; first-prev=insertItem;first=insertItem;first-prev=NULL
30、;elseptr-prev-next=insertItem;insertItem-prev=ptr-prev;if (err_type=4)currentThread-Yield();insertItem-next=ptr;ptr-prev=insertItem;if(flag)lock-Release();2.實現(xiàn)一個線程安全的表結(jié)構(gòu)相關(guān)代碼:Table.h#include synch-sem.hclass Table public: / create a table to hold at most size entries. Table(int size); Table(); / allo
31、cate a table slot for object. / return the table index for the slot or -1 on error. int Alloc(int *object); / return the object from table index index or NULL on error. / (assert index is in range). Leave the table entry allocated / and the pointer in place. void *Get(int index); / free a table slot
32、 void Release(int index,int t); private: / Your code here. Lock *tablelock; int *table; int *value; int tablesize;/the size of the table;Table.cc#include Table.h #include thread.h#include system.hextern int flag;Table:Table(int size) / create a table to hold at most size entries. tablesize = size; t
33、ablelock = new Lock(Tablelock); table =new int*size;value =new intsize;for(int i=0;iAcquire(); for ( i=0; iYield(); tablei=object; valuei=1;if(flag) tablelock-Release();return i; if(flag) tablelock-Release(); return -1; void *Table:Get(int index) if ( index0 & indexAcquire(); if ( index=0 & indexRel
34、ease(); 3.實現(xiàn)一個大小受限的緩沖區(qū)相關(guān)代碼:BoudedBuffer.hclass BoundedBuffer public:BoundedBuffer();BoundedBuffer(); / create a bounded buffer with a limit of maxsize bytes void Read(char *data, int size,int t); / write size bytes from data into the bounded buffer. / (size may be greater than maxsize) void Write(ch
35、ar *data, int size,int t); private:int buffersize;Lock *bufferlock;Condition *bufferEmpty;Condition *bufferFull;char *buffer;int readout;int writein;int num;BoundedBuffer.cc#include copyright.h#include synch-sleep.h#include BoundedBuffer.h#include system.hextern int flag;BoundedBuffer:BoundedBuffer(
36、) bufferlock=new Lock(bufferlock); bufferEmpty=new Condition(readlist); bufferFull=new Condition(writelist);buffersize=10;buffer=new charbuffersize;readout=0;writein=0;num=0;BoundedBuffer:BoundedBuffer() delete bufferlock; delete bufferEmpty; delete bufferFull;delete buffer;void BoundedBuffer:Read(c
37、har *data,int size,int t)if(flag)bufferlock-Acquire();for(int i=0;iWait(bufferlock);i=0;elseprintf(the buffer is empty,cant read!n);return;elsedatai=bufferreadout;printf(n read buffer%d by thread %d,readout,t);num-;readout=(readout+1)%buffersize;if(flag)bufferFull-Broadcast(bufferlock);if(flag)buffe
38、rlock-Release();void BoundedBuffer:Write(char *data,int size,int t)if(flag)bufferlock-Acquire();for(int i=0;iWait(bufferlock);i=0;elseprintf(nbuffer is full cant write!);return;elsebufferwritein=datai;printf(n write buffer%d by thread %d,writein,t);writein=(writein+1)%buffersize;num+;if(flag)bufferE
39、mpty-Broadcast(bufferlock);if(flag)bufferlock-Release();4.修改threadtest.cc#include copyright.h#include system.h#include math.h#include thread.h#include stdio.h#include dllist.h#include Table.h#include BoundedBuffer.hint testnum = 1,threadnum=1,n,err_type=0,flag=0;DLList *dllist; Table *table;BoundedB
40、uffer *buffer;extern void Insert(int ,int ,DLList*);extern void Remove(int ,int ,DLList*);voidSimpleThread(int which) int num; for (num = 0; num Yield(); voidDLListThread(int t)Insert(t,n,dllist);Remove(t,n,dllist);void BufferThread(int t)char str10;char *p=str;char *q=str;if(t%2)=0)buffer-Write(p,10,t);elsebuffer-Read(q,10,t);printf(n);void TableThread(int t)int *p510; int i
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 文化產(chǎn)業(yè)示范基地復核書
- 河北省邢臺市威縣寺莊中學2024-2025學年八年級上學期期中地理試題(含答案)
- 實驗室用拭子實驗室工具產(chǎn)業(yè)鏈招商引資的調(diào)研報告
- 吉他弦橋市場需求與消費特點分析
- 單肩包市場發(fā)展預測和趨勢分析
- 人教版英語八年級下冊 Unit 1-3 單元閱讀訓練
- 高效灌溉技術(shù)在蔬菜種植中的應(yīng)用分析報告
- 可充氣薄橡膠玩具市場需求與消費特點分析
- 土耳其氈帽產(chǎn)業(yè)規(guī)劃專項研究報告
- 城市公共設(shè)施門窗改造方案
- 作文啟蒙篇:第1課優(yōu)秀課件
- 結(jié)構(gòu)力學求解器使用教程
- 幼兒園中班語言活動《猜猜我有多愛你》課件
- 卵圓孔未閉和腦卒中課件
- 小學數(shù)學西南師大三年級上冊四兩位數(shù)除以一位數(shù)的除法解決問題(進一法)
- 公司燃氣鍋爐技術(shù)規(guī)范書
- 文化館建筑設(shè)計任務(wù)書
- 《肺功能檢測》課件
- 鋼渣熱悶工藝規(guī)程及人員崗位職責
- (中職)數(shù)控編程與操作教程全冊電子教案
- 初中 初一 語文 寫作《學會記事》(第一課時) 微課課件
評論
0/150
提交評論