CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #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.

  2. #2
    Join Date
    Jan 2009
    Posts
    596

    Re: Segmentation fault in bounded buffer problem

    Well, in this bit:
    Code:
       void *producer(void *param)
       {
          buffer_item item = (buffer_item) param;
          int random = rand() &#37; 5;
       
          while(true)
          {
             sleep(random);
          
             item = rand();
             buffer[random] = (buffer_item) rand;
          
             if(insert_item(item))
                cout << "Oh no!";
             else
                cout << "We did it!";
          
          }
       
       
       }
    See the bit in red? You are casting the address of the rand() function to a buffer_item. Presumably not what you wanted...

    C-style casts are dangerous - use them only when absolutely necessary. They are always legal C++, so will never give a compiler error. But, the result may not be what you intended.
    Last edited by Peter_B; March 30th, 2012 at 04:53 PM.

  3. #3
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Segmentation fault in bounded buffer problem

    I would like to emphasize what Peter B say and that's: never ever use C-style cast in C++ code! They are dangerous since they effectively silence the compiler thus making you get away with more or less just any stupidity you can come up with.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  4. #4

    Re: Segmentation fault in bounded buffer problem

    Changed it to casting the variable item to buffer_item instead.

    Segmentation fault still there. Perhaps I can't think of what I should be casting.

  5. #5

    Re: Segmentation fault in bounded buffer problem

    Also, and this one if for processes, I'm getting a seg fault here too. I'm trying to make it do the fib sequence. However, what's causing the segmentation fault?

    Code:
    #include <stdio.h>
    #include <sys/shm.h>
    #include <sys/stat.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    #define MAX_SEQUENCE 10
    
    typedef struct 
    {
      long fib_sequence[MAX_SEQUENCE];
      int sequence_size;
    }shared_data;
    
    int segment_id;
    int main(int argc, char *argv[])
    {
    
      if (argc !=2)
        {
          return 1;
        }
    
      if (!((int) argv[1] <= MAX_SEQUENCE))
        return 1;
    
    shared_data *shared_memory;
    
    shared_memory = (shared_data *) shmat(segment_id, NULL, 0);
    segment_id = shmget(IPC_PRIVATE, sizeof(shared_data), S_IRUSR| S_IWUSR);
    
    shared_memory->sequence_size = (int) argv[1];
    pid_t pid;
    
    pid = fork();
    
    if (pid == 0)
    {
    if (shared_memory->sequence_size == 1)
    shared_memory->fib_sequence[0] = 0;
    else if (shared_memory->sequence_size == 2)
    {
    shared_memory->fib_sequence[0] = 0;
    shared_memory->fib_sequence[1] = 1;
    }
    
    else 
    {
    shared_memory->fib_sequence[0] = 0;
    shared_memory->fib_sequence[1] = 1;
    int i;
    
    for ( i = 2; i < shared_memory->sequence_size; i++)
    {
    shared_memory->fib_sequence[i] = shared_memory->fib_sequence[i-1] + shared_memory->fib_sequence[i-2];
    
    }
    
    }
    shmdt(shared_memory);
    }
    
    else if (pid > 0)
    {
    
    wait();
    int i;
    for (i = 0; i < shared_memory->sequence_size; i++)
    {
    
      cout << shared_memory->fib_sequence[i];
      
    
    }
    shmctl(segment_id, IPC_RMID, NULL);
    }
    
    
    cout << shared_memory->sequence_size;
    
       return 0;
    }
    Sorry about all the code, I have 4 small projects I had to do at once.

  6. #6
    Join Date
    Jan 2009
    Posts
    596

    Re: Segmentation fault in bounded buffer problem

    If you read the documentation you would see that here:
    Code:
    shared_memory = (shared_data *) shmat(segment_id, NULL, 0);
    segment_id = shmget(IPC_PRIVATE, sizeof(shared_data), S_IRUSR| S_IWUSR);
    you are calling these functions in the wrong order.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured