Last active
May 18, 2016 10:15
-
-
Save ahmedengu/c0cfc4d29e6d6079f241f6fcfbb25337 to your computer and use it in GitHub Desktop.
2 producer 2 consumer reading same values
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <pthread.h> | |
#include <semaphore.h> | |
#include <math.h> | |
#include <ctype.h> | |
#define NUM 15 | |
int number = NUM, buff = 0,buff2 = 0; | |
char queue[NUM][NUM],queue2[NUM][NUM]; | |
sem_t full_sem, empty_sem; | |
void insert(char e[]) { | |
if (buff + 1 < number) { | |
buff++; | |
printf(", inserted text: %s", e); | |
int i = 0; | |
for (i = 0; i < number; ++i) { | |
queue[buff][i] = e[i]; | |
} | |
} else { | |
printf("\nfull"); | |
} | |
if (buff2 + 1 < number) { | |
buff2++; | |
printf(", inserted text: %s", e); | |
int i = 0; | |
for (i = 0; i < number; ++i) { | |
queue2[buff2][i] = e[i]; | |
} | |
} else { | |
printf("\nfull"); | |
} | |
} | |
void *produce2() { | |
int i = 0; | |
for (i = 0; i < number; ++i) { | |
sem_wait(&full_sem); | |
printf("\nP2:: insert random text #%d", i); | |
char tmpChar[number]; | |
int j = 0; | |
for (j = 0; j < number; ++j) { | |
tmpChar[j] = (char) 'a' + (random() % 26); | |
} | |
insert(tmpChar); | |
sem_post(&empty_sem); | |
} | |
} | |
void *produce() { | |
int i = 0; | |
for (i = 0; i < number; ++i) { | |
sem_wait(&full_sem); | |
printf("\nP1:: insert random text #%d", i); | |
char tmpChar[number]; | |
int j = 0; | |
for (j = 0; j < number; ++j) { | |
tmpChar[j] = (char) 'a' + (random() % 26); | |
} | |
insert(tmpChar); | |
sem_post(&empty_sem); | |
} | |
} | |
void *consume(void *a) { | |
int i = 0; | |
for (i = 0; i < number; ++i) { | |
char v[number]; | |
sem_wait(&empty_sem); | |
if (buff > 0) { | |
int j = 0; | |
for (j = 0; j < number; ++j) { | |
v[j] = toupper(queue[buff][j]); | |
} | |
buff--; | |
} else { | |
printf("\nempty"); | |
} | |
printf("\nc1:: uppercase char: %s", v); | |
sem_post(&full_sem); | |
} | |
} | |
void *consume2(void *a) { | |
int i = 0; | |
for (i = 0; i < number; ++i) { | |
char v[number]; | |
sem_wait(&empty_sem); | |
if (buff2 > 0) { | |
int j = 0; | |
for (j = 0; j < number; ++j) { | |
v[j] = toupper(queue2[buff2][j]); | |
} | |
buff2--; | |
} else { | |
printf("\nempty"); | |
} | |
printf("\nc2:: uppercase char: %s", v); | |
sem_post(&full_sem); | |
} | |
} | |
int main() { | |
pthread_t t[4]; | |
sem_init(&full_sem, 0, number - 1); | |
sem_init(&empty_sem, 0, 0); | |
pthread_create(&t[0], NULL, &produce, NULL); | |
pthread_create(&t[1], NULL, &produce2, NULL); | |
pthread_create(&t[2], NULL, &consume, NULL); | |
pthread_create(&t[3], NULL, &consume2, NULL); | |
pthread_join(t[0], NULL); | |
pthread_join(t[1], NULL); | |
pthread_join(t[2], NULL); | |
pthread_join(t[3], NULL); | |
sem_destroy(&full_sem); | |
sem_destroy(&empty_sem); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
P2:: insert random text #0, inserted text: nwlrbbmqbhcdarz, inserted text: nwlrbbmqbhcdarz | |
P2:: insert random text #1, inserted text: owkkyhiddqscdxr, inserted text: owkkyhiddqscdxr | |
P2:: insert random text #2, inserted text: jmowfrxsjybldbe, inserted text: jmowfrxsjybldbe | |
P2:: insert random text #3, inserted text: fsarcbynecdyggx, inserted text: fsarcbynecdyggx | |
P2:: insert random text #4, inserted text: xpklorellnmpapq, inserted text: xpklorellnmpapq | |
P2:: insert random text #5, inserted text: fwkhopkmcoqhnwn, inserted text: fwkhopkmcoqhnwn | |
P2:: insert random text #6, inserted text: kuewhsqmgbbuqcl, inserted text: kuewhsqmgbbuqcl | |
P2:: insert random text #7, inserted text: jjivswmdkqtbxix, inserted text: jjivswmdkqtbxix | |
P2:: insert random text #8, inserted text: mvtrrbljptnsnfw, inserted text: mvtrrbljptnsnfw | |
P2:: insert random text #9, inserted text: zqfjmafadrrwsof, inserted text: zqfjmafadrrwsof | |
P2:: insert random text #10, inserted text: sbcnuvqhffbsaqx, inserted text: sbcnuvqhffbsaqx | |
P2:: insert random text #11, inserted text: wpqcacehchzvfrk, inserted text: wpqcacehchzvfrk | |
P2:: insert random text #12, inserted text: mlnozjkpqpxrjxk, inserted text: mlnozjkpqpxrjxk | |
P2:: insert random text #13, inserted text: itzyxacbhhkicqc, inserted text: itzyxacbhhkicqc | |
c2:: uppercase char: ITZYXACBHHKICQC | |
c2:: uppercase char: MLNOZJKPQPXRJXK | |
c2:: uppercase char: WPQCACEHCHZVFRK | |
c2:: uppercase char: SBCNUVQHFFBSAQX | |
c2:: uppercase char: ZQFJMAFADRRWSOF | |
c2:: uppercase char: MVTRRBLJPTNSNFW | |
c2:: uppercase char: JJIVSWMDKQTBXIX | |
c2:: uppercase char: KUEWHSQMGBBUQCL | |
c2:: uppercase char: FWKHOPKMCOQHNWN | |
c2:: uppercase char: XPKLORELLNMPAPQ | |
c2:: uppercase char: FSARCBYNECDYGGX | |
c2:: uppercase char: JMOWFRXSJYBLDBE | |
c2:: uppercase char: OWKKYHIDDQSCDXR | |
c2:: uppercase char: NWLRBBMQBHCDARZ | |
P2:: insert random text #14 | |
full, inserted text: oendtomfgdwdwfc | |
c2:: uppercase char: OENDTOMFGDWDWFC | |
P1:: insert random text #0 | |
full, inserted text: gpxiqvkuytdlcgd | |
P1:: insert random text #1 | |
full, inserted text: ewhtaciohordtqk | |
P1:: insert random text #2 | |
full, inserted text: vwcsgspqoqmsboa | |
P1:: insert random text #3 | |
full, inserted text: guwnnyqxnzlgdgw | |
P1:: insert random text #4 | |
full, inserted text: pbtrwblnsadeugu | |
P1:: insert random text #5 | |
full, inserted text: umoqcdrubetokyx | |
P1:: insert random text #6 | |
full, inserted text: hoachwdvmxxrdry | |
P1:: insert random text #7 | |
full, inserted text: xlmndqtukwagmle | |
P1:: insert random text #8 | |
full, inserted text: juukwcibxubumen | |
P1:: insert random text #9 | |
full, inserted text: meyatdrmydiajxl | |
P1:: insert random text #10 | |
full, inserted text: oghiqfmzhlvihjo | |
P1:: insert random text #11 | |
full, inserted text: uvsuyoypayulyei | |
P1:: insert random text #12 | |
full, inserted text: muotehzriicfskp | |
P1:: insert random text #13 | |
full, inserted text: ggkbbipzzrzucxa | |
c1:: uppercase char: ITZYXACBHHKICQC | |
c1:: uppercase char: MLNOZJKPQPXRJXK | |
c1:: uppercase char: WPQCACEHCHZVFRK | |
c1:: uppercase char: SBCNUVQHFFBSAQX | |
c1:: uppercase char: ZQFJMAFADRRWSOF | |
c1:: uppercase char: MVTRRBLJPTNSNFW | |
c1:: uppercase char: JJIVSWMDKQTBXIX | |
c1:: uppercase char: KUEWHSQMGBBUQCL | |
c1:: uppercase char: FWKHOPKMCOQHNWN | |
c1:: uppercase char: XPKLORELLNMPAPQ | |
c1:: uppercase char: FSARCBYNECDYGGX | |
c1:: uppercase char: JMOWFRXSJYBLDBE | |
c1:: uppercase char: OWKKYHIDDQSCDXR | |
c1:: uppercase char: NWLRBBMQBHCDARZ | |
P1:: insert random text #14, inserted text: mludfykgruowzgi | |
full | |
c1:: uppercase char: MLUDFYKGRUOWZGI |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment