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

T2 + fin T1.

parent d8757b37
No related merge requests found
t1
t2
t3
\ No newline at end of file
t3
*.o
diningphilosophers
\ No newline at end of file
......@@ -90,7 +90,7 @@ Producer produced: 697887366, buffer position: 1
Consumer consumed: 697887366, buffer position: 1
Producer produced: 1716540840, buffer position: 2
Consumer consumed: 1716540840, buffer position: 2
Consumer consumed: 2009249934, buffer position: 3 -> wut ??
Consumer consumed: 2009249934, buffer position: 3 -> Here consumer displays its message before producer, but the call to "remove_item" happens indeed after the call to "insert_item"
Producer produced: 2009249934, buffer position: 3
Producer produced: 1043650057, buffer position: 4
Consumer consumed: 1043650057, buffer position: 4
......@@ -101,8 +101,8 @@ Consumer consumed: 1238749214, buffer position: 1
Producer produced: 994503885, buffer position: 2
```
Now we have 2 consumers and 2 producers.
Now we have 2 consumers and 2 producers. A call to "remove_item" from the consumer happens only if a producer called "insert_item" at least once before.
The process of the producer-consumer model is the following:
- The producer produces data into a buffer continuously until the buffer gets full, and then waits until it's not full anymore.
- The consumer consumes data from the above mentioned buffer only when the buffer isn't empty. Otherwise, it waits until the buffer isn't empty anymore.
\ No newline at end of file
- Producers continuously produce data that they place into a buffer until the buffer gets full (bounded-buffer), and then wait until it's not full anymore (data is consumed by a consumer by being removed from the buffer) to resume production.
- Consumers consume data from the above mentioned buffer only when the buffer isn't empty. In case the buffer is empty, consumers wait until it is not empty anymore (data is produced by a producer and placed into the buffer) to resume consuming.
\ No newline at end of file
#include <complex.h>
#include <pthread.h>
#include <stdio.h>
#include "dp.h"
......@@ -25,7 +26,7 @@ void test(int i)
{
state[i] = EATING;
// TODO unblock threads from condition variable
pthread_cond_signal(cond_vars + i);
}
}
......@@ -40,7 +41,7 @@ void pickup_forks(int number)
{
sleep(1);
// TODO wait on condition variable and mutex
pthread_cond_wait(cond_vars + number, &mutex_lock);
}
pthread_mutex_unlock(&mutex_lock);
......
......@@ -12,7 +12,7 @@ void init()
state[i] = THINKING;
thread_id[i] = i;
// TODO initialize condition variable
pthread_cond_init(cond_vars + i, NULL);
}
// initialize mutex lock
......
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