[2] | 1 | \documentclass[a4paper,12pt]{article}
|
---|
| 2 | \usepackage{hyperref}
|
---|
| 3 | \usepackage{a4wide}
|
---|
| 4 | %\usepackage{indentfirst}
|
---|
| 5 | \usepackage[english]{babel}
|
---|
| 6 | \usepackage{graphics}
|
---|
| 7 | %\usepackage[pdftex]{graphicx}
|
---|
| 8 | \usepackage{latexsym}
|
---|
| 9 | \usepackage{fancyhdr}
|
---|
| 10 | \usepackage{fancyvrb}
|
---|
| 11 |
|
---|
| 12 | \parindent=0cm
|
---|
| 13 |
|
---|
| 14 | \pagestyle{fancyplain}
|
---|
| 15 | \newcommand{\tstamp}{\today}
|
---|
| 16 | \newcommand{\id}{$ $Id: report.tex 195 2007-05-30 01:04:25Z rick $ $}
|
---|
| 17 | \lfoot[\fancyplain{\tstamp}{\tstamp}] {\fancyplain{\tstamp}{\tstamp}}
|
---|
| 18 | \cfoot[\fancyplain{\id}{\id}] {\fancyplain{\id}{\id}}
|
---|
| 19 | \rfoot[\fancyplain{\thepage}{\thepage}] {\fancyplain{\thepage}{\thepage}}
|
---|
| 20 |
|
---|
| 21 |
|
---|
| 22 | \title{ Concepts of programming languages \\
|
---|
| 23 | \large{Assignment 1 - Java}}
|
---|
| 24 | \author{Rick van der Zwet\\
|
---|
| 25 | \texttt{<hvdzwet@liacs.nl>}\\
|
---|
| 26 | \\
|
---|
| 27 | LIACS\\
|
---|
| 28 | Leiden Universiteit\\
|
---|
| 29 | Niels Bohrweg 1\\
|
---|
| 30 | 2333 CA Leiden\\
|
---|
| 31 | The Netherlands}
|
---|
| 32 | \date{\today}
|
---|
| 33 | \begin{document}
|
---|
| 34 | \maketitle
|
---|
| 35 | \section{Introduction}
|
---|
| 36 | Analyse on Java program Write two Java programs to get some experience
|
---|
| 37 | implementing concurrent programs in Java.
|
---|
| 38 |
|
---|
| 39 | \section{Analyse Eyes.java}
|
---|
| 40 | Compiling and execution of the program is pretty staight forward
|
---|
| 41 | \begin{verbatim}
|
---|
| 42 | $ javac Eyes.class Eyes.java
|
---|
| 43 | \end{verbatim}
|
---|
| 44 | A detailed explanation of the program code is found in the Appendix -
|
---|
| 45 | Ogen.java \ref{file:Ogen.java}
|
---|
| 46 |
|
---|
| 47 | \section{The Readers/Writer Problem}
|
---|
| 48 | \subsection{Problem}
|
---|
| 49 | \label{rwproblem}
|
---|
| 50 | Two kind processes -readers and writers- share a database. Writers
|
---|
| 51 | execute transactions than update the database; reader transactions
|
---|
| 52 | access the database without modifying it. The database is assumed
|
---|
| 53 | initially to be in a consistend state (i.e one in which relations
|
---|
| 54 | between data are meaningfull). Each transaction, if executed in
|
---|
| 55 | isolation, transforms the database from one consistent state to another.
|
---|
| 56 | To preclude interference between transactions, a writer process must
|
---|
| 57 | have exclusive access to the database. Assuming no writer is accessing
|
---|
| 58 | the database, any number of readers my concurrently execute
|
---|
| 59 | transactions.
|
---|
| 60 | \\
|
---|
| 61 | To tackle this problem we first have to indentify the main states and
|
---|
| 62 | summerize the conditions:
|
---|
| 63 | \begin{enumerate}
|
---|
| 64 | \item No processes is reading/writing the database \\
|
---|
| 65 | Both new read/write processes are allowed to start right away
|
---|
| 66 | \item X processes are reading the database \\
|
---|
| 67 | A new read process is allowed to start right away, a write process needs to
|
---|
| 68 | wait
|
---|
| 69 | \item 1 process is writing the database \\
|
---|
| 70 | Both new read/write processes need to wait
|
---|
| 71 | \end{enumerate}
|
---|
| 72 |
|
---|
| 73 | \subsection{Implementation}
|
---|
| 74 | \label{rw-implentation}
|
---|
| 75 | To implement this problem we keep track how many processes are reading
|
---|
| 76 | the database by using an counter (called Readers), which is incremented
|
---|
| 77 | when an reader starts reading and decremented when done. Writing to the
|
---|
| 78 | datebase will be locked by a flag (called WriteLocked) stating we are
|
---|
| 79 | busy and released when done.
|
---|
| 80 | \\\\
|
---|
| 81 | When a reader process tries to access the database, it will check
|
---|
| 82 | whether the WriteLocked is set free and then increment the Writers, and
|
---|
| 83 | decrement the Writers when done else it will wait
|
---|
| 84 | \\\\
|
---|
| 85 | When a writer process tries to access the database, it will check wether
|
---|
| 86 | the WriteLocked is set free, else it will wait. If the WriteLocked is
|
---|
| 87 | set free and there are no Readers (e.g Readers = 0), it will lock it
|
---|
| 88 | WriteLocked and release when done.
|
---|
| 89 | \\\\
|
---|
| 90 | The java implemetation is found at first/Database.java~\ref{file:Database.java}
|
---|
| 91 |
|
---|
| 92 | \subsection{Test Suite}
|
---|
| 93 | \label{rw-test-suite}
|
---|
| 94 | In order to test this implemtation a test program is created. In short
|
---|
| 95 | it will create X number of readers and writers using threads to make
|
---|
| 96 | them recurrent and have them all access the database
|
---|
| 97 | \\\\
|
---|
| 98 | The java implemetation is found at RWProblem.java~\ref{file:RWProblem.java}
|
---|
| 99 |
|
---|
| 100 |
|
---|
| 101 | \section{The Readers/Writer Problem enhanced}
|
---|
| 102 | \subsection{Problem}
|
---|
| 103 | Implement the Readers/Writers problem (described at~\ref{rwproblem})
|
---|
| 104 | giving priority to the writers: i.e. if a writer wants to enter the
|
---|
| 105 | database but has to wait then no reader may enter, and of course the
|
---|
| 106 | writer still has to wait for current readers in the database to be
|
---|
| 107 | finished
|
---|
| 108 | \\\\
|
---|
| 109 | The main states defined in~\ref{rwproblem} still exists, but the
|
---|
| 110 | state 2 is slightly changed
|
---|
| 111 | \begin{enumerate}
|
---|
| 112 | \item No processes is reading/writing the database \\
|
---|
| 113 | Both new read/write processes are allowed to start right away
|
---|
| 114 | \item X processes are reading the database \\
|
---|
| 115 | A new read process is allowed to start if no writer process is waiting,
|
---|
| 116 | a write process needs to wait
|
---|
| 117 | \item 1 process is writing the database \\
|
---|
| 118 | Both new read/write processes need to wait
|
---|
| 119 | \end{enumerate}
|
---|
| 120 |
|
---|
| 121 | \subsection{Implementation}
|
---|
| 122 | As the actions are slightly changed, the implementation needs to be
|
---|
| 123 | changed a slight little bit as well from the implementation at~
|
---|
| 124 | \ref{rw-implentation}. The flag WriterWaiting will indentify wether a
|
---|
| 125 | Writer is waiting. A Reader which tries to access the database will now
|
---|
| 126 | check wheter both WriteLocked and WriterWaiting are false before reading
|
---|
| 127 | is done.
|
---|
| 128 | A Writer which try to access the database and find the database locked
|
---|
| 129 | by WriteLocked will now set WriterWaiting and start waiting, when a
|
---|
| 130 | Writer find WriteLocked to be false it will start running (setting
|
---|
| 131 | WriteLocked to be true) and set WriterWaiting to be false.
|
---|
| 132 | \\\\
|
---|
| 133 | Using this implementation there is only one caveat -which only occurs
|
---|
| 134 | when the stack grow that big that not all processes database attemps
|
---|
| 135 | are checked during the time of one write- . Take a look at the following
|
---|
| 136 | stack order: W(rite), R(ead), R, R, R, W, R, R. and assume the current state
|
---|
| 137 | is 1 (no-one reading/writing). Process 1 will set the WriteWaiting flag
|
---|
| 138 | to be false, if the write of Process completed, before the next Write is
|
---|
| 139 | checked (and the flag WriteWaiting is set again). The read processes are
|
---|
| 140 | allowed to start even there is a Write process waiting in the queue
|
---|
| 141 | \\\\
|
---|
| 142 | The java implemetation is found at second/Database.java~
|
---|
| 143 | \ref{file:secondDatabase.java}
|
---|
| 144 |
|
---|
| 145 | \subsection{Test Suite}
|
---|
| 146 | Used the same test tool as defined in~\ref{rw-test-suite}
|
---|
| 147 |
|
---|
| 148 | \section{Appendix}
|
---|
| 149 |
|
---|
| 150 | \subsection{Ogen.java}
|
---|
| 151 | \label{file:Ogen.java}
|
---|
| 152 | \VerbatimInput{../eyes/Ogen.java}
|
---|
| 153 | %Newpage
|
---|
| 154 | \subsection{first/Database.java}
|
---|
| 155 | \label{file:Database.java}
|
---|
| 156 | \VerbatimInput{../first/Database.java}
|
---|
| 157 | %Newpage
|
---|
| 158 | \subsection{second/Database.java}
|
---|
| 159 | \label{file:secondDatabase.java}
|
---|
| 160 | \VerbatimInput{../second/Database.java}
|
---|
| 161 | %Newpage
|
---|
| 162 | \subsection{RWProblem.java}
|
---|
| 163 | \label{file:RWProblem.java}
|
---|
| 164 | \VerbatimInput{../first/RWProblem.java}
|
---|
| 165 | %Newpage
|
---|
| 166 |
|
---|
| 167 | \end{document}
|
---|