Commit 58e92369 authored by cdf009's avatar cdf009

lab6

parent 9d52ea94
...@@ -10,3 +10,7 @@ The ways to achieve deadlock is: ...@@ -10,3 +10,7 @@ The ways to achieve deadlock is:
2) is to ensure that a processes is waiting on another rescourse so here that means that one philopsher is waiting on another pphilopsher to be able to use the chopstick and eat 2) is to ensure that a processes is waiting on another rescourse so here that means that one philopsher is waiting on another pphilopsher to be able to use the chopstick and eat
3) is to ensure that the processes cannot be interupted so here this means that a philopsher cannot stop another philopsher and take his chopstick 3) is to ensure that the processes cannot be interupted so here this means that a philopsher cannot stop another philopsher and take his chopstick
4) the must be multiples processes so here that means there are numerous philopshers which is true. 4) the must be multiples processes so here that means there are numerous philopshers which is true.
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.
/*
* 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);
pthread_mutex_lock(&chopsticks[i]);
napping(2);
printf("Philospher %d : picking up chopstick. \n", i);
pthread_mutex_lock(&chopsticks[(i+1) % 5]);
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;
}
Philospher 0 : thinking.
Philospher 1 : thinking.
Philospher 2 : thinking.
Philospher 3 : thinking.
Philospher 4 : thinking.
Philospher 1 : hungry.
Philospher 0 : hungry.
Philospher 4 : hungry.
Philospher 2 : hungry.
Philospher 3 : hungry.
Philospher 1 : picking up chopstick.
Philospher 0 : picking up chopstick.
Philospher 4 : picking up chopstick.
Philospher 3 : picking up chopstick.
Philospher 2 : picking up 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