[cs631apue] Question for Lecture on 12/2

Jan Schaumann jschauma at stevens.edu
Tue Dec 3 11:16:06 EST 2019


Gabrielle M Mccormack <gmccorma at stevens.edu> wrote:
 
> Are mutexes or semaphores better for preventing
> deadlocks or are they each applicable to their own
> respective ways?


If you're using the term 'mutex' here to refer to the
locking mechanisms in lecture 12, then the most
notable distinction is that locks are directly
associated with a file (either the whole file or or a
region in a file), while semaphores are entirely
independent kernel structures.

Either can be advantageous (or disadvantageous) in its
own way, for example:
- locks don't have to be cleaned up, as they are
  automatically released when your process terminates
- you can have multiple shared locks, but you can't
  have multiple simultaneous consumers of a semaphore

> Is there ever a time when deadlocks are inevitable?

Well, that depends entirely on how the code is
written. :-)  Deadlocks are a logic problem,
regardless of the technical implementation.  That is,
if your problem requires coordination between multiple
parties, then no technical solution will prevent you
from accidentally writing code that leads to a
deadlock.

Some programming languages, libraries, or tools offer
some level of deadlock detection, but by necessity,
100% deadlock detection is eventually a variation of
the halting problem (even though for most cases you
can determine whether a deadlock is prevented because
you can set sufficient constraints on the detection
algorithm).

When handling concurrency, you will likely have to
trade off exclusive access and performance, as any
blocked access implies (potentially drastic or long
lasting) performance degredation.  This, then, becomes
an illustration of the CAP theorem...

-Jan


More information about the cs631apue mailing list