Last active
May 29, 2016 15:06
-
-
Save ahmedengu/478d36829388799154be2f0f722e476c to your computer and use it in GitHub Desktop.
2 producer 1 consumer // it cause dead lock
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; | |
char queue[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"); | |
} | |
} | |
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("\nuppercase char: %s", v); | |
sem_post(&full_sem); | |
} | |
} | |
int main() { | |
pthread_t t[3]; | |
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_join(t[0], NULL); | |
pthread_join(t[1], NULL); | |
pthread_join(t[2], 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
P1:: insert random text #0 | |
P2:: insert random text #0, inserted text: owkkyhiddqscdx▒, inserted text: nwlrbbmqbhcdar▒▒ | |
P2:: insert random text #1, inserted text: jmowfrxsjybldb▒▒ | |
P2:: insert random text #2, inserted text: fsarcbynecdygg▒▒ | |
P2:: insert random text #3 | |
P1:: insert random text #1, inserted text: fwkhopkmcoqhnw▒▒ | |
P1:: insert random text #2, inserted text: xpklorellnmpap▒▒ | |
P2:: insert random text #4, inserted text: jjivswmdkqtbxi▒▒ | |
P2:: insert random text #5, inserted text: mvtrrbljptnsnf▒▒ | |
P2:: insert random text #6, inserted text: kuewhsqmgbbuqc▒▒ | |
uppercase char: | |
uppercase char: KUEWHSQMGBBUQCL | |
uppercase char: MVTRRBLJPTNSNFW | |
uppercase char: JJIVSWMDKQTBXIX | |
uppercase char: | |
uppercase char: XPKLORELLNMPAPQ | |
uppercase char: FSARCBYNECDYGGX | |
uppercase char: JMOWFRXSJYBLDBE | |
uppercase char: NWLRBBMQBHCDARZ, inserted text: zqfjmafadrrwso▒▒ | |
P1:: insert random text #3, inserted text: sbcnuvqhffbsaq▒▒ | |
P1:: insert random text #4, inserted text: wpqcacehchzvfr▒▒ | |
P2:: insert random text #7, inserted text: mlnozjkpqpxrjx▒▒ | |
P2:: insert random text #8, inserted text: itzyxacbhhkicq▒▒ | |
P2:: insert random text #9 | |
P1:: insert random text #5, inserted text: gpxiqvkuytdlcg▒▒ | |
P1:: insert random text #6, inserted text: ewhtaciohordtq▒▒ | |
P1:: insert random text #7, inserted text: vwcsgspqoqmsbo▒▒ | |
P1:: insert random text #8, inserted text: guwnnyqxnzlgdg▒▒ | |
P1:: insert random text #9, inserted text: pbtrwblnsadeug▒▒ | |
P1:: insert random text #10, inserted text: umoqcdrubetoky▒▒ | |
P1:: insert random text #11, inserted text: hoachwdvmxxrdr▒▒ | |
P1:: insert random text #12, inserted text: xlmndqtukwagml▒▒ | |
, inserted text: oendtomfgdwdwf▒▒ | |
uppercase char: ZQFJMAFADRRWSOF | |
uppercase char: OENDTOMFGDWDWFC | |
P1:: insert random text #13, inserted text: juukwcibxubume▒▒ | |
uppercase char: HOACHWDVMXXRDRY | |
uppercase char: JUUKWCIBXUBUMEN | |
uppercase char: UMOQCDRUBETOKYX | |
uppercase char: PBTRWBLNSADEUGU | |
P1:: insert random text #14, inserted text: meyatdrmydiajx▒▒ | |
P2:: insert random text #10, inserted text: oghiqfmzhlvihj▒▒ | |
P2:: insert random text #11, inserted text: uvsuyoypayulye▒▒ | |
P2:: insert random text #12, inserted text: muotehzriicfsk▒▒ | |
///// deadlock |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment