Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. Question 1: Launch this program several times. What do you notice? Explain it!
  2. The result can be other than 3000 sometimes, that’s because when the thread is
  3. not synchronize , multiple workers can access to the variable and make the increase amount
  4. different each the program is executed
  5.  
  6. Question 2: Change the code of the general executable program by replacing
  7. ThreadedWorkerWithoutSync with ThreadedWorkerWithSync to initiate three
  8. instances worker1-3. What is the difference between the output of this program
  9. and that of question 1? Explain it!
  10. The result is always 3000 as the thread is synchronize, so at one particular time
  11. there is only one worker access to variable
  12.  
  13. Question 3: Change the code of the general executable program by replace the
  14. instances worker1-3. What is the difference between the output of this program
  15. and the output in question 1? Explain it!
  16. The result is always 3000, because with ThreadedWorkerWithSync class,instead of being given access to variable every time the exploit() method was called, a worker can be given access only when the variable is free(meaning it is not accessed by any worker at the moment),the variable will be then locked to the worker ,and when the operation has been done(increase by 1),it will be unlocked.
  17.  
  18. Question 4: Complete this file above (in the part YOUR-CODE-HERE) with a loop
  19. to increase the variable shared by 1 for 5 seconds.
  20. (hint: time(NULL) will return the present system time in second).
  21. while(time(NULL)<=end)
  22. {
  23. shared++;
  24. sleep(1);
  25. }
  26.  
  27. Question 5: Try to increase the value of threads and the value of the constant
  28. NUM_TRANS after each execution time until you obtain the different results
  29. between Balance and INIT_BALANCE+credits-debits. Explain why do you get this
  30. difference.
  31. For every time the program is executed, all the threads run simultaneously while also having access to the same variables at the same time.
  32. Even for a simple addition command, for example balance = balance + v; there’s still 2 steps for each thread to do: calculate balance + v and store it to a temp variable, then assign the value of temp back to balance
  33. Because there’s no resource locking, the value of balance can even be changed between the time the 2 steps is done in a thread, since another thread could finish its job earlier
  34. This resulted in all the variables have different values each time the program is done executing
  35.  
  36. Question 6: Try to build and run this program. Launch it repeatedly until you see
  37. the difference between Shared and Expect values. Analyze the source code to
  38. understand the problem that leads to this difference.
  39. Consider the code block for locking
  40. for (i = 0; i < 100; i++)
  41. {
  42. //check lock
  43. while (lock > 0); //spin until unlocked
  44. lock = 1; //set lock
  45. shared++; //increment
  46. lock = 0; //unlock
  47. }
  48. Because all threads are spinning in the while loop waiting for the lock to be open at the same time, there’s a chance that a thread swap can happen after the loop is stopped, leading to 2 or more threads can have the lock.
  49.  
  50. Question 7: Now, you have to modify the code of the file without-lock.c in
  51. implementing the mutex lock above (you can name it differently like mutex-lockbanking.c). Try to launch it repeatedly and evaluate the obtained output. What is the improvement after using mutex lock?
  52. The total balance now matches the expected value based on the credit and debit
  53. Credits: 586
  54. Debits: 13020
  55.  
  56. 50+586-13020= -12384
  57. Balance: -12384
  58.  
  59. Question 8: compare the run times of the two strategies to prove that Fine
  60. Locking is faster and much faster on larger load sets
  61.  
  62.  
  63. Question 9: Run this program and what do you get as output? Explain what the
  64. deadlock is.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement