/* * Problem 1: Dining Philosophers * * N silent philosophers sit at a table aro
ID: 3659116 • Letter: #
Question
/* * Problem 1: Dining Philosophers * * N silent philosophers sit at a table around a bowl of spaghetti. * A spork (spoon-fork) is placed between each pair of adjancent * philosophers. Each philosopher must alternately think and eat. * However, a philosopher can only eat spaghetti when she has both * left and right sporks. Each spork can be held by only one philosopher * and so a philosopher can use the spork only if it's not being used * by another philosopher. After she finishes eating, she needs to * put down both the spork so they become available to others. A * philosopher can grab the spork on her right or the one on her left * as they become available. * * The problem is to design a concurrent algorithm such that each * philosopher won't starve, assuming that any philosopher can not * know when others may want to eat or think. * * Implement this algorithm free of deadlock and starvation. * Most of your code will go in philosopher_thread(). */ #define _BSD_SOURCE // usleep is from BSD #include #include #include #include #include #include #include /* philosopher's state */ typedef enum _state { STATE_THINKING = 'T', STATE_HUNGRY = 'H', STATE_EATING = 'E' } state_t; /* a single spork */ typedef struct _spork { bool down; } spork_t; /* a single philosopher */ typedef struct _philosopher { int id; state_t state; spork_t *left, *right; struct _philosopher *left_phil, *right_phil; double avg_wait; bool do_random; } philosopher_t; /* mutex for printing */ static pthread_mutex_t stdout_lock; /* thread-safe printf */ #define LOCKED_PRINTF(fmt, args...) pthread_mutex_lock(&stdout_lock); printf(fmt, args); fflush(stdout); pthread_mutex_unlock(&stdout_lock) /* returns the approximate elapsed time since the first call (in seconds) */ static struct timeval time0; double elapsed_time() { struct timeval tv; gettimeofday(&tv, NULL); return (tv.tv_sec - time0.tv_sec + (tv.tv_usec - time0.tv_usec) / 1.e6 ); } /* update the state of the philospher */ void update_state(philosopher_t *philosopher, state_t state) { philosopher->state = state; LOCKED_PRINTF("(t=%f) %d: %c ", elapsed_time(), philosopher->id, philosopher->state); } /* grab the left spork */ void pickup_left(philosopher_t *philosopher) { philosopher->left->down = false; LOCKED_PRINTF("(t=%f) %d: LEFT UP ", elapsed_time(), philosopher->id); } /* grab the right spork */ void pickup_right(philosopher_t *philosopher) { philosopher->right->down = false; LOCKED_PRINTF("(t=%f) %d: RIGHT UP ", elapsed_time(), philosopher->id); } /* release the left spork */ void putdown_left(philosopher_t *philosopher) { philosopher->left->down = true; LOCKED_PRINTF("(t=%f) %d: LEFT DOWN ", elapsed_time(), philosopher->id); } /* release the right spork */ void putdown_right(philosopher_t *philosopher) { philosopher->right->down = true; LOCKED_PRINTF("(t=%f) %d: RIGHT DOWN ", elapsed_time(), philosopher->id); } /* sleep some random amount of time */ void phil_sleep(philosopher_t *philosopher) { int wait_time_us = philosopher->avg_wait * 1e6; if (philosopher->do_random) { wait_time_us = rand() / (RAND_MAX + 1.0) * 2.0 * wait_time_us; } usleep(wait_time_us); } /* philosopher thread (naive solution) */ void *philosopher_thread(void *arg) { philosopher_t *philosopher = (philosopher_t *) arg; while (true) { // I want to think ... update_state(philosopher, STATE_THINKING); phil_sleep(philosopher); // I'm hungry, I want to eat ... update_state(philosopher, STATE_HUNGRY); // gotta pick up the sporks before I can eat while (!philosopher->left->down) // wait until left is available phil_sleep(philosopher); pickup_left(philosopher); while (!philosopher->right->down) // wait until right is available phil_sleep(philosopher); pickup_right(philosopher); // FOOD! update_state(philosopher, STATE_EATING); // wait while I finish this rice phil_sleep(philosopher); // okay, put the sporks down putdown_left(philosopher); putdown_right(philosopher); } pthread_exit(NULL); } /* display usage instructions */ void usage(char *arg0) { fprintf(stderr, "Usage: %s num_philosophers avg_wait_seconds use_randomness " "Example: %s 5 0.5 1 ", arg0, arg0); exit(-1); } int main(int argc, char *argv[]) { gettimeofday(&time0, NULL); int n; double avg_wait; bool do_random; /* enough arguments? */ if (argc < 4) { usage(argv[0]); } n = atoi(argv[1]); avg_wait = atof(argv[2]); do_random = atoi(argv[3]); /* init mutex for printing */ pthread_mutex_init(&stdout_lock, NULL); /* create N sporks */ spork_t *sporks = (spork_t *) malloc(sizeof(spork_t) * n); for (int i = 0; i < n; i++) { sporks[i].down = true; } /* create N philosophers */ philosopher_t *philosophers = (philosopher_t *) malloc(sizeof(philosopher_t) * n); for (int i = 0; i < n; i++) { philosophers[i].id = i; philosophers[i].left = &sporks[i]; philosophers[i].right = &sporks[(i + 1) % n]; philosophers[i].left_phil = &philosophers[(i + n - 1) % n]; philosophers[i].right_phil = &philosophers[(i + 1) % n]; philosophers[i].avg_wait = avg_wait; philosophers[i].do_random = do_random; philosophers[i].state = STATE_THINKING; } /* create N threads - and start them */ pthread_t *pids = (pthread_t *) malloc(sizeof(pthread_t) * n); for (int i = 0; i < n; i++) { pthread_create(&pids[i], NULL, philosopher_thread, &philosophers[i]); } for (int i = 0; i < n; i++) { pthread_join(pids[i], NULL); } free(pids); free(philosophers); free(sporks); pthread_mutex_destroy(&stdout_lock); }Explanation / Answer
#include #include #include #include char fn2[20]; main() { int c; clrscr(); do{ printf(" Main Menu ------------------------------- "); printf("1.Copy a File 2.Move a File 3.Exit "); scanf("%d",&c); switch(c) { case 1: copy_file(); break; case 2: move_file(); break; case 3: exit(0); } }while(c=20) { printf("Press any key to continue"); getchar(); p=0; } } return 0; }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.