Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ClientInfo::ClientInfo(int x, int y, int z)
- {
- serverG=x;
- serverR=y;
- tickTime=z;
- }
- client is in my thread.cc and thread.h
- this next is in 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 <time.h>
- #include <stdlib.h>
- #include "list.h"
- extern AlarmClock *alarm;
- //---------------------------------------------------------------------
- // SimpleThread
- // Loop infinitelys, yielding the CPU to another ready thread
- //each iteration.
- //
- //"which" is simply a number identifying the thread, for debugging
- //purposes.
- // Calls upon the AlarmClock Class created in Thread.h, Thread.cc
- //----------------------------------------------------------------------
- class ServerNetwork
- {
- public:
- int regular[10];
- int graphic[10];
- ServerNetwork();
- ~ServerNetwork();
- void setGServer();
- void setRServer();
- void clrRServer();
- void clrGServer();
- void checkGateWays(int, int, int);
- void AllocateServers(int , int, int);
- Semaphore *graphicS;
- Semaphore *gateWayS;
- Semaphore *mutex2;
- Semaphore *mutex1;
- Semaphore *regularS;
- char* graphicCount;
- char* Gateways;
- char* enterExit;
- char* enterExit2;
- char*regularCount;
- ClientInfo *c1;
- ClientInfo *c2;
- };
- ServerNetwork::ServerNetwork()
- {
- for(int i=0; i<10; i++)
- {
- graphic[i]=0;
- regular[i]=0;
- }
- graphicS= new Semaphore(graphicCount, 10);
- mutex1 = new Semaphore(enterExit, 1);
- mutex2= new Semaphore(enterExit2, 1);
- regularS= new Semaphore(regularCount, 10);
- gateWayS = new Semaphore(Gateways, 2); //come back to this one !!!
- // Semaphore(Gatways, 1);
- c1=NULL;
- c2=NULL;
- }
- ServerNetwork::~ServerNetwork()
- {
- delete c1;
- delete c2;
- delete mutex2;
- delete mutex1;
- delete regularS;
- delete graphicS;
- delete Gateways;
- }
- void ServerNetwork::checkGateWays(int x, int y, int z)
- {
- gateWayS->P();
- AllocateServers( x, y ,z );
- gateWayS->V();
- }
- void ServerNetwork::AllocateServers(int x, int y, int z)
- {
- c1= new ClientInfo(x, y , z);
- mutex1->P();
- for(int i=0; i<c1->serverG; i++)
- {
- graphicS->P();
- setGServer();
- alarm->Pause(z);
- }
- //alarmClock->Pause();
- mutex1->V();
- mutex2->P();
- for(int k=0; k<c1->serverG; k++)
- {
- clrGServer();
- graphicS->V();
- }
- // alarmClock->DestroyWake();
- mutex2->V();
- }
- void ServerNetwork::setGServer()
- {
- int i=0;
- while(graphic[i]==1)
- {
- i++;
- }
- graphic[i]=1;
- }
- void ServerNetwork::setRServer()
- {
- int i=0;
- while(regular[i]==1)
- {
- i++;
- }
- regular[i]=1;
- }
- void ServerNetwork::clrGServer()
- {
- int i=0;
- while(graphic[i]==0)
- {
- i++;
- }
- graphic[i]=0;
- }
- void ServerNetwork::clrRServer()
- {
- int i=0;
- while(regular[i]==0)
- {
- i++;
- }
- regular[i]=0;
- }
- ServerNetwork * serve;
- void NullThread(int which) //used with Null Thread
- {
- while(1) {
- interrupt->OneTick();
- currentThread->Yield();
- }
- }
- void
- SimpleThread(int which)
- {
- while(1){ // set to loop infinitely
- int regularRand =(rand()%11); // finds the # of regular servers between 0 and 10\
- .
- int graphicRand =(rand()%11); // finds the # of graphic servers between 0 and 10\
- .
- int randomTicks =(rand()%1001)+100; // finds the howLong value between 100 and 1\
- 000.
- printf("Regular Rand: %d, GraphicRand: %d, RandomTicks: %d\n", regularRand, grap\
- hicRand, randomTicks);
- // printf("How Long: %d\n", randomTicks);
- // printf("\n");
- serve->checkGateWays(regularRand, graphicRand,randomTicks);
- // printf("\nSelf Destruct Sleep Queue\n");
- //alarm->SelfDestruct(randomTicks); //add the new thread into the Self Destruct \
- Sleeping Queue
- currentThread->Yield(); //if thread done
- }
- }
- //----------------------------------------------------------------------
- // ThreadTest
- // Set up a ping-pong between two threads, by forking a thread
- //to call SimpleThread, and then calling SimpleThread ourselves.
- //----------------------------------------------------------------------
- void
- ThreadTest()
- {
- serve = new ServerNetwork();
- // printf("-----Queues at Startup-------\n");
- srand(time(0));// call for finding random integers for How long and Percent
- // 2 null threads to keep nachos alive
- Thread *null1 = new Thread("NullThread 1");
- null1->Fork(NullThread, 1);
- Thread *null2= new Thread("NullThread 2");
- null2->Fork(NullThread, 1);
- // 20 thread in an infinite loop to simpleThread
- Thread *client1 = new Thread("Client 1");
- client1->Fork(SimpleThread, 1);
- SimpleThread(0);
- }
Add Comment
Please, Sign In to add comment