0

I am working on an equivalent to the Dining Philosopher problem in C.

Our subject has a few new rules, adding a Resting state and the following rule:

"The states 'think' and 'eat' have a maximum duration that you'll have to determine."

The following structure has a state variable:

typedef struct  philo_s
{
  pthread_t     handler; // The thread of the philosopher
  char          state; // Either 'E' for eat, 'T' for think or 'R' for rest
  int           dur_think; // Defines the maximum duration of the think state
  int           dur_eat; // Defines the maximum furation of the eat state
  int           max_eat;
  int           num;
}               philo_t;

What I have understood from the rule stated above, is that a given thread can't have the same state for a given amount of time.

So how may I monitor a structure to see if it has had the same value for a defined amount of seconds?

If philosopher 1 can't be in an Eat state for more then one second and he reaches that limit, I will perhaps force him into another state.

Code does not seem relevant since the functions are simply conditions to change the philosophers states.

  • 2
    "Code does not seem relevant" ... and yet, the task is to write code! – ams Mar 07 '16 at 16:59
  • The time that a philosopher spends in the "eat" state is arbitrary, so you can generate a random number (up to a limit that you choose) which determines how long it takes the philosopher to "eat". But the time that the philosopher spends in the "think" state depends on the availability of resources, so an upper bound makes no sense, unless your intent is to print a failure message, and exit the program. – user3386109 Mar 07 '16 at 17:26

0 Answers0