C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 29th, 2012, 03:42 PM
Thilar Thilar is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 3 Thilar User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 46 m 43 sec
Reputation Power: 0
Thumbs down HELP! Multiple threads access to global variables

I'm trying to solve the producer consumer problem for multiple producers and consumers using a multi slot buffer.

I am using a mutex to ensure mutual exclusion when the producer and consumers are accessing the buffers, and a conditional variable to block when the buffer is full or empty.

The globals are buffer, which is an array of struct 'data' and buffercount, which is an int to keep track of the size of buffer.

However, I cannot access the variables and my threads stall. This can be seen because though in the producer thread, i create the data and get into the function writeData, which writes the data to the buffer, I cannot print the values of count or write to the buffer in that function, it just stops.

Below is my code:

Code:
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>


typedef struct{
int x,y,z;
}data;

volatile data buffer[20];
volatile int count;
pthread_mutex_t m;
pthread_cond_t qu_empty_cond = PTHREAD_COND_INITIALIZER;
pthread_cond_t qu_full_cond = PTHREAD_COND_INITIALIZER;



void produce();
void consume();
void createData(data*);
void writeBuffer(data);
void readBuffer(data*);
void waitRandom();
void shiftBuffer();


int main (){
pthread_t p, c;
count = 0;

if(pthread_mutex_init(&m, NULL)){
        printf("mutex creation failed\n");
        return 1;
}
else printf("mutex creation sucess\n");

if(pthread_create (&p,NULL,(void *) &produce,NULL)){
        printf("producer creation failed\n");
        return 1;
}

if (pthread_create (&c,NULL,(void *) &consume,NULL)){
        printf("consumer creation failed\n");
        return 1;
}

return 0;
}

void produce(void){
printf("producer creation success count\n");
while(1){
        data a;
        createData(&a);
        printf("PRODUCE: checking for mutual exclusion\n");
        pthread_mutex_lock(&m);
        printf("have mutual exclusion to produce\n");
        //if 20 block
        /*while(count>=20){
                printf("stuck \n");
                pthread_cond_wait(&qu_full_cond, &m);
                }*/
        writeBuffer(a);
        //increment
        count++;
        if(count=1)
                pthread_cond_signal(&qu_empty_cond);
        pthread_mutex_unlock(&m);
        printf("lost mutual exclusion to produce\n");
        waitRandom();
        }
}

void consume(void){
printf("consumer creation success \n");
while(1){
        data a;
        printf("CONSUME: checking for mutual exclusion\n");
        pthread_mutex_lock(&m);
        printf("have mutual exclusion to consume\n");
        //if 0 block
        while(count<=0){
                printf("stuck\n");
                waitRandom();
                pthread_cond_wait(&qu_empty_cond, &m);
                }
        readBuffer(&a);
        shiftBuffer();
        count--;
        //if 19 release
        if(count=19)
                pthread_cond_signal(&qu_full_cond);
        pthread_mutex_unlock(&m);
        printf("lost mutual exclusion to consume\n");
        waitRandom();
        }
}



void createData(data* a){
        printf("creating data\n");
        a->x=1;
        a->y=2;
        a->z=3;
}

void writeBuffer(data a){
        printf("inthefunction1111\n");
        printf("writing to buffer slot %d, new size of buffer is %d\n", count, count+1);
        buffer[count] = a;
        printf("%d      %d      %d",buffer[count].x,buffer[count].y,buffer[count].z);
}

void readBuffer(data* a){
        printf("reading buffer slot 0 \n");
        a->x = buffer[0].x;
        a->y = buffer[0].y;
        a->z = buffer[0].z;
}

void shiftBuffer(void){
        int i;
        printf("deleting buffer slot 0 \n");
        for(i=0;i<count;i++)
                buffer[i]=buffer[i+1];
}

void waitRandom(void){
        srand ( time(NULL) );
        sleep(rand()%2000 + 1000 );
}

Reply With Quote
  #2  
Old October 29th, 2012, 03:52 PM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,906 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 4 Days 1 h 31 m 41 sec
Reputation Power: 1774
Does your code have no indentation in your editor as well?

Your code is impossible to read "as is".

Please edit your post, and post indented code.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

Reply With Quote
  #3  
Old October 29th, 2012, 04:09 PM
Thilar Thilar is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 3 Thilar User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 46 m 43 sec
Reputation Power: 0
Fixed

Quote:
Originally Posted by salem
Does your code have no indentation in your editor as well?

Your code is impossible to read "as is".

