### lab 6

parent f465da37
 # Lab 6 CC = gcc CFLAGS = -std=gnu99 -Wall -g #-DDEBUG LIB = -lpthread ALL = dp problem1 problem2 problem3-1 problem3-2 all: \$(ALL) dp: \$(CC) \$(CFLAGS) -o \$@ \$@.c \$(LIB) problem1: \$(CC) \$(CFLAGS) -o \$@ \$@.c \$(LIB) problem2: \$(CC) \$(CFLAGS) -o \$@ \$@.c \$(LIB) problem3-1: \$(CC) \$(CFLAGS) -o \$@ \$@.c \$(LIB) problem3-2: \$(CC) \$(CFLAGS) -o \$@ \$@.c \$(LIB) .PHONY: clean clean: /bin/rm -rf \$(ALL)
 Lab 6 Problem 1: 1. I didn't observe any problems. 2. By analyzing the dining philosopher's problem in terms of deadlock conditions we know that mututal exclusion holds since no two philosopher's can access the chopsticks at the same time. resource holding also holds since a philosopher can have one chopstick and can be waiting for the other. Similarly, no preemption holds since a philosopher will release only after the philosopher is done eating. Lastly, circular wait holds as well since another philosopher may be waiting for a chopstick used by another which in turn can be waited upon hence creating a cycle. Since all the conditions hold a deadlock can happen. Problem 2: 1. The deadlock happened when a philosopher (4) picked up its respective left chopstick and was waiting to pick up the right one but during that wait another philosopher (0) picked up the right chopstick for that philosopher. This repeated for the other philosophers and ultimately each philosopher was left with just one chopstick resulting in a deadlock. Problem 3: 1. Adding those measures decreases the chance of the deadlock happening but the conditions of deadlocks still hold hence the deadlock can happen. \ No newline at end of file
