Thread: Mutex_Create_Threads - Segmentation fault (core dumped)

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    58

    Question Mutex_Create_Threads - Segmentation fault (core dumped)

    Hi ,
    Could you view my code - why do I get "Segmentation fault (core dumped)" when trying to run this program ?

    there is some warning regarding pthread_create but I don't understand what is the problem

    Code:
    #include <pthread.h>
    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    pthread_mutex_t mutexid;
    
    typedef struct
    {
      pid_t ProcID;
      pthread_t ThreadID;
      time_t rawtime;
    } ThreadInfo;
    
    thread_func(void *parameters)
    {
      struct ThreadInfo *p = (struct ThreadInfo*) parameters; 
        pthread_mutex_lock(&mutexid);
        sleep(2);
      printf("Process ID= %d\n",getpid());
      printf("this thread func");
      
      pthread_mutex_unlock(&mutexid);
    }
    int main()
    {
    
     ThreadInfo Thread_args;
     pthread_t Threads[10];
     int i;
     pthread_mutex_init(&mutexid,NULL);
    
      for(i=0;i<10;i++)
      {pthread_create(&Threads[i], NULL, &thread_func, &Thread_args);
        printf("this main");
    }
     
      for(i=0;i<10;i++)
     { pthread_join(&Threads[i], NULL); //Make sure the first thread has finished.
      }
     pthread_mutex_destroy(&mutexid);
    return 0;
    }

    $gcc Mutex_Create_Threads.c -lpthread -o Mutex_Create_Threads
    Mutex_Create_Threads.c: In function ‘main’:
    Mutex_Create_Threads.c:35:3: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type [enabled by default]
    In file included from Mutex_Create_Threads.c:1:0:
    /usr/include/pthread.h:225:12: note: expected ‘void * (*)(void *)’ but argument is of type ‘int (*)(void *)’
    Mutex_Create_Threads.c:40:2: warning: passing argument 1 of ‘pthread_join’ makes integer from pointer without a cast [enabled by default]
    In file included from Lesson_7.1_Mutex_Create_Threads.c:1:0:
    /usr/include/pthread.h:242:12: note: expected ‘pthread_t’ but argument is of type ‘pthread_t *’
    $./_Mutex_Create_ThreadsSegmentation fault (core dumped)

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    What is the return value of thread_func? It's suppose to be void*, you don't seem to return anything.

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    58
    this not coused the error

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by ilans11il View Post
    this not coused the error
    What makes you say so? It's a runtime error. Look at the warnings:

    warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type

    //and

    expected ‘void * (*)(void *)’ but argument is of type ‘int (*)(void *)

    Surely there is no way that following an int, in the belief that it's a void* can lead to a segfault.

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by ilans11il View Post
    this not coused the error
    There's also a second warning:
    Quote Originally Posted by ilans11il View Post
    Mutex_Create_Threads.c:40:2: warning: passing argument 1 of ‘pthread_join’ makes integer from pointer without a cast [enabled by default]
    In file included from Lesson_7.1_Mutex_Create_Threads.c:1:0:
    /usr/include/pthread.h:242:12: note: expected ‘pthread_t’ but argument is of type ‘pthread_t *
    Code:
    pthread_join(&Threads[i], NULL); //Make sure the first thread has finished.
    You call pthread_join() with a pointer to pthread_t instead of just a pthread_t (you don't need the &).

    Bye, Andreas

  6. #6
    Registered User
    Join Date
    Feb 2013
    Posts
    58
    thank youuuuuuuuuuuuuu

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation Fault (core dumped)
    By TimI in forum C Programming
    Replies: 4
    Last Post: 11-05-2012, 07:45 AM
  2. Segmentation Fault (core dumped)
    By Juan Cervantes in forum C Programming
    Replies: 9
    Last Post: 10-29-2012, 05:58 PM
  3. Segmentation fault (core dumped)
    By KoRn KloWn in forum C Programming
    Replies: 3
    Last Post: 09-22-2012, 02:34 AM
  4. Segmentation Fault (Core Dumped)
    By pureenergy13 in forum C Programming
    Replies: 3
    Last Post: 11-02-2011, 07:50 AM
  5. Segmentation fault, core dumped
    By dweenigma in forum C Programming
    Replies: 2
    Last Post: 05-21-2007, 03:50 PM