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

Fin de T2, et début t3.

parent 66de83c6
Branches
No related merge requests found
# Task 3
The problem is that each thread is unlocking a mutex it didn't unlock.
\ No newline at end of file
#include <pthread.h>
#include <stdio.h>
#include "dp.h"
int left_neighbor(int number)
......@@ -23,7 +25,7 @@ void test(int i)
{
state[i] = EATING;
pthread_cond_signal(cond_vars + i);
// TODO unblock threads from condition variable
}
}
......@@ -38,7 +40,7 @@ void pickup_forks(int number)
{
sleep(1);
pthread_cond_wait(cond_vars + number, &mutex_lock);
// TODO wait on condition variable and mutex
}
pthread_mutex_unlock(&mutex_lock);
......
/*
Note on the task 2:
The code given didn't work because of a design flaw, so we replaced dining.c and philosopher.c by a single dp.c file.
We also moved some functions and variables from one file to another, and modified the Makefile according to these changes.
*/
#include "dp.h"
extern pthread_t tid[NUMBER];
......
#include <pthread.h>
#include <stdio.h>
#include <time.h>
#include "dp.h"
// structure of a dining philosopher alternating between thinking and eating
......
ex3/t3.c 0 → 100644
#include <pthread.h>
pthread_mutex_t first_mutex;
pthread_mutex_t second_mutex;
// thread one runs in this function
void *do_work_one(void *param)
{
int done = 0;
while (!done)
{
pthread_mutex_lock(&first_mutex);
if (pthread_mutex_trylock(&second_mutex) == 0)
{
// do some work
pthread_mutex_unlock(&second_mutex);
done = 1;
}
pthread_mutex_unlock(&first_mutex);
}
pthread_exit(0);
}
// thread two runs in this function
void *do_work_two(void *param)
{
int done = 0;
while (!done)
{
pthread_mutex_lock(&second_mutex);
if (pthread_mutex_trylock(&first_mutex) == 0)
{
// do some work
pthread_mutex_unlock(&first_mutex);
done = 1;
}
pthread_mutex_unlock(&second_mutex);
}
pthread_exit(0);
}
int main() {
pthread_mutex_init(&first_mutex, NULL);
pthread_mutex_init(&second_mutex, NULL);
pthread_t tid1, tid2;
pthread_create(&tid1, NULL, do_work_one, NULL);
pthread_create(&tid2, NULL, do_work_two, NULL);
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
return 0;
}
\ No newline at end of file
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