Labs/Lab6/dp.c 0 → 100755
 #include #include #include #include #include #define TIME_FACTOR 1000000 #define NUM_PHILS 5 ///////////////////////， void napping(int t) { unsigned int seed = pthread_self(); int time = (rand_r(&seed) % t) + 1; printf("napping for %d secs\n", time); usleep(time * TIME_FACTOR); } ///////////////////////， void *Philosopher(void *id) { long long pid = (long long) id; while (true) { printf("Philosopher %lld is thinking.\n", pid); napping(2); printf("Philosopher %lld is hungry.\n", pid); printf("Philosopher %lld is starting to eat.\n", pid); napping(1); printf("Philosopher %lld is done eating.\n", pid); } pthread_exit(NULL); } ///////////////////////， int main(int argc, char *argv[]) { pthread_t phil_threads[NUM_PHILS]; int ret; for (int i = 0; i < NUM_PHILS; i++) { ret = pthread_create(&phil_threads[i], NULL, Philosopher, (void*)(long long)i); if (ret) { perror("Error: failed to create philosopher thread!"); exit(-1); } } ///////////////////////， for (int i = 0; i < NUM_PHILS; i++) { ret = pthread_join(phil_threads[i], NULL); if (ret) { perror("Error: failed to join philosopher thread!"); exit(-1); } } ///////////////////////， pthread_exit(NULL); }
 #include #include #include #include #include ///////////////////////， #define TIME_FACTOR 1000000 #define NUM_PHILS 5 #define NUM_CHOPSTICKS 5 ///////////////////////， pthread_mutex_t chopsticks[NUM_CHOPSTICKS]; void napping(int t) { unsigned int seed = pthread_self(); int time = (rand_r(&seed) % t) + 1; printf("napping for %d secs\n", time); usleep(time * TIME_FACTOR); } ///////////////////////， void *Philosopher(void *id) { long long pid = (long long) id; while (true) { printf("Philosopher %lld is thinking.\n", pid); napping(2); printf("Philosopher %lld is hungry.\n", pid); pthread_mutex_lock(&chopsticks[pid]); pthread_mutex_lock(&chopsticks[(pid+1)%5]); printf("Philosopher %lld is starting to eat.\n", pid); napping(1); printf("Philosopher %lld is done eating.\n", pid); pthread_mutex_unlock(&chopsticks[(pid+1)%5]); pthread_mutex_unlock(&chopsticks[pid]); } pthread_exit(NULL); } int main(int argc, char *argv[]) { pthread_t phil_threads[NUM_PHILS]; int ret; for (int i = 0; i < NUM_CHOPSTICKS; i++) { ret = pthread_mutex_init(&chopsticks[i], NULL); if (ret) { perror("Error: failed to initialize Mutex!"); exit(-1); } } for (int i = 0; i < NUM_PHILS; i++) { ret = pthread_create(&phil_threads[i], NULL, Philosopher, (void*)(long long)i); if (ret) { perror("Error: failed to create philosopher thread!"); exit(-1); } } for (int i = 0; i < NUM_PHILS; i++) { ret = pthread_join(phil_threads[i], NULL); if (ret) { perror("Error: failed to join philosopher thread!"); exit(-1); } } pthread_exit(NULL); }
 Philosopher 0 is thinking. napping for 1 secs Philosopher 1 is thinking. napping for 2 secs Philosopher 2 is thinking. napping for 1 secs Philosopher 3 is thinking. napping for 2 secs Philosopher 4 is thinking. napping for 1 secs Philosopher 4 is hungry. Philosopher 0 is hungry. Philosopher 4 is starting to eat. napping for 1 secs Philosopher 2 is hungry. Philosopher 2 is starting to eat. napping for 1 secs Philosopher 2 is done eating. Philosopher 4 is done eating. Philosopher 3 is hungry. Philosopher 4 is thinking. napping for 1 secs Philosopher 3 is starting to eat. napping for 1 secs Philosopher 1 is hungry. Philosopher 2 is thinking. napping for 1 secs Philosopher 0 is starting to eat. napping for 1 secs Philosopher 0 is done eating. Philosopher 3 is done eating. Philosopher 0 is thinking. Philosopher 3 is thinking. napping for 2 secs napping for 1 secs Philosopher 2 is hungry. Philosopher 2 is starting to eat. napping for 1 secs Philosopher 4 is hungry. Philosopher 4 is starting to eat. napping for 1 secs Philosopher 0 is hungry. Philosopher 2 is done eating. Philosopher 2 is thinking. napping for 1 secs Philosopher 1 is starting to eat. napping for 1 secs Philosopher 4 is done eating. Philosopher 4 is thinking. napping for 1 secs Philosopher 1 is done eating. Philosopher 1 is thinking. napping for 2 secs Philosopher 2 is hungry. Philosopher 2 is starting to eat. napping for 1 secs Philosopher 3 is hungry. Philosopher 0 is starting to eat. napping for 1 secs Philosopher 4 is hungry. Philosopher 2 is done eating. Philosopher 2 is thinking. napping for 1 secs Philosopher 0 is done eating. Philosopher 0 is thinking. napping for 1 secs Philosopher 4 is starting to eat. napping for 1 secs Philosopher 1 is hungry. Philosopher 1 is starting to eat. napping for 1 secs Philosopher 0 is hungry. Philosopher 2 is hungry. Philosopher 4 is done eating. Philosopher 4 is thinking. napping for 1 secs Philosopher 3 is starting to eat. napping for 1 secs Philosopher 1 is done eating. Philosopher 1 is thinking. napping for 2 secs Philosopher 3 is done eating. Philosopher 3 is thinking. napping for 2 secs Philosopher 4 is hungry. Philosopher 2 is starting to eat. napping for 1 secs Philosopher 0 is starting to eat. napping for 1 secs Philosopher 0 is done eating. Philosopher 0 is thinking. napping for 1 secs Philosopher 2 is done eating. Philosopher 2 is thinking. napping for 1 secs Philosopher 4 is starting to eat. napping for 1 secs Philosopher 1 is hungry. Philosopher 1 is starting to eat. napping for 1 secs Philosopher 3 is h \ No newline at end of file
 #include #include #include #include #include #define TIME_FACTOR 1000000 #define NUM_PHILS 5 #define NUM_CHOPSTICKS 5 ///////////////////////， pthread_mutex_t chopsticks[NUM_CHOPSTICKS]; ///////////////////////， void napping(int t) { unsigned int seed = pthread_self(); int time = (rand_r(&seed) % t) + 1; printf("napping for %d secs\n", time); usleep(time * TIME_FACTOR); } void *Philosopher(void *id) { long long pid = (long long) id; long long rcs = (pid+1)%5; while (true) { printf("Philosopher %lld is thinking.\n", pid); napping(2); printf("Philosopher %lld is hungry.\n", pid); printf("Philosopher %lld is picking up chopstick %lld.\n", pid, pid); pthread_mutex_lock(&chopsticks[pid]); napping(3); printf("Philosopher %lld is picking up chopstick %lld.\n", pid, rcs); pthread_mutex_lock(&chopsticks[rcs]); printf("Philosopher %lld is starting to eat.\n", pid); napping(1); printf("Philosopher %lld is done eating.\n", pid); printf("Philosopher %lld is putting down chopstick %lld.\n", pid, rcs); pthread_mutex_unlock(&chopsticks[rcs]); napping(3); printf("Philosopher %lld is putting down chopstick %lld.\n", pid, pid); pthread_mutex_unlock(&chopsticks[pid]); } pthread_exit(NULL); } ///////////////////////， int main(int argc, char *argv[]) { pthread_t phil_threads[NUM_PHILS]; int ret; for (int i = 0; i < NUM_CHOPSTICKS; i++) { ret = pthread_mutex_init(&chopsticks[i], NULL); if (ret) { perror("Error: failed to initialize Mutex!"); exit(-1); } } for (int i = 0; i < NUM_PHILS; i++) { ret = pthread_create(&phil_threads[i], NULL, Philosopher, (void*)(long long)i); if (ret) { perror("Error: failed to create philosopher thread!"); exit(-1); } } for (int i = 0; i < NUM_PHILS; i++) { ret = pthread_join(phil_threads[i], NULL); if (ret) { perror("Error: failed to join philosopher thread!"); exit(-1); } } pthread_exit(NULL); }
 Philosopher 0 is thinking. Philosopher 1 is thinking. napping for 2 secs Philosopher 3 is thinking. napping for 2 secs napping for 1 secs Philosopher 2 is thinking. napping for 1 secs Philosopher 4 is thinking. napping for 1 secs Philosopher 4 is hungry. Philosopher 4 is picking up chopstick 4. napping for 2 secs Philosopher 2 is hungry. Philosopher 2 is picking up chopstick 2. napping for 2 secs Philosopher 0 is hungry. Philosopher 0 is picking up chopstick 0. napping for 2 secs Philosopher 1 is hungry. Philosopher 1 is picking up chopstick 1. napping for 3 secs Philosopher 3 is hungry. Philosopher 3 is picking up chopstick 3. napping for 2 secs Philosopher 4 is picking up chopstick 0. Philosopher 2 is picking up chopstick 3. Philosopher 0 is picking up chopstick 1. Philosopher 3 is picking up chopstick 4. Philosopher 1 is picking up chopstick 2.
 #include #include #include #include #include ///////////////////////， #define TIME_FACTOR 1000000 #define NUM_PHILS 5 #define NUM_CHOPSTICKS 5 ///////////////////////， pthread_mutex_t chopsticks[NUM_CHOPSTICKS]; ///////////////////////， void napping(int t) { unsigned int seed = pthread_self(); int time = (rand_r(&seed) % t) + 1; printf("napping for %d secs\n", time); usleep(time * TIME_FACTOR); } void *Philosopher(void *id) { long long pid = (long long) id; long long rcs = (pid+1)%5; while (true) { printf("Philosopher %lld is thinking.\n", pid); napping(2); printf("Philosopher %lld is hungry.\n", pid); if (pid%2 == 0) { printf("Philosopher %lld is picking up chopstick %lld.\n", pid, pid); pthread_mutex_lock(&chopsticks[pid]); napping(3); printf("Philosopher %lld is picking up chopstick %lld.\n", pid, rcs); pthread_mutex_lock(&chopsticks[rcs]); } else { printf("Philosopher %lld is picking up chopstick %lld.\n", pid, pid); pthread_mutex_lock(&chopsticks[rcs]); napping(3); printf("Philosopher %lld is picking up chopstick %lld.\n", pid, rcs); pthread_mutex_lock(&chopsticks[pid]); } printf("Philosopher %lld is starting to eat.\n", pid); napping(1); printf("Philosopher %lld is done eating.\n", pid); printf("Philosopher %lld is putting down chopstick %lld.\n", pid, rcs); pthread_mutex_unlock(&chopsticks[rcs]); napping(3); printf("Philosopher %lld is putting down chopstick %lld.\n", pid, pid); pthread_mutex_unlock(&chopsticks[pid]); } pthread_exit(NULL); } int main(int argc, char *argv[]) { pthread_t phil_threads[NUM_PHILS]; int ret; for (int i = 0; i < NUM_CHOPSTICKS; i++) { ret = pthread_mutex_init(&chopsticks[i], NULL); if (ret) { perror("Error: failed to initialize Mutex!"); exit(-1); } } for (int i = 0; i < NUM_PHILS; i++) { ret = pthread_create(&phil_threads[i], NULL, Philosopher, (void*)(long long)i); if (ret) { perror("Error: failed to create philosopher thread!"); exit(-1); } } for (int i = 0; i < NUM_PHILS; i++) { ret = pthread_join(phil_threads[i], NULL); if (ret) { perror("Error: failed to join philosopher thread!"); exit(-1); } } pthread_exit(NULL); }
 #include #include #include #include #include ///////////////////////， #define TIME_FACTOR 1000000 #define NUM_PHILS 5 #define NUM_CHOPSTICKS 5 ///////////////////////， pthread_mutex_t chopsticks[NUM_CHOPSTICKS]; ///////////////////////， void napping(int t) { unsigned int seed = pthread_self(); int time = (rand_r(&seed) % t) + 1; printf("napping for %d secs\n", time); usleep(time * TIME_FACTOR); } void *Philosopher(void *id) { long long pid = (long long) id; long long rcs = (pid+1)%5; while (true) { printf("Philosopher %lld is thinking.\n", pid); napping(2); printf("Philosopher %lld is hungry.\n", pid); if (pid < rcs) { printf("Philosopher %lld is picking up chopstick %lld.\n", pid, pid); pthread_mutex_lock(&chopsticks[pid]); napping(3); printf("Philosopher %lld is picking up chopstick %lld.\n", pid, rcs); pthread_mutex_lock(&chopsticks[rcs]); } else { printf("Philosopher %lld is picking up chopstick %lld.\n", pid, pid); pthread_mutex_lock(&chopsticks[rcs]); napping(3); printf("Philosopher %lld is picking up chopstick %lld.\n", pid, rcs); pthread_mutex_lock(&chopsticks[pid]); } printf("Philosopher %lld is starting to eat.\n", pid); napping(1); printf("Philosopher %lld is done eating.\n", pid); printf("Philosopher %lld is putting down chopstick %lld.\n", pid, rcs); pthread_mutex_unlock(&chopsticks[rcs]); napping(3); printf("Philosopher %lld is putting down chopstick %lld.\n", pid, pid); pthread_mutex_unlock(&chopsticks[pid]); } pthread_exit(NULL); } int main(int argc, char *argv[]) { pthread_t phil_threads[NUM_PHILS]; int ret; for (int i = 0; i < NUM_CHOPSTICKS; i++) { ret = pthread_mutex_init(&chopsticks[i], NULL); if (ret) { perror("Error: failed to initialize Mutex!"); exit(-1); } } for (int i = 0; i < NUM_PHILS; i++) { ret = pthread_create(&phil_threads[i], NULL, Philosopher, (void*)(long long)i); if (ret) { perror("Error: failed to create philosopher thread!"); exit(-1); } } for (int i = 0; i < NUM_PHILS; i++) { ret = pthread_join(phil_threads[i], NULL); if (ret) { perror("Error: failed to join philosopher thread!"); exit(-1); } } pthread_exit(NULL); }
 Lab 6 Problem 1: I added all the required wrappers, they worked when called in echod and echoreq Problem 2: I completed all the requirements and the code works as expected. Problem 3: I completed all the requirements and the code works as expected.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!