Guest User

Untitled

a guest
Jul 17th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.03 KB | None | 0 0
  1. ClientInfo::ClientInfo(int x, int y, int z)
  2. {
  3.   serverG=x;
  4.   serverR=y;
  5.   tickTime=z;
  6. }
  7. client is in my thread.cc and thread.h
  8.  
  9. this next is in threadtest.cc
  10. / threadtest.cc
  11. //Simple test case for the threads assignment.
  12. //
  13. //Create two threads, and have them context switch
  14. //back and forth between themselves by calling Thread::Yield,
  15. //to illustratethe inner workings of the thread system.
  16. //
  17. // Copyright (c) 1992-1993 The Regents of the University of California.
  18. // All rights reserved.  See copyright.h for copyright notice and limitation
  19. // of liability and disclaimer of warranty provisions.
  20.  
  21. #include "copyright.h"
  22. #include "system.h"
  23. #include <time.h>
  24. #include <stdlib.h>
  25. #include "list.h"
  26. extern AlarmClock *alarm;
  27.  
  28.  
  29. //---------------------------------------------------------------------
  30. // SimpleThread
  31. // Loop infinitelys, yielding the CPU to another ready thread
  32. //each iteration.
  33. //
  34. //"which" is simply a number identifying the thread, for debugging
  35. //purposes.
  36. //      Calls upon the AlarmClock Class created in Thread.h, Thread.cc
  37. //----------------------------------------------------------------------
  38.  
  39.  
  40.  
  41. class ServerNetwork
  42. {
  43. public:
  44.   int regular[10];
  45.   int graphic[10];
  46.   ServerNetwork();
  47.   ~ServerNetwork();
  48.   void setGServer();
  49.   void setRServer();
  50.   void clrRServer();
  51.   void clrGServer();
  52.   void checkGateWays(int, int, int);
  53.   void AllocateServers(int , int, int);
  54.   Semaphore *graphicS;
  55.   Semaphore *gateWayS;
  56.   Semaphore *mutex2;
  57.   Semaphore *mutex1;
  58.   Semaphore *regularS;
  59.   char* graphicCount;
  60.   char* Gateways;
  61.   char* enterExit;
  62.   char* enterExit2;
  63.   char*regularCount;
  64.   ClientInfo *c1;
  65.   ClientInfo *c2;
  66. };
  67. ServerNetwork::ServerNetwork()
  68. {
  69.   for(int i=0; i<10; i++)
  70.     {
  71.       graphic[i]=0;
  72.       regular[i]=0;
  73.     }
  74.  
  75.   graphicS= new Semaphore(graphicCount, 10);
  76.   mutex1 = new Semaphore(enterExit, 1);
  77.   mutex2= new Semaphore(enterExit2, 1);
  78.   regularS= new Semaphore(regularCount, 10);
  79.   gateWayS = new Semaphore(Gateways, 2); //come back to this one !!!
  80.   // Semaphore(Gatways, 1);
  81.   c1=NULL;
  82.   c2=NULL;
  83. }
  84. ServerNetwork::~ServerNetwork()
  85. {
  86.   delete c1;
  87.   delete c2;
  88.   delete mutex2;
  89.   delete mutex1;
  90.   delete regularS;
  91.   delete graphicS;
  92.   delete Gateways;
  93.  
  94.  
  95.  
  96. }
  97.  
  98. void ServerNetwork::checkGateWays(int x, int y, int z)
  99. {
  100.  
  101.   gateWayS->P();
  102.   AllocateServers( x, y ,z );
  103.   gateWayS->V();
  104. }
  105.  
  106. void ServerNetwork::AllocateServers(int x, int y, int z)
  107. {
  108.   c1= new ClientInfo(x, y , z);
  109.   mutex1->P();
  110.   for(int i=0; i<c1->serverG; i++)
  111.     {
  112.       graphicS->P();
  113.       setGServer();
  114.      alarm->Pause(z);
  115.     }
  116.   //alarmClock->Pause();
  117.   mutex1->V();
  118.   mutex2->P();
  119.   for(int k=0; k<c1->serverG; k++)
  120.     {
  121.       clrGServer();
  122.       graphicS->V();
  123.  
  124.     }
  125.   //  alarmClock->DestroyWake();
  126.   mutex2->V();
  127.  
  128.  
  129. }
  130. void ServerNetwork::setGServer()
  131. {
  132.   int i=0;
  133.   while(graphic[i]==1)
  134.     {
  135.       i++;
  136.     }
  137.   graphic[i]=1;
  138.  
  139. }
  140. void ServerNetwork::setRServer()
  141. {
  142.   int i=0;
  143.   while(regular[i]==1)
  144.     {
  145.       i++;
  146.     }
  147.   regular[i]=1;
  148. }
  149. void ServerNetwork::clrGServer()
  150. {
  151.   int i=0;
  152.   while(graphic[i]==0)
  153.     {
  154.       i++;
  155.     }
  156.   graphic[i]=0;
  157. }
  158.  
  159.  
  160.  
  161. void ServerNetwork::clrRServer()
  162. {
  163.   int i=0;
  164.   while(regular[i]==0)
  165.     {
  166.       i++;
  167.     }
  168.   regular[i]=0;
  169. }
  170.  
  171. ServerNetwork * serve;
  172. void NullThread(int which) //used with Null Thread
  173. {
  174.   while(1) {
  175.     interrupt->OneTick();
  176.  
  177.   currentThread->Yield();
  178.   }
  179. }
  180.  
  181.  
  182. void
  183. SimpleThread(int which)
  184. {
  185.   while(1){ // set to loop infinitely
  186.  
  187.     int regularRand =(rand()%11); // finds the # of regular servers between 0 and 10\
  188. .
  189.     int graphicRand =(rand()%11); // finds the # of graphic servers between 0 and 10\
  190. .
  191.     int randomTicks =(rand()%1001)+100; // finds the howLong value between 100 and 1\
  192. 000.
  193.     printf("Regular Rand: %d, GraphicRand: %d, RandomTicks: %d\n", regularRand, grap\
  194. hicRand, randomTicks);
  195. // printf("How Long: %d\n", randomTicks);
  196.     //  printf("\n");
  197.  
  198.    serve->checkGateWays(regularRand, graphicRand,randomTicks);
  199.  
  200.    // printf("\nSelf Destruct Sleep Queue\n");
  201.     //alarm->SelfDestruct(randomTicks); //add the new thread into the Self Destruct \
  202.  Sleeping Queue
  203.  
  204.   currentThread->Yield(); //if thread done
  205.   }
  206. }
  207. //----------------------------------------------------------------------
  208. // ThreadTest
  209. // Set up a ping-pong between two threads, by forking a thread
  210. //to call SimpleThread, and then calling SimpleThread ourselves.
  211. //----------------------------------------------------------------------
  212.  
  213.  
  214. void
  215. ThreadTest()
  216. {
  217.  
  218.   serve = new ServerNetwork();
  219.   // printf("-----Queues at Startup-------\n");
  220.   srand(time(0));// call for finding random integers for How long and Percent
  221.  
  222.   // 2 null threads to keep nachos alive
  223.   Thread *null1 = new Thread("NullThread 1");
  224.   null1->Fork(NullThread, 1);
  225.   Thread *null2= new Thread("NullThread 2");
  226.   null2->Fork(NullThread, 1);
  227.   // 20 thread in an infinite loop to simpleThread
  228.   Thread *client1 = new Thread("Client 1");
  229.   client1->Fork(SimpleThread, 1);
  230.   SimpleThread(0);
  231.  
  232. }
Add Comment
Please, Sign In to add comment