Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.89 KB | None | 0 0
  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. int a=value;
  50. a++;
  51. currentThread->Yield();
  52. value=a;
  53. printf("**** Inc thread %d new value %d\n", (int) which, value);
  54. }
  55.  
  56. //2. implement the new version of Dec: Dec_v1
  57. void Dec_v1(_int which)
  58. {
  59. int a=value;
  60. a--;
  61. value=a;
  62. printf("**** Dec thread %d new value %d\n", (int) which, value);
  63. }
  64.  
  65. //3. implement TestValueOne by create two threads with Inc_v1 and two threads with Dec_v1
  66. // you should pass the checking at the end, printing "congratulations! passed."
  67. void TestValueOne()
  68. {
  69. value=0;
  70. printf("enter TestValueOne, value=%d...\n", value);
  71.  
  72. Thread *t1 = new Thread("child1"); //Create new thread t1
  73. t1->Fork(Inc_v1, 1, 1);
  74. Thread *t2 = new Thread("child2"); //Create new thread t2
  75. t2->Fork(Inc_v1, 2, 0);
  76. Thread *t3 = new Thread("child3"); //Create new thread t3
  77. t3->Fork(Dec_v1, 3, 0);
  78. Thread *t4 = new Thread("child4"); //Create new thread t4
  79. t4->Fork(Dec_v1, 4, 0);
  80.  
  81. //currentThread->Join(t1);
  82. currentThread->Join(t2);
  83. //currentThread->Join(t3);
  84. //currentThread->Join(t4);
  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. int a=value;
  101. a++;
  102. value=a;
  103. printf("**** Inc thread %d new value %d\n", (int) which, value);
  104. }
  105.  
  106. //2. implement the new version of Dec: Dec_v2
  107. void Dec_v2(_int which)
  108. {
  109. int a=value;
  110. a--;
  111. currentThread->Yield();
  112. value=a;
  113. printf("**** Dec thread %d new value %d\n", (int) which, value);
  114. }
  115.  
  116. //3. implement TestValueMinusOne by create two threads with Inc_v2 and two threads with Dec_v2
  117. // you should pass the checking at the end, printing "congratulations! passed."
  118. void TestValueMinusOne()
  119. {
  120. value=0;
  121. printf("enter TestValueMinusOne, value=%d...\n", value);
  122.  
  123. Thread *t1 = new Thread("child1");
  124. t1->Fork(Dec_v2, 1, 1);
  125. Thread *t2 = new Thread("child2");
  126. t2->Fork(Dec_v2, 2, 1);
  127. Thread *t3 = new Thread("child3");
  128. t3->Fork(Inc_v2, 3, 1);
  129. Thread *t4 = new Thread("child4");
  130. t4->Fork(Inc_v2, 4, 1);
  131.  
  132. currentThread->Join(t1);
  133. currentThread->Join(t2);
  134. currentThread->Join(t3);
  135. currentThread->Join(t4);
  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 parameters here.
  150.  
  151. Lock *lock = new Lock("Inc/Dec lock"); //Create new lock
  152. Semaphore *semaphore = new Semaphore("Inc/Dec semaphore", 1);
  153. //lock->Acquire();
  154. //lock->Release();
  155.  
  156.  
  157. //2. implement the new version of Inc: Inc_Consistent
  158. void Inc_Consistent(_int which)
  159. {
  160. semaphore->P();
  161. int a=value;
  162. currentThread->Yield();
  163. a++;
  164. value=a;
  165. semaphore->V();
  166. printf("**** Inc thread %d new value %d\n", (int) which, value);
  167. }
  168.  
  169. //3. implement the new version of Dec: Dec_Consistent
  170. void Dec_Consistent(_int which)
  171. {
  172. semaphore->P();
  173. int a=value;
  174. a--;
  175. currentThread->Yield();
  176. value=a;
  177. semaphore->V();
  178. printf("**** Inc thread %d new value %d\n", (int) which, value);
  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. Thread *t4 = new Thread("child4");
  189. t4->Fork(Inc_Consistent, 4, 1);
  190. Thread *t1 = new Thread("child1");
  191. t1->Fork(Dec_Consistent, 1, 1);
  192. Thread *t2 = new Thread("child2");
  193. t2->Fork(Dec_Consistent, 2, 1);
  194. Thread *t3 = new Thread("child3");
  195. t3->Fork(Inc_Consistent, 3, 1);
  196.  
  197. currentThread->Join(t1);
  198. currentThread->Join(t2);
  199. currentThread->Join(t3);
  200. currentThread->Join(t4);
  201.  
  202. //2. checking the value. you should not modify the code or add any code lines behind
  203. //this section.
  204. if(value==0)
  205. printf("congratulations! passed.\n");
  206. else
  207. printf("value=%d, failed.\n", value);
  208. }
  209.  
  210. //select the function that you want to test.
  211. void
  212. ThreadTest()
  213. {
  214. int loopTimes=0;
  215. DEBUG('t', "Entering SimpleTest");
  216. //for exercise 1.
  217. TestValueOne();
  218. //TestValueMinusOne();
  219. //for exercise 2.
  220. //TestConsistency();
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement