aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLuis R. Rodriguez <mcgrof@kernel.org>2016-05-05 16:48:10 -0700
committerLuis R. Rodriguez <mcgrof@kernel.org>2016-05-06 16:01:54 -0700
commit99381126182f95b53f2ddce0943da447c09088e8 (patch)
tree2dcc7c8ed4833c97bf537cb677bc3213a79b4362
parent61ec5f5d6ec8701237ba64065f1754395fc27c86 (diff)
downloadcocci-tact-99381126182f95b53f2ddce0943da447c09088e8.tar.gz
change rules
Life is complex: 0. We invite more people. 1. People invited to the party can arrive different times. In our case its up to some random time and the scheduler. 2. People invited are shy, they will only eat if people that are confirmed to be there are also eating. If we don't eat we have no trash to throw away. 3. People take random amount of time to eat food. 4. People invited are weak minded, if someone did not throw away their trash they follow and become messy and also do not throw away their own trash. Now results can vary, here's some examples, but pretty much it should be clear how buggy this is. Run 1: Before party: FOOD: [ ******************************* ] TRASH: [ ] After party: FOOD: [ * ***************************** ] TRASH: [ ******************************* ] Run 2: Before party: FOOD: [ ******************************* ] TRASH: [ ] After party: FOOD: [ ****************************** ] TRASH: [ ******************************* ] Signed-off-by: Luis R. Rodriguez <mcgrof@kernel.org>
-rw-r--r--main.c47
1 files changed, 39 insertions, 8 deletions
diff --git a/main.c b/main.c
index 2965ec7..f977ce8 100644
--- a/main.c
+++ b/main.c
@@ -5,12 +5,19 @@
#include <unistd.h>
#include <stdbool.h>
#include <string.h>
-#define NUM_THREADS 11
+#define NUM_THREADS 31
+
+enum thead_id_status {
+ THREAD_AT_HOME = 0,
+ THREAD_AT_PARTY = 1,
+ THREAD_DONE = 2,
+};
struct bin {
bool ready;
int food[NUM_THREADS+1];
int trash[NUM_THREADS+1];
+ int tid_status[NUM_THREADS+1];
pthread_mutex_t mutex;
};
@@ -21,19 +28,34 @@ struct party_ticket {
struct bin *party;
+static void sleep_random(void)
+{
+ unsigned int val;
+
+ val = 10000LL * rand() / RAND_MAX;
+ srand(getpid());
+ usleep(val);
+}
+
void eat(long tid)
{
bool eat_in_order = true;
long t;
/* If anyone ate out of place lets take notice of that... */
- for (t=0; t < tid; t++) {
- if (party->food[t])
+ for (t=0; t < NUM_THREADS; t++) {
+ if (t == tid)
+ continue;
+ if (party->tid_status[t] >= THREAD_AT_PARTY &&
+ party->food[t])
eat_in_order = false;
}
+ sleep_random();
+
if (party->food[tid])
- party->food[tid] = 0;
+ if (eat_in_order)
+ party->food[tid] = 0;
}
void cleanup(long tid)
@@ -41,25 +63,33 @@ void cleanup(long tid)
bool clean_follow = true;
long t;
- for (t=0; t < tid; t++) {
- if (!party->trash[t])
+ for (t=0; t < NUM_THREADS; t++) {
+ if (t == tid)
+ continue;
+ if (party->tid_status[t] >= THREAD_DONE &&
+ !party->trash[t])
clean_follow = false;
}
- party->trash[tid] = 1;
+ if (clean_follow)
+ party->trash[tid] = 1;
}
void *thread_party(void *t)
{
long tid = (long)t;
+ party->tid_status[tid] = THREAD_AT_PARTY;
+
eat(tid);
if (tid % 2 == 1)
- sleep(1);
+ sleep_random();
cleanup(tid);
+ party->tid_status[tid] = THREAD_DONE;
+
pthread_exit(NULL);
}
@@ -102,6 +132,7 @@ int main(int argc, char *argv[])
for (t=0; t <= NUM_THREADS; t++) {
party->food[t] = 1;
party->trash[t] = 0;
+ party->tid_status[t] = THREAD_AT_HOME;
}
party->ready = true;