Please edit your post, and post indented code.

Reply With Quote
  #4  
Old October 29th, 2012, 04:44 PM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,906 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 4 Days 1 h 31 m 41 sec
Reputation Power: 1774
Well did you compile with any warnings enabled?

Or run the code with any warnings still to be fixed?

Code:
$ gcc -Wall foo.c -pthread
foo.c: In function ‘produce’:
foo.c:68:9: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
foo.c: In function ‘consume’:
foo.c:93:9: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
foo.c: In function ‘waitRandom’:
foo.c:133:9: warning: implicit declaration of function ‘sleep’ [-Wimplicit-function-declaration]

For these
if(count=1)
which I guess should be
if(count==1)

Reply With Quote
  #5  
Old October 29th, 2012, 04:58 PM
Thilar Thilar is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 3 Thilar User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 46 m 43 sec
Reputation Power: 0
Thanks for the suggestions!
I fixed the == operators in the if statements and included unistd.h to fix the problem with sleep. I also added lines to check the return argument of the mutex lock, however I am still having the same problems.

Below is my updated code.

Code:

#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>

typedef struct{
int x,y,z;
}data;

volatile data buffer[20];
volatile int count;
pthread_mutex_t m;
pthread_cond_t qu_empty_cond = PTHREAD_COND_INITIALIZER;
pthread_cond_t qu_full_cond = PTHREAD_COND_INITIALIZER;



void produce();
void consume();
void createData(data*);
void writeBuffer(data);
void readBuffer(data*);
void waitRandom();
void shiftBuffer();


int main (){
pthread_t p, c;
count = 0;

if(pthread_mutex_init(&m, NULL)){
        printf("mutex creation failed\n");
        return 1;
}
else printf("mutex creation sucess\n");

if(pthread_create (&p,NULL,(void *) &produce,NULL)){
        printf("producer creation failed\n");
        return 1;
}

if (pthread_create (&c,NULL,(void *) &consume,NULL)){
        printf("consumer creation failed\n");
        return 1;
}

return 0;
}

void produce(void){
printf("producer creation success count\n");
while(1){
        data a;
        createData(&a);
        printf("PRODUCE: checking for mutual exclusion\n");
        if(pthread_mutex_lock(&m))
                printf("MUTEX lock in PRODUCER failed, blocking until MUTEX is available \n");
        else
        printf("have mutual exclusion to produce\n");
        //if 20 block
        /*while(count>=20){
                printf("stuck \n");
                pthread_cond_wait(&qu_full_cond, &m);
                }*/
        writeBuffer(a);
        //increment
        count++;
        if(count==1)
                pthread_cond_signal(&qu_empty_cond);
        pthread_mutex_unlock(&m);
        printf("lost mutual exclusion to produce\n");
        waitRandom();
        }
}

void consume(void){
printf("consumer creation success \n");
while(1){
        data a;
        printf("CONSUME: checking for mutual exclusion\n");
        if(pthread_mutex_lock(&m))
                printf("MUTEX lock in CONSUMER failed, blocking until MUTEX is available \n");
        else
                printf("have mutual exclusion to consume\n");
        //if 0 block
        while(count<=0){
                printf("stuck\n");
                waitRandom();
                pthread_cond_wait(&qu_empty_cond, &m);
                }
        readBuffer(&a);
        shiftBuffer();
        count--;
        //if 19 release
        if(count==19)
                pthread_cond_signal(&qu_full_cond);
        pthread_mutex_unlock(&m);
        printf("lost mutual exclusion to consume\n");
        waitRandom();
        }
}



void createData(data* a){
        printf("creating data\n");
        a->x=1;
        a->y=2;
        a->z=3;
}

void writeBuffer(data a){
        printf("inthefunction1111\n");
        printf("writing to buffer slot %d, new size of buffer is %d\n", count, count+1);
        buffer[count] = a;
        printf("%d      %d      %d",buffer[count].x,buffer[count].y,buffer[count].z);
}

void readBuffer(data* a){
        printf("reading buffer slot 0 \n");
        a->x = buffer[0].x;
        a->y = buffer[0].y;
        a->z = buffer[0].z;
}

void shiftBuffer(void){
        int i;
        printf("deleting buffer slot 0 \n");
        for(i=0;i<count;i++)
                buffer[i]=buffer[i+1];
}

void waitRandom(void){
        srand ( time(NULL) );
        sleep(rand()%2000 + 1000 );
}

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > HELP! Multiple threads access to global variables

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap