|
-
March 30th, 2012, 04:01 PM
#1
Segmentation fault in bounded buffer problem
buffer.h
Code:
#ifndef BUFFER_H
#define BUFFER_H
typedef int buffer_item;
class Buffer
{
public:
#define BUFFER_SIZE = 5;
};
#endif
buffer.cpp
Code:
#include "buffer.h"
#include <iostream>
#include <fstream>
#include <semaphore.h>
#include <pthread.h>
#include <stdlib.h>
using namespace std;
buffer_item buffer[5];
pthread_mutex_t mutex;
sem_t mutex2;
sem_t sem;
pthread_t tid; // the thread id
pthread_attr_t attr; // thread attributes
int insert_item(buffer_item item)
{
do{
sem_wait(&sem);
pthread_mutex_lock(&mutex);
pthread_mutex_unlock(&mutex);
sem_post(&mutex2);
}while(true);
return 0;
return 1;
}
int remove_item(buffer_item *item)
{
bool ok = false;
int loc = 0;
for (int i =0; i < 5; i++)
{
if (buffer[i] == *item)
{
ok = true;
loc = i;
}
}
if (ok == true)
{
do{
sem_wait(&mutex2);
pthread_mutex_lock(&mutex);
buffer[loc] = -1;
pthread_mutex_unlock(&mutex);
sem_post(&sem);
} while(true);
return 0;
}
return 1;
}
void *producer(void *param)
{
buffer_item item = (buffer_item) param;
int random = rand() % 5;
while(true)
{
sleep(random);
item = rand();
buffer[random] = (buffer_item) rand;
if(insert_item(item))
cout << "Oh no!";
else
cout << "We did it!";
}
}
void *consumer(void *param)
{
buffer_item *item = (buffer_item *) param;
int random = rand() % 5;
while(true)
{
sleep(random);
if(remove_item(item))
cout << "Oh no!";
else
cout << "We did it!";
}
}
void init()
{
pthread_attr_init(&attr);
/* create the mutex lock */
pthread_mutex_init(&mutex, NULL); // mutex lock
/*** critical section ***/
/* create empty and full semaphores */
sem_init(&sem, 0, 1); // empty
sem_init(&mutex2, 0, 5); // full
}
void Swap(bool *a, bool *b)
{
bool temp = *a;
*a = *b;
*b = temp;
}
int main(int argc, char *argv[])
{
if (argc !=4)
{
cout << "Error!";
return 1;
}
int GrimReaper = 0;
int TeaPartySquad =0;
int Democrats = 0;
GrimReaper = (int) argv[1]; // time main sleeps before exiting
cout << argv[1];
cout << GrimReaper;
TeaPartySquad = (int) argv[2]; // the number of producers
Democrats = (int) argv[3]; // the number of consumers
if (argv[1] <= 0 || argv[2] <= 0 || argv[3] <=0)
{
cout << "Huh?";
return 1;
}
for (int i =0; i < 5; i++)
{
buffer[i] = 0; // initialize buffer
}
init(); // initialize lock and empty and full semaphores
for (int i = 0; i < *argv[2]; i++)
{
pthread_create(&tid, &attr, producer, (char *) i);
}
for (int j =0; j < *argv[3]; j++)
{
pthread_create(&tid, &attr, consumer, (char *) j);
}
sleep((unsigned int ) argv[1]);
pthread_exit(0);
return 0;
}
Why am I getting a segmentation fault?
I'm thinking that either my random is adding some number that's out of range as an index, despite my % that should prevent it ! , or it's trying to remove from an invalid index or something. Or I should have called join, though I thought the sleep and exit in the main thread would take care of that problem.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|