SHARE
TWEET

Untitled

a guest Sep 17th, 2019 95 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // threadtest.cc
  2. //  Simple test case for the threads assignment.
  3. //
  4. //  Create two threads, and have them context switch
  5. //  back and forth between themselves by calling Thread::Yield,
  6. //  to illustratethe inner workings of the thread system.
  7. //
  8. // Copyright (c) 1992-1993 The Regents of the University of California.
  9. // All rights reserved.  See copyright.h for copyright notice and limitation
  10. // of liability and disclaimer of warranty provisions.
  11.  
  12. #include "copyright.h"
  13. #include "system.h"
  14.  
  15. //----------------------------------------------------------------------
  16. // Inc and Dec execute value++ and value-- for twice.
  17. //
  18. //  "which" is simply a number identifying the thread, for debugging
  19. //  purposes.
  20. //----------------------------------------------------------------------
  21.  
  22. int value=0;
  23. //case 0
  24. void Inc(_int which)
  25. {
  26.     int a=value;
  27.     a++;
  28.     value=a;
  29.     printf("**** Inc thread %d new value %d\n", (int) which, value);
  30. }
  31.  
  32. void Dec(_int which)
  33. {
  34.     int a=value;
  35.     a--;
  36.     value=a;
  37.     printf("**** Dec thread %d new value %d\n", (int) which, value);
  38. }
  39.  
  40. //exercise 1: two Inc threads and two Dec threads, and implement the interleaving
  41. //so that value=targetV when all the four threads ends.
  42.  
  43.  
  44. //targetV=1;
  45. //After executing TestValueOne(), the value should be one.
  46. //1. implement the new version of Inc: Inc_v1
  47. void Inc_v1(_int which)
  48. {
  49.     //fill your code
  50.     int a=value;
  51.     a++;
  52.     printf("**** Inc thread %d a value %d\n", (int) which, a);
  53.     currentThread->Yield();
  54.     value=a;
  55.     printf("**** Inc thread %d new value %d\n", (int) which, value);
  56. }
  57.  
  58. //2. implement the new version of Dec: Dec_v1
  59. void Dec_v1(_int which)
  60. {
  61.     //fill your code
  62.     int a=value;
  63.     a--;
  64.     printf("**** Dec thread %d a value %d\n", (int) which, a);
  65.     value=a;
  66.     printf("**** Dec thread %d new value %d\n", (int) which, value);
  67. }
  68.  
  69. //3. implement TestValueOne by create two threads with Inc_v1 and two threads with Dec_v1
  70. // you should pass the checking at the end, printing "congratulations! passed."
  71. void TestValueOne()
  72. {
  73.     value=0;
  74.     printf("enter TestValueOne, value=%d...\n", value);
  75.     //1. fill your code here.
  76.  
  77.     Thread *Inc1 = new Thread("Inc1");
  78.     Thread *Inc2 = new Thread("Inc2");
  79.     Thread *Dec1 = new Thread("Dec1");
  80.     Thread *Dec2 = new Thread("Dec2");
  81.     Inc1->Fork(Inc_v1, 2, 0);
  82.     Dec1->Fork(Dec_v1, 1, 0);
  83.     Dec2->Fork(Dec_v1, 3, 0);
  84.     Inc_v1(0); 
  85.  
  86.     //2. checking the value. you should not modify the code or add any code lines behind
  87.     //this section.
  88.     if(value==1)
  89.         printf("congratulations! passed.\n");
  90.     else
  91.         printf("value=%d, failed.\n", value);
  92. }
  93.  
  94.  
  95. //targetV=-2;
  96. //After executing TestValueMinusTwo(), the value should be -2.
  97. //1. implement the new version of Inc: Inc_v2
  98. void Inc_v2(_int which)
  99. {
  100.     //fill your code
  101.     int a=value;
  102.     a++;
  103.     printf("**** Inc thread %d a value %d\n", (int) which, a); 
  104.     value=a;
  105.     printf("**** Inc thread %d new value %d\n", (int) which, value);
  106. }
  107.  
  108. //2. implement the new version of Dec: Dec_v2
  109. void Dec_v2(_int which)
  110. {
  111.     //fill your code
  112.     int a=value;
  113.     a--;
  114.     printf("**** Dec thread %d a value %d\n", (int) which, a);
  115.     currentThread->Yield();
  116.     value=a;
  117.     printf("**** Dec thread %d new value %d\n", (int) which, value);
  118. }
  119.  
  120. //3. implement TestValueMinusOne by create two threads with Inc_v2 and two threads with Dec_v2
  121. // you should pass the checking at the end, printing "congratulations! passed."
  122. void TestValueMinusOne()
  123. {
  124.     value=0;
  125.     printf("enter TestValueMinusOne, value=%d...\n", value);
  126.  
  127.     //fill your code
  128.     Thread *Inc1 = new Thread("Inc1");
  129.     Thread *Inc2 = new Thread("Inc2");
  130.     Thread *Dec1 = new Thread("Dec1");
  131.     Thread *Dec2 = new Thread("Dec2");
  132.     Dec1->Fork(Dec_v2, 1, 0);  
  133.     Inc1->Fork(Inc_v2, 2, 0);
  134.     Inc2->Fork(Inc_v2, 3, 0);
  135.     Dec_v2(0);
  136.  
  137.     //2. checking the value. you should not modify the code or add any code lines behind
  138.     //this section.
  139.     if(value==-1)
  140.         printf("congratulations! passed.\n");
  141.     else
  142.         printf("value=%d, failed.\n", value);
  143. }
  144.  
  145.  
  146. //Exercise 2: offer an implementation of Inc and Dec so that
  147. //no matter what kind of interleaving occurs, the result value should be consistent.
  148.  
  149. //1. Declare any paramters here.
  150. Lock *conLock = new Lock("conLock");
  151. //fill your code
  152.  
  153. //2. implement the new version of Inc: Inc_Consistent
  154. void Inc_Consistent(_int which)
  155. {
  156.     //fill your code
  157.     conLock->Acquire();
  158.     int a=value;
  159.     a++;
  160.     //currentThread->Yield();
  161.     value=a;
  162.     printf("**** Inc thread %d new value %d\n", (int) which, value);
  163.     conLock->Release();
  164.     currentThread->Yield();
  165. }
  166.  
  167. //3. implement the new version of Dec: Dec_Consistent
  168. void Dec_Consistent(_int which)
  169. {
  170.     //fill your code
  171.     conLock->Acquire();
  172.     int a=value;
  173.     a--;
  174.     //currentThread->Yield();
  175.     value=a;
  176.     printf("**** Dec thread %d new value %d\n", (int) which, value);
  177.     conLock->Release();
  178.     currentThread->Yield();
  179. }
  180.  
  181. //4. implement TestValueMinusOne by create two threads with Inc_Consistent and two threads with Dec_Consistent
  182. // you should pass the checking at the end, printing "congratulations! passed."
  183. void TestConsistency()
  184. {
  185.     value=0;
  186.     printf("enter TestConsistency, value=%d...\n", value);
  187.  
  188.     //fill your code
  189.     Thread *Inc1 = new Thread("Inc1");
  190.     Thread *Inc2 = new Thread("Inc2");
  191.     Thread *Dec1 = new Thread("Dec1");
  192.     Thread *Dec2 = new Thread("Dec2");
  193.     Dec1->Fork(Dec_Consistent, 2, 0);  
  194.     Inc1->Fork(Inc_Consistent, 3, 0);  
  195.     Inc2->Fork(Dec_Consistent, 0, 0);
  196.     Inc_Consistent(1);
  197.  
  198.     conLock->Acquire();
  199.     //2. checking the value. you should not modify the code or add any code lines behind
  200.     //this section.
  201.     if(value==0)
  202.         printf("congratulations! passed.\n");
  203.     else
  204.         printf("value=%d, failed.\n", value);
  205.    
  206.     conLock->Release();
  207. }
  208.  
  209. //select the function that you want to test.
  210. void
  211. ThreadTest()
  212. {
  213.     int loopTimes=0;
  214.     DEBUG('t', "Entering SimpleTest");
  215.     //for exercise 1.
  216.     //TestValueOne();
  217.     //TestValueMinusOne();
  218.     //for exercise 2.
  219.     TestConsistency();
  220. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top