Commit a11474c4 authored by Son Pham's avatar Son Pham

Lab7, Problem 3 Completed

parent e87386dd
CC = gcc
CFLAGS = -g
LFLAGS = -lpthread
ALL = dp problem1 problem2
ALL = dp problem1 problem2 problem3-1 problem3-2
all : $(ALL)
......@@ -11,5 +11,9 @@ problem1: problem1.c
$(CC) $(CFLAGS) $(LFLAGS) problem1.c -o problem1
problem2: problem2.c
$(CC) $(CFLAGS) $(LFLAGS) problem2.c -o problem2
problem3-1: problem3-1.c
$(CC) $(CFLAGS) $(LFLAGS) problem3-1.c -o problem3-1
problem3-2: problem3-2.c
$(CC) $(CFLAGS) $(LFLAGS) problem3-2.c -o problem3-2
clean:
/bin/rm -f *.o *~ core $(ALL)
\ No newline at end of file
......@@ -3,7 +3,7 @@
# Prof: Luiz Felipe Perrone
# answers.txt
[1.1] About three seconds into the execution, The program seems to stop functioning entirely with a bunch of philosophers getting hungry.
[1.1] The program seems to keep running quite smoothly. It entered deadlock after I run it for about one minute.
[1.2] Deadlock could happen because current execution of the program satisfy all conditions for deadlock:
......@@ -13,3 +13,5 @@
- Circular wait: Philosopher tries to request the right chopsticks in circle.
[2.1] The situation that lead to deadlock is when all five philosophers picked upt he left chopstick. All five philosophers wait for their right chopsticks to no avail
[3.1] These solutions eliminate deadlock entirely since they break the circular wait.
......@@ -20,7 +20,7 @@ void* philosopher(void* param) {
// Cast the param back to ID
int id = (intptr_t) param;
// Since ID is different across philosopher, use ID as seed
unsigned int seed = id;
unsigned int seed = id + 1;
// Temporary variable t
int t;
......
......@@ -63,7 +63,7 @@ void* philosopher(void* param) {
// Eating seciont
printf("Philosopher %d is hungry\n", id);
pthread_mutex_lock(&chopsticks[id]);
pthread_mutex_lock(&chopsticks[(id+1)/NUM_THREADS]);
pthread_mutex_lock(&chopsticks[(id+1)%NUM_THREADS]);
// -- Critical section
printf("Philosopher %d is ready to eat\n", id);
// Eat for some amount of time
......@@ -75,7 +75,7 @@ void* philosopher(void* param) {
// Releasing the chopsticls
pthread_mutex_unlock(&chopsticks[id]);
pthread_mutex_unlock(&chopsticks[(id+1)/NUM_THREADS]);
pthread_mutex_unlock(&chopsticks[(id+1)%NUM_THREADS]);
}
}
......@@ -107,4 +107,4 @@ int destroy_mutexes() {
pthread_mutex_destroy(&chopsticks[i]);
}
return 0;
};
}
Philosopher 3 is thinking
Philosopher 1 is thinking
Philosopher 2 is thinking
Philosopher 0 is thinking
Philosopher 4 is thinking
Philosopher 4 is hungry
Philosopher 4 is ready to eat
Philosopher 0 is hungry
Philosopher 1 is hungry
Philosopher 1 is ready to eat
Philosopher 2 is hungry
Philosopher 4 is done eating
Philosopher 4 is thinking
Philosopher 1 is done eating
Philosopher 1 is thinking
Philosopher 2 is ready to eat
Philosopher 0 is ready to eat
Philosopher 2 is done eating
Philosopher 2 is thinking
Philosopher 2 is hungry
Philosopher 2 is ready to eat
Philosopher 3 is hungry
Philosopher 0 is done eating
Philosopher 0 is thinking
Philosopher 1 is hungry
Philosopher 4 is hungry
Philosopher 4 is ready to eat
Philosopher 0 is hungry
Philosopher 2 is done eating
Philosopher 2 is thinking
Philosopher 1 is ready to eat
Philosopher 4 is done eating
Philosopher 4 is thinking
Philosopher 3 is ready to eat
Philosopher 4 is hungry
Philosopher 2 is hungry
Philosopher 1 is done eating
Philosopher 1 is thinking
Philosopher 0 is ready to eat
Philosopher 1 is hungry
Philosopher 3 is done eating
Philosopher 3 is thinking
Philosopher 2 is ready to eat
Philosopher 2 is done eating
Philosopher 2 is thinking
Philosopher 3 is hungry
Philosopher 0 is done eating
Philosopher 0 is thinking
Philosopher 4 is ready to eat
Philosopher 1 is ready to eat
Philosopher 4 is done eating
Philosopher 4 is thinking
Philosopher 3 is ready to eat
Philosopher 0 is hungry
Philosopher 1 is done eating
Philosopher 1 is thinking
Philosopher 0 is ready to eat
Philosopher 1 is hungry
Philosopher 2 is hungry
Philosopher 4 is hungry
Philosopher 3 is done eating
Philosopher 3 is thinking
Philosopher 2 is ready to eat
Philosopher 0 is done eating
Philosopher 0 is thinking
Philosopher 4 is ready to eat
Philosopher 4 is done eating
Philosopher 4 is thinking
Philosopher 2 is done eating
Philosopher 2 is thinking
Philosopher 1 is ready to eat
Philosopher 4 is hungry
Philosopher 4 picked up the left chopstick
Philosopher 4 picked up the right chopstick
Philosopher 4 is ready to eat
Philosopher 0 is hungry
Philosopher 0 picked up the left chopstick
Philosopher 3 is hungry
Philosopher 2 is hungry
Philosopher 4 is done eating
Philosopher 4 is thinking
Philosopher 3 is ready to eat
Philosopher 1 is done eating
Philosopher 1 is thinking
Philosopher 0 is ready to eat
Philosopher 3 is done eating
Philosopher 3 is thinking
Philosopher 2 is ready to eat
Philosopher 1 is hungry
Philosopher 0 is done eating
Philosopher 0 is thinking
Philosopher 4 is hungry
Philosopher 4 is ready to eat
Philosopher 2 is done eating
Philosopher 2 is thinking
Philosopher 1 is ready to eat
Philosopher 0 is hungry
Philosopher 2 is hungry
Philosopher 2 picked up the left chopstick
Philosopher 3 is hungry
Philosopher 4 is done eating
Philosopher 4 released up the left chopstick
Philosopher 4 released up the right chopstick
Philosopher 4 is thinking
Philosopher 1 picked up the left chopstick
Philosopher 3 is ready to eat
Philosopher 4 is hungry
Philosopher 1 is done eating
Philosopher 1 is thinking
Philosopher 0 is ready to eat
Philosopher 0 is done eating
Philosopher 0 is thinking
Philosopher 3 is done eating
Philosopher 3 is thinking
Philosopher 2 is ready to eat
Philosopher 4 is ready to eat
Philosopher 1 is hungry
Philosopher 3 is hungry
Philosopher 3 picked up the left chopstick
Philosopher 2 is done eating
Philosopher 2 is thinking
Philosopher 1 is ready to eat
Philosopher 1 is done eating
Philosopher 1 is thinking
Philosopher 2 is hungry
Philosopher 1 is hungry
Philosopher 0 is hungry
Philosopher 4 is done eating
Philosopher 4 is thinking
Philosopher 3 is ready to eat
Philosopher 4 is hungry
Philosopher 4 picked up the left chopstick
Philosopher 3 is done eating
Philosopher 3 is thinking
Philosopher 2 is ready to eat
Philosopher 2 is done eating
Philosopher 2 is thinking
Philosopher 1 is ready to eat
Philosopher 1 is done eating
Philosopher 1 is thinking
Philosopher 0 is ready to eat
Philosopher 3 is hungry
Philosopher 2 is hungry
Philosopher 1 is hungry
Philosopher 0 is done eating
Philosopher 0 is thinking
Philosopher 4 is ready to eat
/**
* Name: Son Pham
* Class: CSCI 315
* Prof: Luiz Felipe Perrone
* problem1.c
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <unistd.h>
#include <pthread.h>
#define NUM_THREADS 5
#define SCALE_FACTOR 1000000
/** Function prototype */
void* philosopher(void* param);
int napping(int t);
int initialize_mutexes();
int destroy_mutexes();
/** Mutexes */
pthread_mutex_t chopsticks[NUM_THREADS];
/**
* Main function
*/
int main() {
// Instantiate all philosopher threads
pthread_t tid[NUM_THREADS];
// Create 5 philosopher threads
int i;
for (i = 0; i < NUM_THREADS; i++) {
pthread_create(&tid[i], NULL, philosopher, (void*) (intptr_t) i);
}
// Sleep for then 10 seconds to make these threads run for a while
sleep(10);
// Return gracefully, will never get here tho
return 0;
}
/**
* The philosopher thread
*/
void* philosopher(void* param) {
// Cast the param back to ID
int id = (intptr_t) param;
// Since ID is different across philosopher, use ID as seed
unsigned int seed = id + 1;
// Temporary variable t
int t;
// Create a while loop that think, get hungry and eats
while(1) {
printf("Philosopher %d is thinking\n", id);
// sleep for random period of time
t = rand_r(&seed);
napping(t);
// Eating seciont
printf("Philosopher %d is hungry\n", id);
if ((id % 2) == 0) {
// If the ID is even, pick up the left chopstick first
pthread_mutex_lock(&chopsticks[id]);
printf("Philosopher %d picked up the left chopstick\n",id);
pthread_mutex_lock(&chopsticks[(id+1)/NUM_THREADS]);
printf("Philosopher %d picked up the right chopstick\n",id);
} else {
// If the ID is odd, pick up the right chopstick first
pthread_mutex_lock(&chopsticks[(id+1)/NUM_THREADS]);
printf("Philosopher %d picked up the right chopstick\n",id);
pthread_mutex_lock(&chopsticks[id]);
printf("Philosopher %d picked up the left chopstick\n",id);
}
// -- Critical section
printf("Philosopher %d is ready to eat\n", id);
// Eat for some amount of time
t = rand_r(&seed);
napping(t);
// Done eating
printf("Philosopher %d is done eating\n", id);
// -- End of critical section
// Releasing the chopsticls
pthread_mutex_unlock(&chopsticks[id]);
printf("Philosopher %d released up the left chopstick\n",id);
pthread_mutex_unlock(&chopsticks[(id+1)/NUM_THREADS]);
printf("Philosopher %d released up the right chopstick\n",id);
}
}
/**
* Sleep for some amount of time
*/
int napping(int t) {
t = t / (RAND_MAX / SCALE_FACTOR);
usleep(t);
}
/**
* Initialize all mutexes
*/
int initialize_mutexes() {
int i;
for (i = 0; i < NUM_THREADS; i++) {
pthread_mutex_init(&chopsticks[i], NULL);
}
return 0;
}
/**
* Destory all mutexes
*/
int destroy_mutexes() {
int i;
for (i = 0; i < NUM_THREADS; i++) {
pthread_mutex_destroy(&chopsticks[i]);
}
return 0;
};
/**
* Name: Son Pham
* Class: CSCI 315
* Prof: Luiz Felipe Perrone
* problem1.c
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <unistd.h>
#include <pthread.h>
#define NUM_THREADS 5
#define SCALE_FACTOR 1000000
/** Function prototype */
void* philosopher(void* param);
int napping(int t);
int initialize_mutexes();
int destroy_mutexes();
/** Mutexes */
pthread_mutex_t chopsticks[NUM_THREADS];
/**
* Main function
*/
int main() {
// Instantiate all philosopher threads
pthread_t tid[NUM_THREADS];
// Create 5 philosopher threads
int i;
for (i = 0; i < NUM_THREADS; i++) {
pthread_create(&tid[i], NULL, philosopher, (void*) (intptr_t) i);
}
// Sleep for then 10 seconds to make these threads run for a while
sleep(10);
// Return gracefully, will never get here tho
return 0;
}
/**
* The philosopher thread
*/
void* philosopher(void* param) {
// Cast the param back to ID
int id = (intptr_t) param;
// Since ID is different across philosopher, use ID as seed
unsigned int seed = id + 1;
// Temporary variable t
int t;
// Create a while loop that think, get hungry and eats
while(1) {
printf("Philosopher %d is thinking\n", id);
// sleep for random period of time
t = rand_r(&seed);
napping(t);
// Eating seciont
printf("Philosopher %d is hungry\n", id);
// Pick the chopstick with lower ID number of the two first
if (id < ((id+1)%NUM_THREADS)) {
pthread_mutex_lock(&chopsticks[id]);
printf("Philosopher %d picked up the left chopstick\n",id);
pthread_mutex_lock(&chopsticks[(id+1)%NUM_THREADS]);
printf("Philosopher %d picked up the right chopstick\n",id);
} else {
pthread_mutex_lock(&chopsticks[(id+1)%NUM_THREADS]);
printf("Philosopher %d picked up the right chopstick\n",id);
pthread_mutex_lock(&chopsticks[id]);
printf("Philosopher %d picked up the left chopstick\n",id);
}
// -- Critical section
printf("Philosopher %d is ready to eat\n", id);
// Eat for some amount of time
t = rand_r(&seed);
napping(t);
// Done eating
printf("Philosopher %d is done eating\n", id);
// -- End of critical section
// Releasing the chopsticls
pthread_mutex_unlock(&chopsticks[id]);
printf("Philosopher %d released up the left chopstick\n",id);
pthread_mutex_unlock(&chopsticks[(id+1)%NUM_THREADS]);
printf("Philosopher %d released up the right chopstick\n",id);
}
}
/**
* Sleep for some amount of time
*/
int napping(int t) {
t = t / (RAND_MAX / SCALE_FACTOR);
usleep(t);
}
/**
* Initialize all mutexes
*/
int initialize_mutexes() {
int i;
for (i = 0; i < NUM_THREADS; i++) {
pthread_mutex_init(&chopsticks[i], NULL);
}
return 0;
}
/**
* Destory all mutexes
*/
int destroy_mutexes() {
int i;
for (i = 0; i < NUM_THREADS; i++) {
pthread_mutex_destroy(&chopsticks[i]);
}
return 0;
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment