Advertisement

11.07.2008 at 10:30AM PST, ID: 23886105
[x]
Attachment Details

dining philosophers problem

[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.6
Tags:

u have to execute the code in unix environment

hi this is rajesh,
                           the following code is the dining philosophers problem.For that problem  I want
answer for the following questions and I want to know why the dead lock is occcuring in
that problem.The questions are,
Q1. The program as initially written creates and runs only 2
philosophers (and 2 forks).  Run the program multiple times.  What
happens, and does this surprise you?  What is the longest that the
program runs in the trials you perform?  The shortest?


Q2. The header file philo.h contains the definition of constants used
by the program. What happens if you increase the number of
philosophers to 5? to 10? to 100? to 200?  Does this have any effect.
Does the effect surprise you, or not?  Explain your answer.

Caution: When you make a change to a constant, you need to make sure
that you recompile the program.  Changes are of course not actually
reflected in a compiled language, like C, until the program is
recompiled and those changes are added.  If you forget to recompile
while experimenting, you may think you are testing 1 value of the
parameter, when you are actually testing something else.


Q3. What happens if you increase/decrease the think and eat random times of the philosophers?  Does it appear to affect the running of the problem?  Is this$


Q4. Sometimes the output sequence of the thinking or eating messages
appears to be out of order.  For example, with 3 philosophers I saw
this output:


Philosopher <0> is thinking... (entered 262)
Philosopher <1> is thinking... (entered 261)
    philosopher <2> pickedup fork <0>
Philosopher <2> is eating... (entered 260)
    philosopher <2> released fork <0>
    philosopher <2> released fork <2>
Philosopher <2> is thinking... (entered 263)
    philosopher <0> pickedup fork <0>
    philosopher <2> pickedup fork <2>
    philosopher <1> pickedup fork <1>

e.g. The 261'st thinking session appeared after 262 in the output log.

Is this surprising or not.  What is the reason that the output
sequence can appear out of order on the standard output of these
messages?

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
/*
About this program:
- Implementation of the Dining Philosophers problem,
  - Fig 6.12, Operating Systems, Stallings
 
- Example implementation using Posix threads and semaphores
 
- Each philosopher is assigned a philosopher identification number (pid)
  - pids range from 0 to N-1 (where N is the number of philosophers)
  - likewise forst range from 0 to M-1 where M is the number of forks, and in the
    classic dining philosophers problems M is always equal to N
 
- Each philosopher is implemented as a separate posix thread
 
- Each fork is associated with a Posix semaphore, and accessing a fork by a
  philosopher requires the philosopher to sem_wait() until the fork is available,
  and to sem_post() (e.g. signal) when they are done eating and have put the fork
  back on the table.
 
- Messages are displayed on standard output by the philosopher threads when the
  perform events such as picking up and putting down a fork, and when eating and
  thinking.
 
- This initial implementation has 2 philosophers and 2 forks.
 
 */
 
#include "philo.h"
 
// global arrays, hold the fork semaphores (using Posix sem_t semaphores)
// and the philosopher thread data structures (Posix pthread_t thread structures)
sem_t forks[NUM_FORKS];
pthread_t philosopherThread[NUM_PHILOSOPHERS];
 
 
/*
  Cause philosopher pid to think.  We pick some random amount of time from 0 to
  MAX_THINK_TIME and do a busy wait in this function to represent the philosopher
  thinking.
 
  Parameters:
    pid  An integer, the philosopher identification number of the philosopher who is
         to think.
 */
void think(int pid)
{
    fprintf(stdout, "Philosopher <%d> is thinking... (entered %d)\n", pid, thinkCount++);
 
    // do a spin wait
    int i;
    int thinktime = rand() % MAX_THINK_TIME;
    for (i=0; i<thinktime; i++);
}
 
 
/*
  Cause philosopher pid to eat.  We pick some random amount of time from 0 to
  MAX_EAT_TIME and do a busy wait in this function to represent the philosopher
  eating.  This function is identical to thinking, but we split apart to alow
  easier understanding of the different tasks of the philosopher, and to allow for
  different think/eat times to be easily set and experimented with.
 
  Parameters:
    pid  An integer, the philosopher identification number of the philosopher who is
         to eat.
 */
void eat(int pid)
{
    fprintf(stdout, "Philosopher <%d> is eating... (entered %d)\n", pid, eatCount++);
 
    // do a spin wait
    int i;
    int eattime = rand() % MAX_EAT_TIME;
    for (i=0; i<eattime; i++);
}
/*
  The main function run by a philosopher's thread.  This is an
  implementation from fig 6.12 of Stalling's Operating Systems
  textbook.  The philosopher simply performs an infinite loop of
  thinking, followed by eating, forever an ever (what a life!)  In
  order to eat, the philosopher needs 2 forks, so he/she first tries
  to pick up the fork to their right (where the forkd id is equal to
  the philosopher id), and then tries to get the fork to his/her left
  (fork id = philosopher id + 1).
 
  Parameters:
 
    arg A pointer to (potentially a while array) of arguments.  This
        is part of the defined Posix thread interface.  The main
        function of a Posix thread accepts a single void* arg
        parameter, through which we can pass parameters initially into
        the thread.  We pass the philosopher identification number
        (pid) which is the first thing that the thread extracts and
        saves away before the philosopher starts running.
 */
void *philosophosize(void *arg)
{
    int pid = *((int *)(arg)); // philosopher id
    int fork;
    fprintf(stdout, "Entered philosopher <%d>\n", pid);
 
    while (1)
    {
        think(pid);
 
        fork = pid;
        sem_wait(&forks[fork]);
        fprintf(stdout, "    philosopher <%d> pickedup fork <%d>\n", pid, fork);
        fork = (pid+1) % NUM_FORKS;
        sem_wait(&forks[fork]);
        fprintf(stdout, "    philosopher <%d> pickedup fork <%d>\n", pid, fork);
 
        eat(pid);
 
        fork = (pid+1) % NUM_FORKS;
        sem_post(&forks[fork]);
        fprintf(stdout, "    philosopher <%d> released fork <%d>\n", pid, fork);
        fork = pid;
        sem_post(&forks[fork]);
        fprintf(stdout, "    philosopher <%d> released fork <%d>\n", pid, fork);
 
    }
 
   return NULL;
}
 
/*
  Program main function.  We create the semaphores and threads to be
  run by the program, then perform joins to wait for all the threads
  to finish (not really relevant in this program, as the threads are
  programmed to run forever, so they never finsih, and the joins never
  return).
 
  One note: when creating the threads using pthread_create, note that
  we initialize the arg integer array with the the philosopher id
  number.  this arg is passed into the newly created thread, so that
  it knows what its pid is when it begins executing.
 
  Parameters:
    argc The number of command line arguments, unused in this program
    argv An array of strings containing the command line arguments, also unused
 
 */
int main(int argc, char **argv)
{
 
 
    // create the semaphores for the forks
    int i;
    for (i=0; i<NUM_FORKS; i++)
    {
        if (sem_init(&forks[i], 0, 1))
        {
            perror("Unable to initialize fork semaphore");
        }
    }
 
    // let's create our threads
    int arg[NUM_PHILOSOPHERS];
    for (i=0; i<NUM_PHILOSOPHERS; i++)
    {
        arg[i] = i;
        if (pthread_create(&philosopherThread[i], NULL, philosophosize, &arg[i]))
        {
            perror("Unable to create philosopher thread");
        }
    }
 
    // join threads, waits till all threads are done
    for (i=0; i<NUM_PHILOSOPHERS; i++)
    {
        pthread_join(philosopherThread[i], NULL);
    }
 
    // everything is finished, print out the number of operations performed
    fprintf(stdout, "Philosophsizing is done\n");
    return EXIT_SUCCESS;
}
 
Expert Comment by duncan_roe:

All comments and solutions are available to Premium Service Members only. Start your 7-day free trial to view the solution to this question.

Already a member? Login to view this solution.

 
 
Author Comment by annerajgadu:

All comments and solutions are available to Premium Service Members only. Start your 7-day free trial to view the solution to this question.

Already a member? Login to view this solution.

 
 
Author Comment by annerajgadu:

All comments and solutions are available to Premium Service Members only. Start your 7-day free trial to view the solution to this question.

Already a member? Login to view this solution.

 
 
Expert Comment by duncan_roe:

All comments and solutions are available to Premium Service Members only. Start your 7-day free trial to view the solution to this question.

Already a member? Login to view this solution.

 
 
Expert Comment by duncan_roe:

All comments and solutions are available to Premium Service Members only. Start your 7-day free trial to view the solution to this question.

Already a member? Login to view this solution.

 
 
Accepted Solution by duncan_roe:

All comments and solutions are available to Premium Service Members only. Start your 7-day free trial to view the solution to this question.

Already a member? Login to view this solution.

 
 
Assisted Solution by duncan_roe:

All comments and solutions are available to Premium Service Members only. Start your 7-day free trial to view the solution to this question.

Already a member? Login to view this solution.

 
 
Assisted Solution by duncan_roe:

All comments and solutions are available to Premium Service Members only. Start your 7-day free trial to view the solution to this question.

Already a member? Login to view this solution.

 
 
Assisted Solution by duncan_roe:

All comments and solutions are available to Premium Service Members only. Start your 7-day free trial to view the solution to this question.

Already a member? Login to view this solution.

 
 
Assisted Solution by duncan_roe:

All comments and solutions are available to Premium Service Members only. Start your 7-day free trial to view the solution to this question.

Already a member? Login to view this solution.

 
 
Administrative Comment by Vee_Mod:

All comments and solutions are available to Premium Service Members only. Start your 7-day free trial to view the solution to this question.

Already a member? Login to view this solution.

 
 
Expert Comment by duncan_roe:

All comments and solutions are available to Premium Service Members only. Start your 7-day free trial to view the solution to this question.

Already a member? Login to view this solution.

 
 
Administrative Comment by Vee_Mod:

All comments and solutions are available to Premium Service Members only. Start your 7-day free trial to view the solution to this question.

Already a member? Login to view this solution.

 
 
Administrative Comment by Vee_Mod:

All comments and solutions are available to Premium Service Members only. Start your 7-day free trial to view the solution to this question.

Already a member? Login to view this solution.

 
 
20081119-EE-VQP-46 - Hierarchy / EE_QW_3_20080625