Advertisement
| 11.07.2008 at 10:30AM PST, ID: 23886105 |
|
[x]
Attachment Details
|
||
|
[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.
Your Input Matters 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! |
||
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;
}
|
Advertisement