{"id":12049,"date":"2021-10-28T13:10:57","date_gmt":"2021-10-28T13:10:57","guid":{"rendered":"https:\/\/assignmenttutors.net\/blog\/mutex-locks-and-semaphores-as-discussed-in-class-are-different\/"},"modified":"2021-10-28T13:10:57","modified_gmt":"2021-10-28T13:10:57","slug":"mutex-locks-and-semaphores-as-discussed-in-class-are-different","status":"publish","type":"post","link":"https:\/\/assignmenttutors.net\/blog\/mutex-locks-and-semaphores-as-discussed-in-class-are-different\/","title":{"rendered":"Mutex locks and semaphores, as discussed in class, are different"},"content":{"rendered":"<div>\n<p>\u00a0<\/p>\n<p>Mutex \u00a0locks and semaphores, as discussed in class, are different techniques to \u00a0solve the race condition and to ensure an efficient synchronization \u00a0between cooperating threads and processes. you will use semaphores to \u00a0solve a number of synchronization problems between cooperating threads.<\/p>\n<p>important to note that:<\/p>\n<p>\u2022 \u00a0Semaphore, in literature, uses wait() and signal(). However, in the \u00a0standard library of Java, these functions are acquire() and release() \u00a0respectively. The same functionalities but with different names.<\/p>\n<p>In \u00a0the first question, the deposit and withdraw functions share a bank \u00a0account to add certain amount or subtract certain amount from the \u00a0balance, respectively.\u00a0<\/p>\n<p>Use semaphore(s) to implement the synchronization.<\/p>\n<p><strong>Implent the following to the code,\u00a0<\/strong><\/p>\n<p><strong>1. Implement the deposit functionality method<\/strong><\/p>\n<p>\u00a0Deposit the input amount to the balance only if the current balance is less than 2000$<\/p>\n<p>\u00a0Deposit \u00a0doesn&#8217;t wait until this condition is true (If the condition is false, \u00a0skip adding the amount), thus use if statements rather than waiting \u00a0while loops<\/p>\n<p>\u00a0Call the displayStatus() function after you deposit the amount and before release the lock<\/p>\n<p><strong>2. Implement the withdraw functionality, method<\/strong><\/p>\n<p>\u00a0Withdraw the input amount from the balance only if the current balance is greater than or equal to input amount<\/p>\n<p>\u00a0Withdraw \u00a0doesn&#8217;t wait until this condition is true (if the condition is false, \u00a0skip withdrawing the amount), thus use if statements rather than waiting \u00a0while \u00a0loops<\/p>\n<p>\u00a0Call the displayStatus() function after you remove the amount and before release the lock<\/p>\n<p>\/\/Question1_WithdrawDeposit.java file<\/p>\n<p><strong>package Threads_Synchronization;<\/strong><\/p>\n<p><strong>import java.util.concurrent.Semaphore;<\/strong><\/p>\n<p><strong>import java.util.concurrent.locks.Lock;<\/strong><\/p>\n<p><strong>import java.util.concurrent.locks.ReentrantLock;<\/strong><\/p>\n<p><strong>public class Question1_WithdrawDeposit {<\/strong><\/p>\n<p><strong>\/*<\/strong><\/p>\n<p><strong>* In this question use semaphore(s) to enable process synchronization<\/strong><\/p>\n<p><strong>*<\/strong><\/p>\n<p><strong>* Thread 1 and thread 2 (in the main function) share a single bank account (initial balance of 1000$).<\/strong><\/p>\n<p><strong>* thread 1 can deposit certain input amount to the balance only if the current balance is less than 2000$<\/strong><\/p>\n<p><strong>* \u00a0thread 2 can withdraw certain input amount from the balance only if the \u00a0current balance is greater than or equal to the input amount.<\/strong><\/p>\n<p><strong>*<\/strong><\/p>\n<p><strong>*\/<\/strong><\/p>\n<p><strong>\/\/ shared resources between thread 1 and thread 2 are:<\/strong><\/p>\n<p><strong>public static int balance = 1000; \/\/the initial value of the account&#8217;s balance<\/strong><\/p>\n<p><strong>\/\/DONOT CHANGE THIS VARIABLE<\/strong><\/p>\n<p><strong>\/\/ add below any further resources you think the deposit and withdraw threads\/functions must share<\/strong><\/p>\n<p><strong>\/\/&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;end of shared resources section<\/strong><\/p>\n<p><strong>\/\/ this function simply displays the current balance of the shared account and which thread made the call<\/strong><\/p>\n<p><strong>\/\/ DONOT CHANGE THIS FUNCTION<\/strong><\/p>\n<p><strong>public static void displayStatus() {<\/strong><\/p>\n<p><strong>if(Thread.currentThread().getName().equals(&#8220;withdraw&#8221;))<\/strong><\/p>\n<p><strong>System.out.println(&#8220;The \u00a0withdraw function successfully took the amount and the current value of \u00a0the account&#8217;s balance is :&#8221;+ balance + &#8220;$&#8221;);<\/strong><\/p>\n<p><strong>else<\/strong><\/p>\n<p><strong>System.out.println(&#8220;The \u00a0deposit function successfully added the amount and the current value of \u00a0the account&#8217;s balance is :&#8221;+ balance + &#8220;$&#8221;);<\/strong><\/p>\n<p><strong>}<\/strong><\/p>\n<p><strong>\/\/ this function accepts an input integer amount value to deposit into the shared account<\/strong><\/p>\n<p><strong>public static void Deposit(int amount){<\/strong><\/p>\n<p><strong>try {<\/strong><\/p>\n<p><strong>System.out.println(&#8220;The deposit function is trying to add &#8220;+ amount +&#8221;$ to the shared balance&#8221;+ balance + &#8220;$&#8221;);<\/strong><\/p>\n<p>\/\/IMPLEMENT HERE the deposit functionality,<\/p>\n<p><strong>\/\/ Deposit the input amount to the balance only if the current balance is less than 2000$<\/strong><\/p>\n<p><strong>\/\/ \u00a0Deposit doesn&#8217;t wait until this condition is true (If the condition is \u00a0false, skip adding the amount), thus use if statements rather than \u00a0waiting while loops<\/strong><\/p>\n<p><strong>\/\/ Call the displayStatus() function after you deposit the amount and before release the lock<\/strong><\/p>\n<p><strong>\/\/ Implement the deposit functionality, as detailed above, in the area below<\/strong><\/p>\n<p><strong>\/\/&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;end of Deposit function<\/strong><\/p>\n<p><strong>} catch (Exception e) {<\/strong><\/p>\n<p><strong>System.out.println(&#8220;Problem with the deposite function &#8220;+e.toString());<\/strong><\/p>\n<p><strong>}<\/strong><\/p>\n<p><strong>}<\/strong><\/p>\n<p><strong>\/\/ this function accepts an input integer amount value to withdraw from the shared account<\/strong><\/p>\n<p><strong>public static void Withdraw(int amount){<\/strong><\/p>\n<p><strong>try {<\/strong><\/p>\n<p><strong>System.out.println(&#8220;The withdraw is trying to remove &#8220;+ amount +&#8221;$ from the shared balance&#8221;+ balance + &#8220;$&#8221;);<\/strong><\/p>\n<p>\/\/IMPLEMENT HERE the withdraw functionality,<\/p>\n<p><strong>\/\/ withdraw the input amount from the balance only if the current balance is greater than or equal to input amount<\/strong><\/p>\n<p><strong>\/\/ \u00a0Withdraw doesn&#8217;t wait until this condition is true (if the condition is \u00a0false, skip withdrawing the amount), thus use if statements rather than \u00a0waiting while loops<\/strong><\/p>\n<p><strong>\/\/ Call the displayStatus() function after you remove the amount and before release the lock<\/strong><\/p>\n<p><strong>\/\/ Implement the withdraw functionality, as detailed above, in the area below<\/strong><\/p>\n<p><strong>\/\/&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;end of Withdraw function<\/strong><\/p>\n<p><strong>} catch (Exception e) {<\/strong><\/p>\n<p><strong>System.out.println(&#8220;Problem with the withdraw function &#8220;+e.toString());<\/strong><\/p>\n<p><strong>}<\/strong><\/p>\n<p><strong>}<\/strong><\/p>\n<p><strong>\/\/ this is the main function<\/strong><\/p>\n<p><strong>\/\/ DONOT CHANGE THIS SECTION<\/strong><\/p>\n<p><strong>public static void main(String[] args) {<\/strong><\/p>\n<p><strong>\/\/create thread 1 to run function 1<\/strong><\/p>\n<p><strong>Thread thread1 = new Thread(new Runnable() {<\/strong><\/p>\n<p><strong>@Override<\/strong><\/p>\n<p><strong>public void run() {<\/strong><\/p>\n<p><strong>while(true) {<\/strong><\/p>\n<p><strong>try {<\/strong><\/p>\n<p><strong>Deposit(200 + (int)(Math.random() * 1000)); \/\/random value between 200 and 1000$<\/strong><\/p>\n<p><strong>Thread.sleep(200 + (int)(Math.random() * 500)); \/\/random delay between 200 and 500<\/strong><\/p>\n<p><strong>} catch (Exception e) {<\/strong><\/p>\n<p><strong>System.out.println(&#8220;Problem with thread 1 &#8220;+e.toString());<\/strong><\/p>\n<p><strong>}<\/strong><\/p>\n<p><strong>}<\/strong><\/p>\n<p><strong>}<\/strong><\/p>\n<p><strong>});<\/strong><\/p>\n<p><strong>\/\/create thread 2 to run function 2<\/strong><\/p>\n<p><strong>Thread thread2 = new Thread(new Runnable() {<\/strong><\/p>\n<p><strong>@Override<\/strong><\/p>\n<p><strong>public void run() {<\/strong><\/p>\n<p><strong>while(true) {<\/strong><\/p>\n<p><strong>try {<\/strong><\/p>\n<p><strong>Withdraw(200 + (int)(Math.random() * 1000)); \/\/random value between 200 and 1000$<\/strong><\/p>\n<p><strong>Thread.sleep(200 + (int)(Math.random() * 500)); \/\/random delay between 200 and 500<\/strong><\/p>\n<p><strong>} catch (Exception e) {<\/strong><\/p>\n<p><strong>System.out.println(&#8220;Problem with thread 2 &#8220;+e.toString());<\/strong><\/p>\n<p><strong>}<\/strong><\/p>\n<p><strong>}<\/strong><\/p>\n<p><strong>}<\/strong><\/p>\n<p><strong>});<\/strong><\/p>\n<p><strong>\/\/ask the threads to start running<\/strong><\/p>\n<p><strong>thread1.setName(&#8220;deposit&#8221;);<\/strong><\/p>\n<p><strong>thread1.start();<\/strong><\/p>\n<p><strong>thread2.setName(&#8220;withdraw&#8221;);<\/strong><\/p>\n<p><strong>thread2.start();<\/strong><\/p>\n<p><strong>}<\/strong><\/p>\n<p><strong>}<\/strong><\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u00a0 Mutex \u00a0locks and semaphores, as discussed in class, are different techniques to \u00a0solve the race condition and to ensure an efficient synchronization \u00a0between cooperating threads and processes. you will use semaphores to \u00a0solve a number of synchronization problems between cooperating threads. important to note that: \u2022 \u00a0Semaphore, in literature, uses wait() and signal(). However, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[31],"tags":[],"class_list":["post-12049","post","type-post","status-publish","format-standard","hentry","category-homework-help"],"_links":{"self":[{"href":"https:\/\/assignmenttutors.net\/blog\/wp-json\/wp\/v2\/posts\/12049","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/assignmenttutors.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/assignmenttutors.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/assignmenttutors.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/assignmenttutors.net\/blog\/wp-json\/wp\/v2\/comments?post=12049"}],"version-history":[{"count":0,"href":"https:\/\/assignmenttutors.net\/blog\/wp-json\/wp\/v2\/posts\/12049\/revisions"}],"wp:attachment":[{"href":"https:\/\/assignmenttutors.net\/blog\/wp-json\/wp\/v2\/media?parent=12049"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/assignmenttutors.net\/blog\/wp-json\/wp\/v2\/categories?post=12049"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/assignmenttutors.net\/blog\/wp-json\/wp\/v2\/tags?post=12049"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}