Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.21 KB | None | 0 0
  1. \documentclass[runningheads,a4paper]{llncs}
  2. \usepackage{graphicx}
  3. \usepackage{amsmath}
  4. \usepackage{algorithm}
  5. \usepackage{algorithmic}
  6. \usepackage{algpseudocode}
  7.  
  8. \begin{document}
  9.  
  10. \begin{minipage}{\textwidth}
  11. \title\textbf{Synchronisation Mechanisms Lab Assignment Technical Report}
  12. \newline
  13. \newline
  14. \newline
  15. Student: Theodora-Ioana Danciulescu, CEN 3.1
  16. \end{minipage}
  17.  
  18. \section{Problem statement}
  19. The statement of this problem is to implement the producer-consumer concurrency problem. The idea of the problem is that there exists a type of producer thread and a type of consumer thread. The two types of threads share the same buffer, as one of the threads has to store elements int the buffer and the others to remove elements from the buffer.
  20. \newline One important aspect to take into consideration is that the size of the buffer is finite, so if the buffer is full no elements can be added, and if the buffer is empty, no elements can be extracted.
  21. \newline The elements that are stored in the buffer are random English alphabet letters. Every producer and consumer threads are assigned a number of letters to produce and to consume, respectively.
  22. \newline The quantity assigned to each thread is computed in the following way: if there are \textit{P} producers, the size of the buffer is \textit{N}. Let $N = pq + r$, where \textit{q} and \textit{r} represents the result by dividing \textit{N} to \textit{P} and respectively the remainder of the division of \textit{N} to \textit{P}.
  23. \newline In the same way is happening in the consumer threads case.
  24. \newline Note: $P < N$, $C < N$.
  25.  
  26. \section{Implementation}
  27.  
  28. \subsection{Semaphores Method}
  29.  
  30. \indent The memory usage of the program in big lines consists of 2 arrays where the producer and consumer threads are stored, the queue data structure used as a buffer for the thread and various variables for sharing data between classes. The time execution for one set of 10 data inputs is
  31.  
  32. \begin{figure}[H]
  33. \includegraphics[width=\linewidth]{exec_time.png}
  34. \label{fig:execution time}
  35. \end{figure}
  36.  
  37. The classes used:
  38.  
  39. \begin{itemize}
  40. \item \textbf{Main.java} - Initially computes the number of letters that each thread should produce/consume according to the formulas in the problem statement. Here is the \textit{main function} that creates the necessary objects for generating input file, created the correspondent threads and starts them, then all the threads are being waited to be finished in order to collect data report from each thread and analyze the global result.
  41. \item \textbf{InputGenerator.java} - This class represents the input generator for our problem.
  42. \newline
  43. \begin{itemize}
  44. \item \textbf{GenerateBufferSize} - Method that is returning random integer in interval [1, 1000] that represents the size of the buffer
  45. \item \textbf{GenerateProducersConsumersNumber} - Method that is returning a pair of random integers in interval [1, 1000] that represent the number of producers consumers respectively. The number of producers and consumers is known from the problem statement that is lower than the size of the buffer.
  46. \item \textbf{GenerateInputFile} - Method that returns a .txt file containing 10 rows with 3 integers on every line representing the size of the buffer, the number of producers and the number of consumers.
  47. \end{itemize}
  48. \item \textbf{ProducerThread} - This class represents the producer behaviour. This class extends the \textit{Thread} class. The \textit{run} method is generating the assigned number of letters to produce and calls the \textit{produce} method.
  49. \item \textbf{ConsumerThread} - This class represents the consumer behaviour. This class extends the \textit{Thread} class. The \textit{run} method calls the \textit{consume} method for the number of times the respective thread has be assigned to consume.
  50. \item \textbf{MyBuffer} - This class represents the queue that stores the items/objects for the two threads that produce and/or consume.
  51. \begin{itemize}
  52. \item A queue data structure represents the shared memory space between the threads. When a letter is produced it is added to the top of the queue and when a letter is consumed if removed from the end of the queue.
  53. \item Semaphore for producers with N number of permits
  54. \item Semaphore for consumers, initially 0 because we need to wait for at least 1 element to be produced.
  55. \item \textbf{produce} - this method is a synchronized method as it is essentially to allow access to the shared memory space to only one process at a time. This method adds a letter to the buffer. It releases the consumer semaphore because after each added item a consumer can use it.
  56. \begin{algorithm}[H]
  57. \caption{produce}
  58. \algblock[TryCatchFinally]{try}{endtry}
  59. \algcblock[TryCatchFinally]{TryCatchFinally}{finally}{endtry}
  60. \algcblockdefx[TryCatchFinally]{TryCatchFinally}{catch}{endtry}
  61. [1]{\textbf{catch} #1}
  62. {\textbf{end try}}
  63. \begin{algorithmic}[1]
  64. \try
  65. \State{acquire semaphoreProducer}
  66. \IF{ buffer not full }
  67. \State{add to buffer}
  68. \State{print \textit{letter was added}}
  69. \ENDIF
  70. \catch
  71. exception
  72. \finally
  73. \State{release semaphoreConsumer}
  74. \endtry
  75. \end{algorithmic}
  76. \end{algorithm}
  77. \end{itemize}
  78. \end{itemize}
  79.  
  80. \subsection{Monitors Method}
  81.  
  82. \indent The memory usage of the program in big lines consists of 2 arrays where the producer and consumer threads are stored, the queue data structure used as a buffer for the thread and various variables for sharing data between classes. The time execution for one set of data input is
  83.  
  84. \begin{figure}[H]
  85. \includegraphics[width=\linewidth]{exec_time2.png}
  86. \label{fig:execution time}
  87. \end{figure}
  88.  
  89. The classes used:
  90.  
  91. \begin{itemize}
  92. \item \textbf{Main.java} - Initially computes the number of letters that each thread should produce/consume according to the formulas in the problem statement. Here is the \textit{main function} that creates the necessary objects for generating input file, created the correspondent threads and starts them, then all the threads are being waited to be finished in order to collect data report from each thread and analyze the global result.
  93. \item \textbf{InputGenerator.java} - This class represents the input generator for our problem.
  94. \newline
  95. \begin{itemize}
  96. \item \textbf{GenerateBufferSize} - Method that is returning random integer in interval [1, 1000] that represents the size of the buffer
  97. \item \textbf{GenerateProducersConsumersNumber} - Method that is returning a pair of random integers in interval [1, 1000] that represent the number of producers consumers respectively. The number of producers and consumers is known from the problem statement that is lower than the size of the buffer.
  98. \item \textbf{GenerateInputFile} - Method that returns a .txt file containing 10 rows with 3 integers on every line representing the size of the buffer, the number of producers and the number of consumers.
  99. \end{itemize}
  100. \item \textbf{ProducerThread} - This class represents the producer behaviour. This class extends the \textit{Thread} class. The \textit{run} method is generating the assigned number of letters to produce.
  101. \begin{itemize}
  102. \item This String represents the alphabet from which we choose the random letters to produce.
  103. \item Reference to the queue that represents the buffer
  104. \item \textbf{run} - The method that generates a random letter from the alphabet string and then adds it to the buffer. It contains a synchronized block as the queue is a shared memory resource between more processes.
  105.  
  106. \begin{algorithm}[H]
  107. \caption{run}
  108. \algblock[TryCatchFinally]{try}{endtry}
  109. \algcblock[TryCatchFinally]{TryCatchFinally}{finally}{endtry}
  110. \algcblockdefx[TryCatchFinally]{TryCatchFinally}{catch}{endtry}
  111. [1]{\textbf{catch} #1}
  112. {\textbf{end try}}
  113. \begin{algorithmic}[1]
  114. \State{synchronized(buffer)}
  115. \WHILE{running}
  116. \State{$letter = random letter $}
  117. \WHILE { buffer is full }
  118. \try
  119. \State{wait}
  120. \catch
  121. exception
  122. \endtry
  123. \ENDWHILE
  124. \State{print letter was added}
  125. \State{add letter to buffer}
  126. \IF{produced the assigned number of letters}
  127. \State{$running = false$}
  128. \ELSE
  129. \State{NotifyAll)
  130. \ENDIF
  131. \ENDWHILE
  132. \end{algorithmic}
  133. \end{algorithm}
  134.  
  135. \end{itemize}
  136. \item \textbf{ConsumerThread} - This class represents the consumer behaviour. This class extends the \textit{Thread} class. The \textit{run} method calls the \textit{consume} method for the number of times the respective thread has be assigned to consume.
  137. \begin{itemize}
  138. \item Reference to the queue that represents the buffer
  139. \item \textbf{run} - The method that generates a random letter from the alphabet string and then adds it to the buffer. It contains a synchronized block as the queue is a shared memory resource between more processes.
  140.  
  141. \begin{algorithm}[H]
  142. \caption{run}
  143. \algblock[TryCatchFinally]{try}{endtry}
  144. \algcblock[TryCatchFinally]{TryCatchFinally}{finally}{endtry}
  145. \algcblockdefx[TryCatchFinally]{TryCatchFinally}{catch}{endtry}
  146. [1]{\textbf{catch} #1}
  147. {\textbf{end try}}
  148. \begin{algorithmic}[1]
  149. \State{synchronized(buffer)}
  150. \WHILE{running}
  151. \State{$letter = random letter $}
  152. \WHILE { buffer is empty }
  153. \try
  154. \State{wait}
  155. \catch
  156. exception
  157. \endtry
  158. \ENDWHILE
  159. \State{print letter was removed}
  160. \State{remove letter from buffer}
  161. \IF{consumed the assigned number of letters}
  162. \State{$running = false$}
  163. \ELSE
  164. \State{NotifyAll)
  165. \ENDIF
  166. \ENDWHILE
  167. \end{algorithmic}
  168. \end{algorithm}
  169.  
  170. \end{itemize}
  171.  
  172. \end{itemize}
  173.  
  174. \subsection{Locks Method}
  175.  
  176. \indent The memory usage of the program in big lines consists of 2 arrays where the producer and consumer threads are stored, the queue data structure used as a buffer for the thread and various variables for sharing data between classes. The time execution for one set of 10 data inputs is
  177.  
  178. \begin{figure}[H]
  179. \includegraphics[width=\linewidth]{exec_time3.png}
  180. \label{fig:execution time}
  181. \end{figure}
  182.  
  183. The classes used:
  184.  
  185. \begin{itemize}
  186. \item \textbf{Main.java} - Initially computes the number of letters that each thread should produce/consume according to the formulas in the problem statement. Here is the \textit{main function} that creates the necessary objects for generating input file, created the correspondent threads and starts them, then all the threads are being waited to be finished in order to collect data report from each thread and analyze the global result.
  187. \item \textbf{InputGenerator.java} - This class represents the input generator for our problem.
  188. \newline
  189. \begin{itemize}
  190. \item \textbf{GenerateBufferSize} - Method that is returning random integer in interval [1, 1000] that represents the size of the buffer
  191. \item \textbf{GenerateProducersConsumersNumber} - Method that is returning a pair of random integers in interval [1, 1000] that represent the number of producers consumers respectively. The number of producers and consumers is known from the problem statement that is lower than the size of the buffer.
  192. \item \textbf{GenerateInputFile} - Method that returns a .txt file containing 10 rows with 3 integers on every line representing the size of the buffer, the number of producers and the number of consumers.
  193. \end{itemize}
  194. \item \textbf{ProducerThread} - This class represents the producer behaviour. This class extends the \textit{Thread} class. The \textit{run} method is generating the assigned number of letters to produce and calls the \textit{produce} method.
  195. \item \textbf{ConsumerThread} - This class represents the consumer behaviour. This class extends the \textit{Thread} class. The \textit{run} method calls the \textit{consume} method for the number of times the respective thread has be assigned to consume.
  196. \item \textbf{MyBuffer} - This class represents the queue that stores the items/objects for the two threads that produce and/or consume.
  197. \begin{itemize}
  198. \item A queue data structure represents the shared memory space between the threads. When a letter is produced it is added to the top of the queue and when a letter is consumed if removed from the end of the queue.
  199. \item ReentrantLock - This variable represents the lock of the threads, it allows only 1 thread at a time in the critical section.
  200. \item bufferNotEmpty, bufferNotFull - These variables represent the condition signa that allows the consumer to start again being sure that the buffer is not empty and respectively the condition signal that allows the producer to start again being sure that the buffer is not full.
  201. \item \textbf{produce} - This method adds a letter to the buffer,it takes the lock, produce, signal and then unlocks the lock.
  202. \begin{algorithm}[H]
  203. \caption{produce}
  204. \algblock[TryCatchFinally]{try}{endtry}
  205. \algcblock[TryCatchFinally]{TryCatchFinally}{finally}{endtry}
  206. \algcblockdefx[TryCatchFinally]{TryCatchFinally}{catch}{endtry}
  207. [1]{\textbf{catch} #1}
  208. {\textbf{end try}}
  209. \begin{algorithmic}[1]
  210. \State{lock the lock}
  211. \try
  212. \WHILE{ buffer is full }
  213. \State{await 10 milliseconds condition bufferNotFull}
  214. \RETURN
  215. \ENDWHILE
  216. \State{add letter to buffer}
  217. \State{signall all bufferNotEmpty}
  218. \catch
  219. exception
  220. \finally
  221. \State{unlock the lock}
  222. \endtry
  223. \end{algorithmic}
  224. \end{algorithm}
  225.  
  226. \item \textbf{consume} - This method removes the first element from the buffer.It takes the lock, checks if the buffer is empty,awaits for the buffer not to be empty and then consumes the objects unlocking the lock.
  227. \begin{algorithm}[H]
  228. \caption{consume}
  229. \algblock[TryCatchFinally]{try}{endtry}
  230. \algcblock[TryCatchFinally]{TryCatchFinally}{finally}{endtry}
  231. \algcblockdefx[TryCatchFinally]{TryCatchFinally}{catch}{endtry}
  232. [1]{\textbf{catch} #1}
  233. {\textbf{end try}}
  234. \begin{algorithmic}[1]
  235. \State{lock the lock}
  236. \try
  237. \WHILE{ buffer is empty }
  238. \State{await 10 milliseconds condition bufferNotEmpty}
  239. \RETURN
  240. \ENDWHILE
  241. \State{remove letter from buffer}
  242. \State{signall all bufferNotFull}
  243. \catch
  244. exception
  245. \finally
  246. \State{unlock the lock}
  247. \endtry
  248. \end{algorithmic}
  249. \end{algorithm}
  250. \end{itemize}
  251. \end{itemize}
  252.  
  253.  
  254. %
  255. %
  256.  
  257. \end{document}
  258.  
  259. \section{Experiments and results}
  260.  
  261. \subsection{Semaphores Method}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement