Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Programming Threads > need help findi...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 74 Topic 4080 of 4146
Post > Topic >>

need help finding a "possible" deadlock in simple application...

by "Chris M. Thomasson" <no@[EMAIL PROTECTED] > Oct 10, 2008 at 02:30 PM

Here is the code which solves the dining philosophers problem:

http://webpages.charter.net/appcore/misc/diner_c.html

Apparently, Szabolcs Ferenczi says this deadlocks as-is. Well, I am having

kind of a hard time determining where the bug is. Can you help me? Can you

please execute the code a couple of times and see if it runs to
completion? 
I am having a hard time getting it to deadlock. I guess I will code it up
in 
Relacy for a last resort!  ;^)




The code as-is runs a try-lock algorithm. Here is the relevant function:
__________________________________________________________________
void
diner_eat_trylock(
  diner* const _this
) {
  int status, i = 1, old;
  puts("********** diner_eat_trylock **********");
  /* lock in reverse-order on purpose... */
  while(! (status =
    pthread_mutex_lock(&_this->forks[i]->lock))) {
    old = i;
    i = (old) ? 0 : 1;
    if (! (status =
           pthread_mutex_trylock(&_this->forks[i]->lock))) {
      break;
    }
    assert(old != i);
    if (status != EBUSY) {
      break;
    }
    if ((status =
         pthread_mutex_unlock(&_this->forks[old]->lock))) {
      break;
    }
    sched_yield();
  }
  if (status) { assert(! status); abort(); }
}
__________________________________________________________________




There is only one unlock function:
__________________________________________________________________
void
diner_eat_unlock(
  diner* const _this
) {
  int status, i = 1;
  /* unlock in reverse-order... */
  for(; i > -1; --i) {
    if ((status =
      pthread_mutex_unlock(&_this->forks[i]->lock))) {
      break;
    }
  }
  if (status) { assert(! status); abort(); }
}
__________________________________________________________________




I can't see a deadlock off hand wrt the interaction between those two 
functions. Here is the thread entry procedure, its setup so that there is
a 
thread per-diner:
__________________________________________________________________
void*
diner_thread(
  void* this_state
) {
  int i = 0;
  diner* const _this = this_state;
  diner_wait(_this, i, "Has Been Seated!");
  for(++i; pg_run && i < DINER_ITERATIONS(); ++i) {
    if (i % 2) {
      diner_think(_this, i);
    } else {
      diner_eat(_this, i);
    }
  }
  pg_run = 0;
  diner_wait(_this, i, "Has Left The Table!");
  return 0;
}
__________________________________________________________________




`pg_run' is a simple volatile variable used to shutdown all threads when
one 
diner is finished. The program can run with any number of diners at the 
table: 1, 2, 3, 4 or 5. You can try it by tweaking the following
functions:
__________________________________________________________________
void diner_startup(
  diner _this[5]
) {
  int status, i = 0;
  pg_run = 1;
  for(; i < 5; ++i) {
    diner_sort_assert(&_this[i]);
    if ((status =
      pthread_create(&_this[i].tid, 0,
        diner_thread, &_this[i]))) {
      break;
    }
  }
  if (status) { assert(! status); abort(); }
  puts("diner_startup");
}


void diner_shutdown(
  diner _this[5]
) {
  int status, i = 0;
  for(; i < 5; ++i) {
    if ((status =
      pthread_join(_this[i].tid, 0))) {
      break;
    }
  }
  if (status) { assert(! status); abort(); }
  puts("diner_shutdown");
}
__________________________________________________________________




Just switch the 5 in the functions above two a lower number to get both of

the functions to spawn that many diners. The only function which ever
calls 
the locks/unlock functions is `diner_eat()' which is as follows:
__________________________________________________________________
void
diner_eat(
  diner* const _this,
  int const seq
) {
  /* lock */
  if (_this->flags & DINER_FLAG_SORTLOCK()) {
    diner_eat_sortlock(_this);
  } else {
    diner_eat_trylock(_this);
  }

  /* wait */
  diner_wait(_this, seq, "Is Eating Spaghettii!");

  /* unlock */
  diner_eat_unlock(_this);
}
__________________________________________________________________




The only thing that comes to mind is that he did not link to the
thread-safe 
version of the crt. However, I still would greatly appreciate any
feedback.




Thank you so very much for your time and energy!
 




 74 Posts in Topic:
