|
10 | 10 |
|
11 | 11 | Lock1->Lock(); Lock2->Lock();
|
12 | 12 | WaitForLock2(); WaitForLock1(); <-- Oops!</code></pre><p>The best way to avoid deadlocks is to avoid having processes cross over in this way. Reduce the need to lock anything as much as you can. In databases avoid making lots of changes to different tables in a single transaction, avoid triggers and switch to optimistic/dirty/nolock reads as much as possible.</p></div></div><div class="row my-2"><div><span><i>Source:</i> <span><a href="https://stackoverflow.com/questions/34512/what-is-a-deadlock" rel="noreferrer" target="_blank" title="What is a Deadlock? Interview Questions Source To Answer">stackoverflow.com</a></span></span> </div></div></div></div></div> <br><br></div><div data-v-5e9078c0="" class="unit"><div><h2>🔹 2. Explain the difference between Asynchronous and Parallel programming?</h2></div> <div><h3>Answer:</h3> <div class="answer"><div><div><div class="AnswerBody"><p>When you run something <strong>asynchronously</strong> it means it is non-blocking, you execute it without waiting for it to complete and carry on with other things.
|
13 | | -<strong>Parallelism</strong> means to run multiple things at the same time, in parallel. Parallelism works well when you can separate tasks into independent pieces of work. Async and Callbacks are generally a way (tool or mechanism) to express concurrency i.e. a set of entities possibly talking to each other and sharing resources. </p><p>Take for example rendering frames of a 3D animation. To render the animation takes a long time so if you were to launch that render from within your animation editing software you would make sure it was running <em>asynchronously</em> so it didn't lock up your UI and you could continue doing other things. Now, each frame of that animation can also be considered as an individual task. If we have multiple CPUs/Cores or multiple machines available, we can render multiple frames in <em>parallel</em> to speed up the overall workload.</p></div></div><div class="row my-2"><div><span><i>Source:</i> <span><a href="https://stackoverflow.com/questions/6133574/how-to-articulate-the-difference-between-asynchronous-and-parallel-programming#:~:text=13%20Answers&text=When%20you%20run%20something%20asynchronously,into%20independent%20pieces%20of%20work." rel="noreferrer" target="_blank" title="Explain the difference between Asynchronous and Parallel programming? Interview Questions Source To Answer">stackoverflow.com</a></span></span> </div></div></div></div></div> <br><br></div><div data-v-5e9078c0="" class="unit"><div><h2>🔹 3. What is a Mutex?</h2></div> <div><h3>Answer:</h3> <div class="answer"><div><div><div class="AnswerBody"><p>A <strong>Mutex</strong> is a mutually exclusive object. It acts as a gate keeper to synchronise two threads. When you have two threads attempting to access a single resource, the general pattern is to have the first block of code attempting access, to set the <em>mutex</em> before entering the code. When the second code block attempts access, it sees that the <em>mutex</em> is set and waits until the first block of code is complete (and un-sets the mutex), then continues.</p><p>Specific details of how this is accomplished obviously varies greatly by programming language.</p><p></p><div><div><div><div></div></div></div></div><p></p></div></div><div class="row my-2"><div><span><i>Source:</i> <span><a href="https://stackoverflow.com/questions/34524/what-is-a-mutex" rel="noreferrer" target="_blank" title="What is a Mutex? Interview Questions Source To Answer">Stackoverflow.com</a></span></span> </div></div></div></div></div> <br><br></div><div data-v-5e9078c0="" class="unit"><div><h2>🔹 4. Is there any difference between a Binary Semaphore and Mutex?</h2></div> <div> |
14 | | - 👉🏼 Check |
15 | | - <a href="https://devinterview.io/design/concurrency-interview-questions">all 21 answers</a></div> <br><br></div><div data-v-5e9078c0="" class="unit"><div><h2>🔹 5. What is a Race Condition?</h2></div> <div> |
16 | | - 👉🏼 Check |
17 | | - <a href="https://devinterview.io/design/concurrency-interview-questions">all 21 answers</a></div> <br><br></div><div data-v-5e9078c0="" class="unit"><div><h2>🔹 6. Write a function that guarantees to never return the same value twice</h2></div> <div> |
| 13 | +<strong>Parallelism</strong> means to run multiple things at the same time, in parallel. Parallelism works well when you can separate tasks into independent pieces of work. Async and Callbacks are generally a way (tool or mechanism) to express concurrency i.e. a set of entities possibly talking to each other and sharing resources. </p><p>Take for example rendering frames of a 3D animation. To render the animation takes a long time so if you were to launch that render from within your animation editing software you would make sure it was running <em>asynchronously</em> so it didn't lock up your UI and you could continue doing other things. Now, each frame of that animation can also be considered as an individual task. If we have multiple CPUs/Cores or multiple machines available, we can render multiple frames in <em>parallel</em> to speed up the overall workload.</p></div></div><div class="row my-2"><div><span><i>Source:</i> <span><a href="https://stackoverflow.com/questions/6133574/how-to-articulate-the-difference-between-asynchronous-and-parallel-programming#:~:text=13%20Answers&text=When%20you%20run%20something%20asynchronously,into%20independent%20pieces%20of%20work." rel="noreferrer" target="_blank" title="Explain the difference between Asynchronous and Parallel programming? Interview Questions Source To Answer">stackoverflow.com</a></span></span> </div></div></div></div></div> <br><br></div><div data-v-5e9078c0="" class="unit"><div><h2>🔹 3. What is a Mutex?</h2></div> <div><h3>Answer:</h3> <div class="answer"><div><div><div class="AnswerBody"><p>A <strong>Mutex</strong> is a mutually exclusive object. It acts as a gate keeper to synchronise two threads. When you have two threads attempting to access a single resource, the general pattern is to have the first block of code attempting access, to set the <em>mutex</em> before entering the code. When the second code block attempts access, it sees that the <em>mutex</em> is set and waits until the first block of code is complete (and un-sets the mutex), then continues.</p><p>Specific details of how this is accomplished obviously varies greatly by programming language.</p><p></p><div><div><div><div></div></div></div></div><p></p></div></div><div class="row my-2"><div><span><i>Source:</i> <span><a href="https://stackoverflow.com/questions/34524/what-is-a-mutex" rel="noreferrer" target="_blank" title="What is a Mutex? Interview Questions Source To Answer">Stackoverflow.com</a></span></span> </div></div></div></div></div> <br><br></div><div data-v-5e9078c0="" class="unit"><div><h2>🔹 4. Is there any difference between a Binary Semaphore and Mutex?</h2></div> <div><h3>Answer:</h3> <div class="answer"><div><div><div class="AnswerBody"><ul><li><p>A <strong>mutex</strong> (or Mutual Exclusion Semaphores) is a <strong>locking mechanism</strong> used to synchronize <em>access</em> to a resource. Only one task (can be a thread or process based on OS abstraction) can acquire the mutex. It means there will be ownership associated with mutex, and only the owner can release the lock (mutex).</p></li><li><p><strong>Semaphore</strong> (or Binary Semaphore) is <strong>signaling mechanism</strong> ("I am done, you can carry on" kind of signal). For example, if you are listening songs (assume it as one task) on your mobile and at the same time your friend called you, an interrupt will be triggered upon which an interrupt service routine (ISR) will signal the call processing task to wakeup. A binary semaphore is NOT protecting a resource from access. Semaphores are more suitable for some synchronization problems like producer-consumer.</p></li></ul><p>Short version:</p><ul><li>A <strong>mutex</strong> can be released only by <strong>the thread that had acquired it</strong>.</li><li>A <strong>binary semaphore</strong> can be signaled <strong>by any thread</strong> (or process).</li></ul></div></div><div class="row my-2"><div><span><i>Source:</i> <span><a href="https://stackoverflow.com/questions/62814/difference-between-binary-semaphore-and-mutex" rel="noreferrer" target="_blank" title="Is there any difference between a Binary Semaphore and Mutex? Interview Questions Source To Answer">stackoverflow.com</a></span></span> </div></div></div></div></div> <br><br></div><div data-v-5e9078c0="" class="unit"><div><h2>🔹 5. What is a Race Condition?</h2></div> <div><h3>Answer:</h3> <div class="answer"><div><div><div class="AnswerBody"><p>A race condition is a situation on concurrent programming where two concurrent threads or processes compete for a resource and the resulting final state depends on who gets the resource first.</p><p>Because the thread scheduling algorithm can swap between threads at any time, you don't know the order in which the threads will attempt to access the shared data. Therefore, the result of the change in data is dependent on the thread scheduling algorithm, i.e. both threads are "racing" to access/change the data. </p><p>Problems often occur when one thread does a "check-then-act" (e.g. "check" if the value is X, then "act" to do something that depends on the value being X) and another thread does something to the value in between the "check" and the "act". E.g:</p><pre><code>if (x == 5) // The "Check" |
| 14 | +{ |
| 15 | + y = x * 2; // The "Act" |
| 16 | + |
| 17 | + // If another thread changed x in between "if (x == 5)" and "y = x * 2" above, |
| 18 | + // y will not be equal to 10. |
| 19 | +}</code></pre><p>The point being, y could be 10, or it could be anything, depending on whether another thread changed x in between the check and act. You have no real way of knowing.</p><p>In order to prevent race conditions from occurring, you would typically put a lock (Mutex or Semaphores) around the shared data to ensure only one thread can access the data at a time. This would mean something like this:</p><pre><code>// Obtain lock for x |
| 20 | +if (x == 5) |
| 21 | +{ |
| 22 | + y = x * 2; // Now, nothing can change x until the lock is released. |
| 23 | + // Therefore y = 10 |
| 24 | +} |
| 25 | +// release lock for x</code></pre></div></div><div class="row my-2"><div><span><i>Source:</i> <span><a href="https://stackoverflow.com/questions/34510/what-is-a-race-condition" rel="noreferrer" target="_blank" title="What is a Race Condition? Interview Questions Source To Answer">stackoverflow.com</a></span></span> </div></div></div></div></div> <br><br></div><div data-v-5e9078c0="" class="unit"><div><h2>🔹 6. Write a function that guarantees to never return the same value twice</h2></div> <div> |
18 | 26 | 👉🏼 Check
|
19 | 27 | <a href="https://devinterview.io/design/concurrency-interview-questions">all 21 answers</a></div> <br><br></div><div data-v-5e9078c0="" class="unit"><div><h2>🔹 7. Explain Deadlock to 5 years old</h2></div> <div>
|
20 | 28 | 👉🏼 Check
|
|
0 commit comments