📄 threadtest.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 "EventBarrier.h"//#include "synchlist.h"// testnum is set in main.cc#include "Elevator.h"int testnum=1;extern int T,N;//T(number of threads),N(number of people);Building *build;//=new Building("build",10,3);EventBarrier *event=new EventBarrier("eventbarrier");extern Alarm * alarm_clock;#define debug 1//EventBarrier *event = new EventBarrier("EVENT");//SynchList *slist=new SynchList();//----------------------------------------------------------------------// SimpleThread// Loop 5 times, yielding the CPU to another ready thread // each iteration.//// "which" is simply a number identifying the thread, for debugging// purposes.//----------------------------------------------------------------------/*voidSimpleThread(int which){ // int num; void *item; item=currentThread; for (num = 0; num < 5; num++) { //printf("*** thread %d looped %d times\n", which, num); printf("people %d wait\n",which); currentThread->Yield(); } if(which==1){ } else{// elev->Runing(); } currentThread->Yield();}*///////////////////////////////////////////////////////////////EventBarrier Test///////////////////////////////////////////////////////////voidWaiter(int which){ printf("people %d wait door.\n",which); event->Wait(); //printf("people %d saw door open.\n",which);// event->Complete(); printf("people %d enter the door.\n",which);}voidSignaler(int which){ printf("door open.\n"); event->Signal(); currentThread->Yield(); printf("door close.\n");}//----------------------------------------------------------------------// ThreadTest1// Set up a ping-pong between two threads, by forking a thread // to call SimpleThread, and then calling SimpleThread ourselves.//----------------------------------------------------------------------voidThreadTest1(){ DEBUG('t', "Entering ThreadTest1"); int i; T=5; for(i=1;i<=T;i++){ Thread *t = new Thread("forked thread"); if(i==T) t->Fork(Signaler,T); else t->Fork(Waiter, i); } // Signaler(1);}////////////////////////////////////////////////////////////////////////Alarm Test//////////////////////////////////////////////////////////////////////void alarmtest(int which){ int old; old =stats->totalTicks; printf("Current Time:%d Ticks,thread %d go to sleep.\n",old,which); alarm_clock->Pause(2); printf("thread %d wakeup after sleep %d Ticks.\n",which,(stats->totalTicks-old)); printf("Curretn Time:%d Ticks.\n",stats->totalTicks);} voidThreadTest2(){ DEBUG('t',"Entering ThreadTest2"); int i; for(i=1;i<=T;i++){ Thread *t=new Thread("forked thread"); t->Fork(alarmtest,i); }}//////////////////////////////////////////////////////////////////////////Elevator Test///////////////////////////////////////////////////////////////////////void rider(int id,int src,int des){ Elevator *e;// if(id==3)// currentThread->Yield(); if(src==des) return; do{ if(src<des){ printf("rider %d callup at floor %d,want goto %d\n",id,src,des); build->CallUp(src); printf("rider %d called.\n",id); e=build->AwaitUp(src); } else{ printf("rider %d calldown at floor %d,want goto %d\n",id,src,des); build->CallDown(src); printf("rider %d called.\n",id); e=build->AwaitDown(src); } }while(!e->Enter()); printf("rider %d enter elevator %d.\n",id,e->GetID()); printf("elevator %d has %d peoples.\n",e->GetID(),e->GetCurrentOccu()); printf("rider %d now requestfloor %d.\n",id,des); e->RequestFloor(des); //printf("elevator %d arrive floor %d.\n",e->GetID(),des); //currentThread->Yield(); e->Exit(); printf("rider %d leave elevator %d.\n",id,e->GetID());}voidelevatorer(int which){ Elevator *e; int dstFloor; e=build->GetElevator(which); printf("elevatorer %d runing.\n",which); dstFloor=e->NextFloor();//get next visit floor,(block?) while(1){ //dstFloor=e->NextFloor();//get next visit floor,(block?) if((e->GetCurrentFloor())==dstFloor){ printf("elevator %d open door at floor %d.\n",which,dstFloor); e->OpenDoors(); currentThread->Yield(); e->CloseDoors(); printf("elevator %d close door at floor %d.\n",which,dstFloor); dstFloor=e->NextFloor();//get next visit floor,(block?) } else{ printf("elevatorer %d: %d->%d\n",which,e->GetCurrentFloor(),dstFloor); e->VisitFloor(dstFloor); //printf("elevatorer %d visitfloor dstFloor %d.\n",which,dstFloor); } currentThread->Yield(); }}void rider_interface(int which){ int from,to;// int time; from=Random()%9+1; to=Random()%9+1; //time=Random()%10+1; //alarm_clock->Pause(time); rider(which,from,to); return;}voidThreadTest3(){ DEBUG('t',"Entering ThreadTest3"); build=new Building("build",10,T); int i; int people=N; int elenum=build->GetElevatorNum(); for(i=1;i<=people;i++){ Thread *t=new Thread("forked rider"); t->Fork(rider_interface,i); } printf("fork %d riders\n",people); for(i=1;i<=elenum;i++){ Thread *t=new Thread("forked elevatorer"); t->Fork(elevatorer,i); } printf("fork %d elevatorers\n",elenum);}//----------------------------------------------------------------------// ThreadTest// Invoke a test routine.//----------------------------------------------------------------------voidThreadTest(){ switch (testnum) { case 1: printf("EventBarrier Test....\n"); ThreadTest1(); break; case 2: printf("Alarm Clock Test....\n"); ThreadTest2(); break; case 3: printf("Elevator Test...\n"); ThreadTest3(); break; default: printf("No test specified.\n"); break; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -