您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 综合/其它 > 操作系统实验-nachos01
实验一体验Nachos下的并发程序设计一、实验人员:二、实验目的:对nachos进行熟悉,并初步体验nachos下的并发程序设计三、实验内容:1、安装Nachos2、用C++实现双向有序链表;3、在nachos系统中使用所写的链表程序并演示一些并发错误四、实验步骤:1、安装Nachos,具体细则如下下载code-linux.tar.gz并上传到服务器建立目录(推荐建立主目录下的nachos)cd到新建目录中tarzxvfcode-linux.tar.gz的完整路径cdnachos-3.4/codemake2、阅读材料阅读nachos-3.4/code/Makefilenachos-3.4/code/Makefile.depnachos-3.4/code/Makefile.commonnachos-3.4/code/threads/Makefile初步了解各Makefile的构成和相互关系。阅读nachos-3.4/code/threads/main.cc了解nachos如何开始。阅读nachos-3.4/code/threads/system.cc的Initialize函数中与debug相关的部分及nachos-3.4/code/threads/utility.cc了解DEBUG的实现与使用,以此进一步熟悉nachos系统。阅读nachos-3.4/code/threads/threadtest.cc,了解nachos中线程的概念及其运作方式。3、编写相关的dllist.h,dllist.cc,dllist-driver.cc文件,具体代码如下dllist.hclassDLLElement{public:DLLElement(void*itemPtr,intsortKey);//initializealistelementDLLElement*next;//nextelementonlist//NULLifthisisthelastDLLElement*prev;//previouselementonlist//NULLifthisisthefirstintkey;//priority,forasortedlistvoid*item;//pointertoitemonthelist};classDLList{public:DLList();//initializethelistDLList(inttype);~DLList();//de-allocatethelistvoidPrepend(void*item);//addtoheadoflist(setkey=min_key-1)voidAppend(void*item);//addtotailoflist(setkey=max_key+1)void*Remove(int*keyPtr);//removefromheadoflist//set*keyPtrtokeyoftheremoveditem//returnitem(orNULLiflistisempty)boolIsEmpty();//returntrueiflisthaselements//routinestoput/getitemson/offlistinorder(sortedbykey)voidSortedInsert(void*item,intsortKey);void*SortedRemove(intsortKey);//removefirstitemwithkey==sortKey//returnNULLifnosuchitemexistsprivate:DLLElement*first;//headofthelist,NULLifemptyDLLElement*last;//lastelementofthelist,NULLifemptyinterr_type;};Dllist.cc#includecopyright.h#includedllist.h#includesystem.hDLLElement::DLLElement(void*itemPtr,intsortKey)//initializealistelement{item=itemPtr;key=sortKey;next=NULL;prev=NULL;}DLList::DLList()//initializethelist{first=NULL;last=NULL;err_type=0;}DLList::DLList(inttype){first=NULL;last=NULL;err_type=type;}DLList::~DLList()//de-allocatethelist{while(Remove(NULL)!=NULL);}voidDLList::Prepend(void*item)//addtoheadoflist(setkey=min_key-1){DLLElement*elm=newDLLElement(item,0);if(IsEmpty()){first=elm;last=elm;}else{elm-key=first-key-1;elm-next=first;elm-prev=NULL;first-prev=elm;first=elm;}}voidDLList::Append(void*item)//addtotailoflist(setkey=max_key+1){DLLElement*elm=newDLLElement(item,0);if(IsEmpty()){first=elm;last=elm;}else{elm-key=last-key+1;elm-next=NULL;elm-prev=last;last-next=elm;last=elm;}}void*DLList::Remove(int*keyPtr)//removefromheadoflist{DLLElement*element;if(IsEmpty()){returnNULL;}void*retitem;element=first;*keyPtr=first-key;if(err_type==1){printf(Removeerror\n);currentThread-Yield();}retitem=element-item;if(first==last){first=NULL;last=NULL;}else{if(err_type==1){printf(Removeerror\n);currentThread-Yield();}first=element-next;first-prev=NULL;}deleteelement;returnretitem;}boolDLList::IsEmpty()//returntrueiflisthaselements{if((first==NULL)&&(last==NULL))returntrue;elseif((first!=NULL)&&(last!=NULL))returnfalse;elseprintf(error!eitherthefirstorthelastisNULL!\n);returnfalse;}voidDLList::SortedInsert(void*item,intsortKey)//routinestoput/getitemson/offlistinorder(sortedbykey){DLLElement*insertItem=newDLLElement(item,sortKey);DLLElement*ptr=first;if(IsEmpty()){first=insertItem;if(err_type==2){printf(SortedInserterror,first!=last\n);currentThread-Yield();}last=insertItem;}else{for(;ptr!=NULL;ptr=ptr-next)if(ptr-keysortKey)break;if(err_type==3){printf(SortedInserterror,thepostionlost\n);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;}else{ptr-prev-next=insertItem;insertItem-prev=ptr-prev;if(err_type==4){printf(SorteadInserterror,sorterror\n);currentThread-Yield();}insertItem-next=ptr;ptr-prev=insertItem;}}}void*DLList::SortedRemove(intsortKey)//removefirstitemwithkey==sortKey{//returnNULLifnosuchitemexistsDLLElement*ptr=first;if(IsEmpty())returnNULL;for(;ptr!=NULL;ptr=ptr-next)if(ptr-keysortKey)break;if(ptr==NULL){printf(Removeerror!Nosuchakey!);returnNULL;}elseif(ptr==first){first=first-next;first-prev=NULL;}elseif(ptr==last){last=last-prev;last-next=NULL;}else{ptr-prev-next=ptr-next;ptr-next-prev=ptr-prev;}returnptr-item;}Dllist-driver.cc#includestdlib.h#includecopyright.h#includedllist.h#includesystem.h#includetime.hvoidInsert(intt,intn,DLList*dllist){inti,ll;srand(time(0));for(i=0;in;i++){ll=rand()%101;dllist-SortedInsert(NULL,ll);printf(Thread%d:insertedkey=%d\n,t,ll);}}voidRemove(intt,intn,DLList*dllist){inti,keyll;for(i=0;in;i++){dllist-Remove(&keyll);printf(Thread%d:removedkey=%d\n,t,keyll);}}4、将上述要的链表文件拷贝nachos-3.4/code/threads/中,修改nachos-3.4/code/Makefile.common中的THREAD_H、THREAD_C、THREAD_O,在nachos-3.4/code/threads/目录中依次执行makedepend和make修改nachos-3.4/code/threads/threadtest.cc和nachos-3.4/code/threads/main.cc实现两个线程调用链表功能,重新编译threads子系统修改nachos-3.4/code/threads/threadtest.cc,在适当位置插入currentThread-Y
本文标题:操作系统实验-nachos01
链接地址:https://www.777doc.com/doc-4531879 .html