need help finding a "possible" deadlock in simple application...
"Chris M. Thomasson&  2008-10-10 14:30:41 
Re: need help finding a "possible" deadlock in simple applicatio
Szabolcs Ferenczi <sza  2008-10-10 14:27:24 
Re: need help finding a "possible" deadlock in simple applicatio
"Chris M. Thomasson&  2008-10-10 14:36:52 
Re: need help finding a "possible" deadlock in simple applicatio
"Chris M. Thomasson&  2008-10-10 15:18:10 
Re: need help finding a "possible" deadlock in simple applicatio
"Chris M. Thomasson&  2008-10-10 15:31:10 
Re: need help finding a "possible" deadlock in simple applicatio
Szabolcs Ferenczi <sza  2008-10-10 15:24:04 
Re: need help finding a "possible" deadlock in simple applicatio
"Chris M. Thomasson&  2008-10-10 16:19:53 
Re: need help finding a "possible" deadlock in simple applicatio
"Dmitriy V'jukov&quo  2008-10-10 15:51:04 
Re: need help finding a "possible" deadlock in simple applicatio
"Chris M. Thomasson&  2008-10-10 16:16:23 
Re: need help finding a "possible" deadlock in simple applicatio
Szabolcs Ferenczi <sza  2008-10-10 15:58:50 
Re: need help finding a "possible" deadlock in simple applicatio
"Chris M. Thomasson&  2008-10-10 16:21:45 
Re: need help finding a "possible" deadlock in simple applicatio
Szabolcs Ferenczi <sza  2008-10-10 16:23:48 
Re: need help finding a "possible" deadlock in simple applicatio
"Chris M. Thomasson&  2008-10-10 16:38:25 
Re: need help finding a "possible" deadlock in simple applicatio
"Dmitriy V'jukov&quo  2008-10-10 16:40:40 
Re: need help finding a "possible" deadlock in simple applicatio
"Chris M. Thomasson&  2008-10-11 00:27:33 
Re: need help finding a "possible" deadlock in simple applicatio
Szabolcs Ferenczi <sza  2008-10-12 04:56:55 
Re: need help finding a "possible" deadlock in simple applicatio
"Dmitriy V'jukov&quo  2008-10-12 06:47:12 
Re: need help finding a "possible" deadlock in simple applicatio
"Chris M. Thomasson&  2008-10-12 08:55:34 
Re: need help finding a "possible" deadlock in simple applicatio
Szabolcs Ferenczi <sza  2008-10-12 07:36:19 
Re: need help finding a "possible" deadlock in simple applicatio
"Dmitriy V'jukov&quo  2008-10-12 08:40:32 
Re: need help finding a "possible" deadlock in simple applicatio
Szabolcs Ferenczi <sza  2008-10-12 09:06:27 
Re: need help finding a "possible" deadlock in simple applicatio
"Chris M. Thomasson&  2008-10-12 10:47:53 
Re: need help finding a "possible" deadlock in simple applicatio
"Dmitriy V'jukov&quo  2008-10-12 09:53:09 
Re: need help finding a "possible" deadlock in simple applicatio
"Dmitriy V'jukov&quo  2008-10-12 09:55:19 
Re: need help finding a "possible" deadlock in simple applicatio
"Dmitriy V'jukov&quo  2008-10-12 10:00:41 
Re: need help finding a "possible" deadlock in simple applicatio
"Dmitriy V'jukov&quo  2008-10-12 10:05:14 
Re: need help finding a "possible" deadlock in simple applicatio
"Dmitriy V'jukov&quo  2008-10-12 10:07:31 
Re: need help finding a "possible" deadlock in simple applicatio
"Dmitriy V'jukov&quo  2008-10-12 10:27:38 
Re: need help finding a "possible" deadlock in simple applicatio
"Dmitriy V'jukov&quo  2008-10-12 10:43:24 
Re: need help finding a "possible" deadlock in simple applicatio
"Dmitriy V'jukov&quo  2008-10-12 10:59:21 
Re: need help finding a "possible" deadlock in simple applicatio
"Chris M. Thomasson&  2008-10-13 09:47:38 
Re: need help finding a "possible" deadlock in simple applicatio
Szabolcs Ferenczi <sza  2008-10-10 17:16:05 
Re: need help finding a "possible" deadlock in simple applicatio
"Chris M. Thomasson&  2008-10-11 00:22:03 
Re: need help finding a "possible" deadlock in simple applicatio
"Chris M. Thomasson&  2008-10-11 22:55:21 
Re: need help finding a "possible" deadlock in simple applicatio
Szabolcs Ferenczi <sza  2008-10-11 10:16:30 
Re: need help finding a "possible" deadlock in simple applicatio
"Chris M. Thomasson&  2008-10-12 11:01:51 
Re: need help finding a "possible" deadlock in simple applicatio
Szabolcs Ferenczi <sza  2008-10-13 13:03:20 
Re: need help finding a "possible" deadlock in simple applicatio
"Chris M. Thomasson&  2008-10-13 16:18:16 
Re: need help finding a "possible" deadlock in simple applicatio
Szabolcs Ferenczi <sza  2008-10-12 12:25:12 
Re: need help finding a "possible" deadlock in simple applicatio
"Chris M. Thomasson&  2008-10-12 15:07:59 
Re: need help finding a "possible" deadlock in simple applicatio
Szabolcs Ferenczi <sza  2008-10-12 15:36:38 
Re: need help finding a "possible" deadlock in simple applicatio
"Chris M. Thomasson&  2008-10-12 15:55:37 
Re: need help finding a "possible" deadlock in simple applicatio
Szabolcs Ferenczi <sza  2008-10-12 16:06:08 
Re: need help finding a "possible" deadlock in simple applicatio
Szabolcs Ferenczi <sza  2008-10-12 16:18:41 
Re: need help finding a "possible" deadlock in simple applicatio
"Chris M. Thomasson&  2008-10-13 16:28:20 
Re: need help finding a "possible" deadlock in simple applicatio
Szabolcs Ferenczi <sza  2008-10-12 17:29:41 
Re: need help finding a "possible" deadlock in simple applicatio
"Chris M. Thomasson&  2008-10-13 09:44:43 
Re: need help finding a "possible" deadlock in simple applicatio
Szabolcs Ferenczi <sza  2008-10-13 10:00:48 
Re: need help finding a "possible" deadlock in simple applicatio
"Dmitriy V'jukov&quo  2008-10-13 10:15:26 
Re: need help finding a "possible" deadlock in simple applicatio
Szabolcs Ferenczi <sza  2008-10-13 10:27:36 
Re: need help finding a "possible" deadlock in simple applicatio
"Chris M. Thomasson&  2008-10-13 10:58:54 
Re: need help finding a "possible" deadlock in simple applicatio
Szabolcs Ferenczi <sza  2008-10-13 11:24:52 
Re: need help finding a "possible" deadlock in simple applicatio
"Chris M. Thomasson&  2008-10-13 16:16:20 
Re: need help finding a "possible" deadlock in simple applicatio
Szabolcs Ferenczi <sza  2008-10-13 11:30:16 
Re: need help finding a "possible" deadlock in simple applicatio
"Dmitriy V'jukov&quo  2008-10-13 12:14:09 
Re: need help finding a "possible" deadlock in simple applicatio
"Dmitriy V'jukov&quo  2008-10-13 12:21:36 
Re: need help finding a "possible" deadlock in simple applicatio
"Dmitriy V'jukov&quo  2008-10-13 12:23:50 
Re: need help finding a "possible" deadlock in simple applicatio
Szabolcs Ferenczi <sza  2008-10-13 12:25:51 
Re: need help finding a "possible" deadlock in simple applicatio
Szabolcs Ferenczi <sza  2008-10-13 12:27:19 
Re: need help finding a "possible" deadlock in simple applicatio
Szabolcs Ferenczi <sza  2008-10-13 12:31:28 
Re: need help finding a "possible" deadlock in simple applicatio
Szabolcs Ferenczi <sza  2008-10-13 12:38:39 
Re: need help finding a "possible" deadlock in simple applicatio
"Dmitriy V'jukov&quo  2008-10-13 12:43:43 
Re: need help finding a "possible" deadlock in simple applicatio
Szabolcs Ferenczi <sza  2008-10-13 12:45:28 
Re: need help finding a "possible" deadlock in simple applicatio
"Dmitriy V'jukov&quo  2008-10-13 12:47:31 
Re: need help finding a "possible" deadlock in simple applicatio
"Dmitriy V'jukov&quo  2008-10-13 12:49:59 
Re: need help finding a "possible" deadlock in simple applicatio
Szabolcs Ferenczi <sza  2008-10-13 12:51:42 
Re: need help finding a "possible" deadlock in simple applicatio
"Chris M. Thomasson&  2008-10-13 16:21:05 
Re: need help finding a "possible" deadlock in simple applicatio
"Dmitriy V'jukov&quo  2008-10-13 12:53:57 
Re: need help finding a "possible" deadlock in simple applicatio
Szabolcs Ferenczi <sza  2008-10-13 12:54:51 
Re: need help finding a "possible" deadlock in simple applicatio
Szabolcs Ferenczi <sza  2008-10-13 12:57:02 
Re: need help finding a "possible" deadlock in simple applicatio
"Chris M. Thomasson&  2008-10-13 16:23:57 
Re: need help finding a "possible" deadlock in simple applicatio
"Chris M. Thomasson&  2008-10-13 17:27:57 
Re: need help finding a "possible" deadlock in simple applicatio
Szabolcs Ferenczi <sza  2008-10-14 02:26:32 
Re: need help finding a "possible" deadlock in simple applicatio
"Chris M. Thomasson&  2008-10-14 10:52:32 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Nov 22 8:49:05 CST 2008.