Commit 9d52ea94 authored by cdf009's avatar cdf009

Lab6

parent 7e941389
CC = gcc
CFLAGS = -std=gnu99 -Wall -g #-DDEBUG
EXECS = dp
all: $(EXECS)
dp: dp.c
$(CC) $(FLAGS) dp.c -o dp -pthread
problem1: problem1.c
$(CC) $(FLAGS) problem1.c -o problem1 -pthread
.PHONY: clean
clean:
/bin/rm -rf $(EXECS)
1.1
After running my code for about 10 seconds, I do not see any issues. The things that I see are philopshers doing different actions like napping, eating and finsihing eating. I also do see the same philopsher doing these actions for a few times in a row. This follows the idea tho that no 2 philosophers should be sitting next to each other and eating at the same time.
1.2
The ways to achieve deadlock is:
1) is to ensure that the processes must be unsharable so here that means that 2 philosphers will not be able to use one chopstick at one time
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
4) the must be multiples processes so here that means there are numerous philopshers which is true.
/*
* 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]);
//pthread_mutex_lock(chopsticks[(i+1) % 5]);
printf("Philospher %d : starting to eat. \n", i);
napping(1);
printf("Philospher %d : done eating. \n", i);
//pthread_mutex_unlock(choptsticks[i]);
//pthread_mutex_unlock(chopsticks[(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);
pthread_mutex_lock(&chopsticks[i]);
pthread_mutex_lock(&chopsticks[(i+1) % 5]);
printf("Philospher %d : starting to eat. \n", i);
napping(1);
printf("Philospher %d : done eating. \n", i);
pthread_mutex_unlock(&chopsticks[i]);
pthread_mutex_unlock(&chopsticks[(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