Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <pthread.h>
- #define BUFFSIZE 5
- #define GEN_CAPS 0.3
- #define GEN_SMALL 0.3
- #define GEN_DIGIT 0.3
- char bufor[BUFFSIZE];
- int bStart, bEnd, bCnt = 0;
- pthread_cond_t buffWR;
- pthread_cond_t buffRR;
- pthread_cond_t buffPR;
- pthread_mutex_t mutex;
- void* generator(void* arg)
- {
- float rnd;
- char new;
- printf("Generator startuje\n");
- while(1)
- {
- sleep(0.5);
- rnd = 1.0 * rand()/RAND_MAX;
- if(rnd <= GEN_CAPS){
- new = rand() % ('Z' - 'A') + 'A';
- } else if(rnd <= GEN_SMALL + GEN_CAPS) {
- new = rand() % ('z' - 'a') + 'a';
- } else if(rnd <= GEN_SMALL + GEN_CAPS + GEN_DIGIT){
- new = rand() % ('0' - '9') + '0';
- } else {
- sleep(2);
- continue;
- }
- pthread_mutex_lock(&mutex);
- while(bCnt >= BUFFSIZE) {
- pthread_cond_wait(&buffWR, &mutex);
- }
- bufor[bEnd] = new;
- printf("Wstawilem w %d : %c\n", bEnd, new);
- bEnd = (bEnd + 1) % BUFFSIZE;
- bCnt++;
- pthread_cond_signal(&buffPR);
- pthread_cond_broadcast(&buffRR);
- pthread_mutex_unlock(&mutex);
- }
- printf("Generator konczy\n");
- return NULL;
- }
- void* consumerSmall(void * arg){
- char get;
- while(1){
- sleep(2);
- pthread_mutex_lock(&mutex);
- while(bCnt <= 0) { pthread_cond_wait(&buffRR, &mutex); }
- get = bufor[bStart];
- if(get>='a' && get <= 'z'){
- printf("CS Usunalem z %d: %c \n", bStart, get);
- bStart = (bStart+1) % BUFFSIZE;
- bCnt--;
- pthread_cond_signal(&buffPR);
- pthread_cond_signal(&buffWR);
- }
- pthread_mutex_unlock(&mutex);
- }
- }
- void* consumerBig(void * arg){
- char get;
- while(1){
- sleep(2);
- pthread_mutex_lock(&mutex);
- while(bCnt <= 0) { pthread_cond_wait(&buffRR, &mutex); }
- get = bufor[bStart];
- if(get>='A' && get <= 'Z'){
- printf("CB Usunalem z %d: %c \n", bStart, get);
- bStart = (bStart+1) % BUFFSIZE;
- bCnt--;
- pthread_cond_signal(&buffPR);
- pthread_cond_signal(&buffWR);
- }
- pthread_mutex_unlock(&mutex);
- }
- }
- void* consumerNum(void * arg){
- char get;
- while(1){
- sleep(2);
- pthread_mutex_lock(&mutex);
- while(bCnt <= 0) { pthread_cond_wait(&buffRR, &mutex); }
- get = bufor[bStart];
- if(get>='0' && get <= '9'){
- printf("CN Usunalem z %d: %c \n", bStart, get);
- bStart = (bStart+1) % BUFFSIZE;
- bCnt--;
- pthread_cond_signal(&buffPR);
- pthread_cond_signal(&buffWR);
- }
- pthread_mutex_unlock(&mutex);
- }
- }
- void * printBuff(void* arg){
- int i;
- while(1) {
- pthread_mutex_lock(&mutex);
- pthread_cond_wait(&buffPR, &mutex);
- printf("Bufor teraz (od %d, dl: %d: [", bStart, bCnt);
- if(bCnt >0)
- for(i=0;i<bCnt;i++){
- printf(" %c", bufor[(bStart + i) % BUFFSIZE]);
- }
- printf(" ]\n");
- pthread_mutex_unlock(&mutex);
- }
- }
- int main(int argc, char *argv[]) {
- pthread_t event;
- pthread_mutex_init(&mutex, NULL);
- pthread_cond_init(&buffWR, NULL);
- pthread_cond_init(&buffRR, NULL);
- pthread_cond_init(&buffPR, NULL);
- printf("GĹ‚Ăłwny wÄ…tek startuje\n");
- pthread_create(&event, NULL, &printBuff, NULL);
- pthread_create(&event, NULL, &generator, NULL);
- pthread_create(&event, NULL, &consumerSmall, NULL);
- pthread_create(&event, NULL, &consumerBig, NULL);
- pthread_create(&event, NULL, &consumerNum, NULL);
- pthread_join(event, NULL);
- printf("Główny wątek kończy\n");
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement