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)