Skip to content
Snippets Groups Projects
Commit 20e546fa authored by antux18's avatar antux18
Browse files

Ajout des fichiers du TP3.

parent 7ef4cb42
No related merge requests found
# To run, enter: make all
all: diningphilosophers
diningphilosophers: main.o dining.o philosopher.o
gcc -o diningphilosophers main.o dining.o philosopher.o -lpthread
main.o: main.c dp.h
gcc -lpthread -c main.c -lpthread
dining.o: dining.c dp.h
gcc -lpthread -c dining.c -lpthread
philosopher.o: philosopher.c dp.h
gcc -lpthread -c philosopher.c -lpthread
ex3/T1.c 0 → 100644
/**
* bounded-buffer problem using the producer-consumer model
* meant to be solved with counting semaphores and mutex locks
*
* to compile: gcc -o T1 T1.c -lpthread
* to run: ./T1 <duration> <producer threads> <consumer threads>
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#define BUFFER_SIZE 5
#define TRUE 1
typedef int buffer_item;
int insertPointer = 0;
int removePointer = 0;
buffer_item buffer[BUFFER_SIZE];
pthread_mutex_t mutex;
sem_t empty;
sem_t full;
int insert_item(buffer_item item);
int remove_item(buffer_item *item);
void *producer(void *param);
void *consumer(void *param);
int insert_item(buffer_item item)
{
// TODO acquire "empty" semaphore and mutex lock
buffer[insertPointer++] = item;
insertPointer = insertPointer % BUFFER_SIZE;
// TODO release mutex lock and "full" semaphore
return 0;
}
int remove_item(buffer_item *item)
{
// TODO acquire "full" semaphore and mutex lock
*item = buffer[removePointer];
buffer[removePointer++] = -1;
removePointer = removePointer % BUFFER_SIZE;
// TODO release mutex lock and "empty" semaphore
return 0;
}
int main(int argc, char *argv[])
{
int duration, producerThreads, consumerThreads;
int i, j;
if (argc != 4)
{
fprintf(stderr, "Useage: <duration> <producer threads> <consumer threads>\n");
return -1;
}
duration = atoi(argv[1]);
producerThreads = atoi(argv[2]);
consumerThreads = atoi(argv[3]);
// TODO initialize the synchronization tools
srand(time(0));
// create the producer and consumer threads
for (i = 0; i < producerThreads; i++)
{
pthread_t tid;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_create(&tid, &attr, producer, NULL);
}
for (j = 0; j < consumerThreads; j++)
{
pthread_t tid;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_create(&tid, &attr, consumer, NULL);
}
// sleep for user specified time
sleep(duration);
return 0;
}
void *producer(void *param)
{
buffer_item random;
int r;
while (TRUE)
{
r = rand() % 5;
sleep(r);
random = rand();
if (insert_item(random))
fprintf(stderr, "Error");
printf("Producer produced: %d, buffer position: %d \n", random, (insertPointer + 4) % 5);
}
}
void *consumer(void *param)
{
buffer_item random;
int r;
while (TRUE)
{
r = rand() % 5;
sleep(r);
if (remove_item(&random))
fprintf(stderr, "Error Consuming");
else
printf("Consumer consumed: %d, buffer position: %d \n", random, (removePointer + 4) % 5);
}
}
#include <pthread.h>
#include <stdio.h>
#include "dp.h"
int left_neighbor(int number)
{
if (number == 0)
return NUMBER - 1;
else
return number - 1;
}
int right_neighbor(int number)
{
if (number == NUMBER - 1)
return 0;
else
return number + 1;
}
void test(int i)
{
// if philosopher is hungry, and left and right neighbor are not eating, then eat
if ((state[i] == HUNGRY) && (state[left_neighbor(i)] != EATING) && (state[right_neighbor(i)] != EATING))
{
state[i] = EATING;
// TODO unblock threads from condition variable
}
}
void pickup_forks(int number)
{
pthread_mutex_lock(&mutex_lock);
state[number] = HUNGRY;
test(number);
while (state[number] != EATING)
{
sleep(1);
// TODO wait on condition variable and mutex
}
pthread_mutex_unlock(&mutex_lock);
}
void return_forks(int number)
{
pthread_mutex_lock(&mutex_lock);
state[number] = THINKING;
test(left_neighbor(number));
test(right_neighbor(number));
pthread_mutex_unlock(&mutex_lock);
}
ex3/dp.h 0 → 100644
#include <pthread.h>
#define NUMBER 5
#define MAX_SLEEP_TIME 1
enum
{
THINKING,
HUNGRY,
EATING
} state[NUMBER];
int thread_id[NUMBER];
// condition variables and associated mutex lock
pthread_cond_t cond_vars[NUMBER];
pthread_mutex_t mutex_lock;
void *philosopher(void *param);
#include <pthread.h>
#include <stdio.h>
#include "dp.h"
pthread_t tid[NUMBER];
void init()
{
int i;
for (i = 0; i < NUMBER; i++)
{
state[i] = THINKING;
thread_id[i] = i;
// TODO initialize condition variable
}
// initialize mutex lock
pthread_mutex_init(&mutex_lock, NULL);
}
void create_philosophers()
{
int i;
for (i = 0; i < NUMBER; i++)
pthread_create(&tid[i], 0, philosopher, (void *)&thread_id[i]);
}
int main(void)
{
int i;
init();
create_philosophers();
for (i = 0; i < NUMBER; i++)
pthread_join(tid[i], NULL);
printf("DINNER IS OVER\n");
return 0;
}
File added
#include <pthread.h>
#include <stdio.h>
#include <time.h>
#include "dp.h"
// structure of a dining philosopher alternating between thinking and eating
void *philosopher(void *param)
{
int *lnumber = (int *)param;
int number = *lnumber;
int sleep_time;
int times_through_loop = 0;
srandom((unsigned)time(NULL));
while (times_through_loop < 2)
{
printf("Philosopher %d is thinking\n", number);
sleep((int)((random() % MAX_SLEEP_TIME) + 1));
pickup_forks(number);
printf("Philosopher %d is eating\n", number);
sleep((int)((random() % MAX_SLEEP_TIME) + 1));
return_forks(number);
++times_through_loop;
}
printf("Philosopher %d is done\n", number);
}
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