Thread: seg fault on pthread_mutex_init

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    60

    seg fault on pthread_mutex_init

    Hi people!!

    I know this is short notice, but I've had this segmentation fault on a pthread_mutex_init (I'm almot 100% that's where it is), and I can't figure out why.

    in a header.h, I have
    Code:
    typedef struct thread {
      pthread_mutex_t lock;
      int state;
      int remaining_time;
      struct thread * next;
    } thread_data;
    
    thread_data *OpSys,
    	    *user1, *user2, *user3, *user4, *user5,
    	    *MMU_data;
    in my main, I have
    Code:
      pthread_mutex_init(OpSys->lock, NULL);
      pthread_mutex_init(&user1->lock, NULL);
      pthread_mutex_lock(&user1->lock);
      pthread_mutex_init(&user2->lock, NULL);
      pthread_mutex_lock(&user2->lock);
      pthread_mutex_init(&user3->lock, NULL);
      pthread_mutex_lock(&user3->lock);
      pthread_mutex_init(&user4->lock, NULL);
      pthread_mutex_lock(&user4->lock);
      pthread_mutex_init(&user5->lock, NULL);
      pthread_mutex_lock(&user5->lock);
      pthread_mutex_init(&MMU_data->lock, NULL);
      pthread_mutex_lock(&MMU_data->lock);
    somewhere in that mess I get the seg fault... I'm not too good with pointers, and even though it compiles fine, I can't obviously run that.

    Any suggestions???

    Thanks
    "Shallow men believe in luck. Strong men believe in cause and effect."
    -Ralph Waldo Emerson

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Can we assume that you have allocated memory to go with all those thread_data pointers? Also assuming that the missing ampersand is a typo.

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    60
    oopsy... yes, there's an ampersand missing, and it's not voluntary

    Can we assume that you have allocated memory to go with all those thread_data pointers?
    what do you mean?

    I did allocate the memory... didn't I?
    ah crap.. no I didn't!!
    ---------------------------------------------
    Now, I did the memory allocation
    Code:
    thread_data *OpSys;
    OpSys = (thread_data *) malloc (sizeof(thread_data));
    but that gives me a "conflicting type for OpSys", "previous declaration of OpSys", and "warning: initilization makes integer from pointer without a cast"... :-/
    Last edited by Lau; 12-10-2003 at 01:09 PM.
    "Shallow men believe in luck. Strong men believe in cause and effect."
    -Ralph Waldo Emerson

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > a "conflicting type for OpSys", "previous declaration of OpSys"
    You've got two of them, and they don't agree

    > "warning: initilization makes integer from pointer without a cast"
    Makes no sense given that line of code
    The usual problem is makes pointer from int cast, and that's because you didn't include stdlib.h for malloc

    Don't forget to malloc your user1 to 5 (why isn't this an array of users)

    pthread_mutex_init(OpSys->lock, NULL);
    pthread_mutex_init(&user1->lock, NULL);
    One of these needs an &
    or the other one doesn't need an &
    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.

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    60
    > a "conflicting type for OpSys", "previous declaration of OpSys"
    You've got two of them, and they don't agree
    I have it declared once and only once. This error didn't come up before I wrote in the maloc part, that's why I'm confused

    > "warning: initilization makes integer from pointer without a cast"
    Makes no sense given that line of code
    The usual problem is makes pointer from int cast, and that's because you didn't include stdlib.h for malloc

    Don't forget to malloc your user1 to 5 (why isn't this an array of users)
    stdlib.h is in there and has been...

    pthread_mutex_init(OpSys->lock, NULL);
    pthread_mutex_init(&user1->lock, NULL);
    One of these needs an &
    or the other one doesn't need an &
    yep... I just forgot that, but it's in too now!

    By the way, the malloc is done in header.h, not in main. Is that a problem?
    Last edited by Lau; 12-10-2003 at 01:57 PM.
    "Shallow men believe in luck. Strong men believe in cause and effect."
    -Ralph Waldo Emerson

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    No idea - post a short and complete program which demonstates the problem
    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.

  7. #7
    Registered User
    Join Date
    Nov 2002
    Posts
    60
    Salem,
    so basically, what we have to do is simulates an operating system, that created five user thread and a memory management unit (i'm not even here yet)
    We have to implement a round robin schedule. Each thread is put in running state after the other.

    my header file has the structs
    Code:
    /* Struct for thread's info */
    typedef struct thread {
      pthread_mutex_t lock;
      int state;
      int remaining_time;
      struct thread * next;
    } thread_data;
    
    typedef struct {
      thread_data * head;
    } queue;
    
    queue * qp;
    
    thread_data * OpSys,
    	    * user[USERS],
    	    * MMU_data;
    in my main (i switched the memory allocation by the way from the header to main)
    Code:
    int main (void) {
    
      int i;
    
      OpSys = (thread_data *) malloc (sizeof(thread_data));
      MMU_data = (thread_data *) malloc (sizeof(thread_data));
      for (i=0; i<USERS; i++)
        user[i] = (thread_data *) malloc (sizeof (thread_data));
    
      /* initializes all mutex and set them to lock (except OS one) */
      pthread_mutex_init(&OpSys->lock, NULL);
      pthread_mutex_init(&MMU_data->lock, NULL);
      pthread_mutex_lock(&MMU_data->lock);
      for (i=0; i<USERS; i++) {
        pthread_mutex_init(&user[i]->lock, NULL);
        pthread_mutex_lock(&user[i]->lock);
      }
    
      /* initialize queue */
      insert();
    
      /* all states are set to NOT DONE (0) */
      OpSys->state = MMU_data->state = NOT_DONE;
      for (i=0; i<USERS; i++)
        user[i] = NOT_DONE;
    
      create_t (OS_thread, OS);
    
      pthread_join (OS_thread, NULL);
      return;
    } /* end of main() */
    I've fixed all the errors, and the program compiles again

    PS: I switched to an array for my users... yep, that makes it a lot clearer and shorter!! ;-)
    Last edited by Lau; 12-10-2003 at 02:54 PM.
    "Shallow men believe in luck. Strong men believe in cause and effect."
    -Ralph Waldo Emerson

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well now is the time to compile it with debug
    say
    gcc -ggdb prog.c

    and use the debugger to catch the line where the segfault happens

    Like
    gdb a.out
    run
    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.

  9. #9
    Registered User
    Join Date
    Nov 2002
    Posts
    60
    well, the seg fault is gone... but it's not working correctly! I'm gonna go bang my head on the wall for a while. Doubt that'll help, but who knows
    "Shallow men believe in luck. Strong men believe in cause and effect."
    -Ralph Waldo Emerson

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
      /* all states are set to NOT DONE (0) */
      OpSys->state = MMU_data->state = NOT_DONE;
      for (i=0; i<USERS; i++)
        user[i]->state = NOT_DONE; /* perhaps? */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  11. #11
    Registered User
    Join Date
    Nov 2002
    Posts
    60
    Thanks Dave for catching that...

    That doesn't begin to solve what doesn't work with that damn program!!!
    "Shallow men believe in luck. Strong men believe in cause and effect."
    -Ralph Waldo Emerson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting a seg fault
    By ammochck21 in forum C Programming
    Replies: 11
    Last Post: 01-23-2009, 05:27 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  4. weird seg fault
    By Vermelho in forum C Programming
    Replies: 3
    Last Post: 05-10-2008, 08:27 PM
  5. Seg Fault Problem
    By ChazWest in forum C++ Programming
    Replies: 2
    Last Post: 04-18-2002, 03:24 PM