Commit b9873245 authored by cdf009's avatar cdf009

lab6

parent 58e92369
......@@ -13,4 +13,10 @@ The ways to achieve deadlock is:
2.1 The situation that lead me to dead lock is adding napping calls after each of the mutex locks! I also increased the napping time while eating. This created a deadlock where each philopsopher picked up a chopstick but did not begin to eat. Then if a philopsopher was actually allowed to start eating, the others had to wait so long for him to finish that there became a deadlock.
3.1
These solutions do not eliminate all potential causes of deadlock. When i run this code numerous times, I still see bouts of deadlock. I beleive this is due to the fact that the napping calls are frequent and the one when the eating happens is long. Therefore the philopshers are waiting so long to eat and therefore once the philopshers pick up a chopstick each, it runs in to deadlock.3.1
These solutions do not eliminate all potential causes of deadlock. When i run this code numerous times, I still see bouts of deadlock. I beleive this is due to the fact that the napping calls are frequent and the one when the eating happens is long. Therefore the philopshers are waiting so long to eat and therefore once the philopshers pick up a chopstick each, it runs in to deadlock.3.1
These solutions do not eliminate all potential causes of deadlock. When i run this code numerous times, I still see bouts of deadlock. I beleive this is due to the fact that the napping calls are frequent and the one when the eating happens is long. Therefore the philopshers are waiting so long to eat and therefore once the philopshers pick up a chopstick each, it runs in to deadlock.
/*
* Copyright (c) 2013 Bucknell University
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author: L. Felipe Perrone (perrone@bucknell.edu)
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <unistd.h>
#include <semaphore.h>
#include <pthread.h>
#include <string.h>
/* SCALE_FACTOR is a constant for you to experiment with:
* if you choose a very large SCALE_FACTOR, your threads
* might spend a long time sleeping. If you choose it to be
* too small, your threads will not sleep at all. Note
* that in the producer and consumer functions, the sleep
* time is computed as the INTEGER DIVISION below:
*
* usleep(SCALE_FACTOR * rand_r(&seed) / RAND_MAX
*
* where RAND_MAX is the largest random numver returned
* by rand_r. If the numerator is smaller than RAND_MAX,
* the quotient of the integer division is ZERO!
*/
#define SCALE_FACTOR 1000
// global variables -----------------------
static pthread_mutex_t chopsticks[5];
int phils[5] = {0, 1, 2, 3, 4};
// end of global variables ----------------
void napping(int x) {
unsigned int seed1 = pthread_self();
int count = rand_r(&seed1) % (x+1);
sleep(count);
}
void *philosopher (void *param) {
//item i;
long long param_long = (long long)param;
int i = (int)param_long;
while (1==1) {
// sleep for random period of time
// usleep(SCALE_FACTOR * (rand_r(seed)) / RAND_MAX);
printf("Philospher %d : thinking. \n", i);
napping(2);
printf("Philospher %d : hungry. \n", i);
if ( i % 2 != 0){
pthread_mutex_lock(&chopsticks[i]);
napping(2);
printf("Philospher %d : picking up chopstick. \n", i);
pthread_mutex_lock(&chopsticks[(i+1) % 5]);
napping(2);
}
if ( i % 2 == 0){
pthread_mutex_lock(&chopsticks[(i+1) % 5]);
napping(2);
printf("Philospher %d : picking up chopstick. \n", i);
pthread_mutex_lock(&chopsticks[i]);
napping(2);
}
printf("Philospher %d : picking up chopstick. \n", ((i+1) % 5));
printf("Philospher %d : starting to eat. \n", i);
napping(100);//make this so that the philopsopher is hogging choptsticks
printf("Philospher %d : done eating. \n", i);
pthread_mutex_unlock(&chopsticks[i]);
printf("Philospher %d : putting down chopstick. \n", i);
pthread_mutex_unlock(&chopsticks[(i+1) % 5]);
printf("Philospher %d : putting down chopstick. \n", ((i+1) % 5));
}
}
int main() {
// get command line arguments
//strcopy(producer, argv[1]);
//strcopy(consumer, argv[2]);
//strcopy(usleep, argv[3]);
int phils = 5;
int i;
pthread_t threads[phils];
long long tid;
for (i = 0; i < phils; i++){
tid = (long long)i;
pthread_create(&threads[i], NULL, philosopher, (void*)tid);
}
sleep(10);
return 0;
}
/*
* Copyright (c) 2013 Bucknell University
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author: L. Felipe Perrone (perrone@bucknell.edu)
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <unistd.h>
#include <semaphore.h>
#include <pthread.h>
#include <string.h>
/* SCALE_FACTOR is a constant for you to experiment with:
* if you choose a very large SCALE_FACTOR, your threads
* might spend a long time sleeping. If you choose it to be
* too small, your threads will not sleep at all. Note
* that in the producer and consumer functions, the sleep
* time is computed as the INTEGER DIVISION below:
*
* usleep(SCALE_FACTOR * rand_r(&seed) / RAND_MAX
*
* where RAND_MAX is the largest random numver returned
* by rand_r. If the numerator is smaller than RAND_MAX,
* the quotient of the integer division is ZERO!
*/
#define SCALE_FACTOR 1000
// global variables -----------------------
static pthread_mutex_t chopsticks[5];
int phils[5] = {0, 1, 2, 3, 4};
// end of global variables ----------------
void napping(int x) {
unsigned int seed1 = pthread_self();
int count = rand_r(&seed1) % (x+1);
sleep(count);
}
void *philosopher (void *param) {
//item i;
long long param_long = (long long)param;
int i = (int)param_long;
while (1==1) {
// sleep for random period of time
// usleep(SCALE_FACTOR * (rand_r(seed)) / RAND_MAX);
printf("Philospher %d : thinking. \n", i);
napping(2);
printf("Philospher %d : hungry. \n", i);
if ( i > ((i+1) % 5)){
pthread_mutex_lock(&chopsticks[i]);
napping(2);
printf("Philospher %d : picking up chopstick. \n", i);
pthread_mutex_lock(&chopsticks[(i+1) % 5]);
napping(2);
}
else{
pthread_mutex_lock(&chopsticks[(i+1) % 5]);
napping(2);
printf("Philospher %d : picking up chopstick. \n", i);
pthread_mutex_lock(&chopsticks[i]);
napping(2);
}
printf("Philospher %d : picking up chopstick. \n", ((i+1) % 5));
printf("Philospher %d : starting to eat. \n", i);
napping(100);//make this so that the philopsopher is hogging choptsticks
printf("Philospher %d : done eating. \n", i);
pthread_mutex_unlock(&chopsticks[i]);
printf("Philospher %d : putting down chopstick. \n", i);
pthread_mutex_unlock(&chopsticks[(i+1) % 5]);
printf("Philospher %d : putting down chopstick. \n", ((i+1) % 5));
}
}
int main() {
// get command line arguments
//strcopy(producer, argv[1]);
//strcopy(consumer, argv[2]);
//strcopy(usleep, argv[3]);
int phils = 5;
int i;
pthread_t threads[phils];
long long tid;
for (i = 0; i < phils; i++){
tid = (long long)i;
pthread_create(&threads[i], NULL, philosopher, (void*)tid);
}
sleep(10);
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