diff options
author | toni <toni@devlap.local> | 2015-11-30 09:56:57 +0100 |
---|---|---|
committer | toni <toni@devlap.local> | 2015-11-30 11:08:11 +0100 |
commit | 7197dcc620d71f052d4114d7419132b8c0204178 (patch) | |
tree | 5de247a408a42d2a5c5e57af3e60a16ac7947480 /parallel/cig_smoker_problem.c | |
parent | 0c7a4259f5d34a332886c9a62ed223784242ade7 (diff) |
added multithreading sync/memory examples
Diffstat (limited to 'parallel/cig_smoker_problem.c')
-rw-r--r-- | parallel/cig_smoker_problem.c | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/parallel/cig_smoker_problem.c b/parallel/cig_smoker_problem.c new file mode 100644 index 0000000..9d0928b --- /dev/null +++ b/parallel/cig_smoker_problem.c @@ -0,0 +1,123 @@ +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <string.h> +#include <pthread.h> +#include <semaphore.h> + +#include <assert.h> + + +enum thrd_ids { + AGENT_A = 0, + AGENT_B, + AGENT_C, + SMOKER_T, + SMOKER_P, + SMOKER_M, + THRD_MAX +}; + +static pthread_t thrds[THRD_MAX]; + +enum sem_ids { + AGENTSEM = 0, + TOBACCO, + PAPER, + MATCH, + SEM_MAX +}; + +static sem_t sems[SEM_MAX]; + + +static void *agenta_thread(void *args) +{ + while (1) { + sem_wait(&sems[AGENTSEM]); + sem_post(&sems[TOBACCO]); + sem_post(&sems[PAPER]); + printf("AGENT_A: +1 TOBACCO , +1 PAPER\n"); + } + return NULL; +} + +static void *agentb_thread(void *args) +{ + while (1) { + sem_wait(&sems[AGENTSEM]); + sem_post(&sems[PAPER]); + sem_post(&sems[MATCH]); + printf("AGENT_B: +1 PAPER, +1 MATCH\n"); + } + return NULL; +} + +static void *agentc_thread(void *args) +{ + while (1) { + sem_wait(&sems[AGENTSEM]); + sem_post(&sems[TOBACCO]); + sem_post(&sems[MATCH]); + printf("AGENT_C: +1 TOBACCO, +1 MATCH\n"); + } + return NULL; +} + +static void *smokerm_thread(void *args) +{ + while (1) { + sem_wait(&sems[TOBACCO]); + sem_wait(&sems[PAPER]); + sem_post(&sems[AGENTSEM]); + printf("SMOKER_M: +1 PAPER , \n"); + } + return NULL; +} + +static void *smokert_thread(void *args) +{ + while (1) { + sem_wait(&sems[PAPER]); + sem_wait(&sems[MATCH]); + sem_post(&sems[AGENTSEM]); + printf("SMOKER_T\n"); + } + return NULL; +} + +static void *smokerp_thread(void *args) +{ + while (1) { + sem_wait(&sems[TOBACCO]); + sem_wait(&sems[MATCH]); + sem_post(&sems[AGENTSEM]); + printf("SMOKER_P\n"); + } + return NULL; +} + +int +main(int argc, char *argv[]) +{ + int i; + + for (i = 0; i < SEM_MAX; i++) { + assert( sem_init(&sems[i], 0, 0) == 0 ); + } + + assert( pthread_create(&thrds[AGENT_A], NULL, agenta_thread, NULL) == 0 ); + assert( pthread_create(&thrds[AGENT_B], NULL, agentb_thread, NULL) == 0 ); + assert( pthread_create(&thrds[AGENT_C], NULL, agentc_thread, NULL) == 0 ); + assert( pthread_create(&thrds[SMOKER_M], NULL, smokerm_thread, NULL) == 0 ); + assert( pthread_create(&thrds[SMOKER_T], NULL, smokert_thread, NULL) == 0 ); + assert( pthread_create(&thrds[SMOKER_P], NULL, smokerp_thread, NULL) == 0 ); + + while (1) { + sleep( random() % 5 ); + sem_post(&sems[AGENTSEM]); + } + return 0; +} + + |