⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 threadtest.cc

📁 nachos系统作业 实现线程系统 实现一个电梯模拟 附实验报告
💻 CC
字号:
// threadtest.cc //	Simple test case for the threads assignment.////	Create two threads, and have them context switch//	back and forth between themselves by calling Thread::Yield, //	to illustratethe inner workings of the thread system.//// Copyright (c) 1992-1993 The Regents of the University of California.// All rights reserved.  See copyright.h for copyright notice and limitation // of liability and disclaimer of warranty provisions.#include "copyright.h"#include "system.h"#include "dllist.h"#include "BoundedBuffer.h"#include "Table.h"// testnum is set in main.ccint testnum = 1;int elementnum=0;int i=0;DLList *q=new DLList;//----------------------------------------------------------------------// SimpleThread// 	Loop 5 times, yielding the CPU to another ready thread //	each iteration.////	"which" is simply a number identifying the thread, for debugging//	purposes.//----------------------------------------------------------------------/*//test 1//----------------------------------------------------------------------voidSimpleThread(int which){   //int num;   // for (num = 0; num < 5; num++) {	//printf("*** thread %d looped %d times\n", which, num);}      if((which%2)==0){   Creat(elementnum,q);   q->ReadFront();   q->ReadBack();        }   //q->ReadBack();   //currentThread->Yield();    else{   Destory(elementnum,q);   q->ReadFront();   q->ReadBack();        }   //q->ReadBack();        }//---------------------------------------------------------------------*///----------------------------------------------------------------------/*//test 2//------------------------------------------------------------------------BoundedBuffer *buf=new BoundedBuffer(100);char msg[20]="msg from producer";char msg2[20];voidproducer()
{    void *data=(void *)msg;     buf->Write(data,20);}void consumer() 
{    void *data=(void *)msg2;     buf->Read(data,10);
     printf("get the msg <%s>\n",msg2);}voidSimpleThread(int which){         DEBUG('t',"%d enter SimpleThread() \n",which);    if((which%2)!=0)producer();               else consumer();}//----------------------------------------------------------------------*///----------------------------------------------------------------------//test 3//----------------------------------------------------------------------Table *tab = new Table(10);char msg[20]="msg from producer";int dex=0;voidproducer()
{    void *data=(void *)msg;     dex=tab->Alloc(data);     printf("insert the index %d\n",dex);}void consumer() 
{     void *data;     data=tab->Get(dex);
     printf("get the msg <%s>\n",data);}void clearner() 
{     tab->Release(dex);     //delete msg;
     printf("Release the index %d\n",dex);}voidSimpleThread(int which){          DEBUG('t',"%d enter SimpleThread() \n",which);     if((which%2)==0)producer();               else {                     consumer();                     clearner();                                                      }}//-----------------------------------------------------------------------//----------------------------------------------------------------------// ThreadTest1// 	Set up a ping-pong between two threads, by forking a thread //	to call SimpleThread, and then calling SimpleThread ourselves.//----------------------------------------------------------------------voidThreadTest1(int id){    DEBUG('t', "Entering ThreadTest1");        Thread *t = new Thread("forked thread");    t->Fork(SimpleThread,id);    //SimpleThread(0);}//----------------------------------------------------------------------// ThreadTest// 	Invoke a test routine.//----------------------------------------------------------------------voidThreadTest(){    //switch (testnum) {    //case 1:	//ThreadTest1();	//break;    //default:	//printf("No test specified.\n");	//break;    //}if(testnum>=1)while(i!=testnum){ThreadTest1(i);i++;}  else printf("No test specified.\n");}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -