Commit 7792d793 authored by Son Pham's avatar Son Pham

Lab7, Problem 1 Completed

parent a3c98986
CC = gcc
CFLAGS = -g
LFLAGS = -lpthread
ALL = dp
ALL = dp problem1
all : $(ALL)
dp: dp.c
$(CC) $(CFLAGS) $(LFLAGS) dp.c -o dp
problem1: problem1.c
$(CC) $(CFLAGS) $(LFLAGS) problem1.c -o problem1
clean:
/bin/rm -f *.o *~ core $(ALL)
\ No newline at end of file
# Name: Son Pham
# Class: CSCI 315
# Prof: Luiz Felipe Perrone
# answers.txt
[1.1] About three seconds into the execution, I see five left chopsticks picked up by the philosophers and the program entered deadlock.
[1.2] Deadlock could happen because current execution of the program satisfy all conditions for deadlock:
- Mutual Exclusion: one chopstick can't be picked up by two philosophers at the same time
- Hold and wait: The philosopher can hold the left chopstick and request the right chopstick which can be hold by another philosopher
- No premption: The philosopher won't release his favorite left chopstick until he get the right chopstick and enter critical section.
- Circular wait: Philosopher tries to request the right chopsticks in circle.
There were no avoidance mechanism, so deadlock occured.
/**
* 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);
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);
// -- 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;
};
Philosopher 1 is thinking
Philosopher 4 is thinking
Philosopher 0 is thinking
Philosopher 2 is thinking
Philosopher 3 is thinking
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 1 is hungry
Philosopher 2 is hungry
Philosopher 2 picked up the left chopstick
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 hungry
Philosopher 3 picked up the left chopstick
Philosopher 4 is hungry
Philosopher 4 picked up the left chopstick
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