Reputation: 570
I have looked all over for pthread_join examples, I am having troubles debugging this code. My code works correctly when I don't run a pthread_join and have it commented out, but when I use it, I get a backtrace.
How would I use GDB to trace a thread? or better how would I fix this?
struct thread_param tp[THREADNUM];
int
search_funct(struct conn_pair temp, enum conn_state markState)
{
pthread_t threads[THREADNUM]; /* Threads */
int thread_cr_res = 0;
int thread_join_res = 0; /* Various ints */
results = 0; /* Zero the results before moving on */
int number = num_elements;
int divisor = THREADNUM;
int remainder = number % divisor;
int multi = number / divisor;
// Initalize counters
int startValue = 0;
int endValue = 0;
int i = 0;
/* Create the threads */
while(i < divisor) {
i++;
startValue = endValue;
endValue = i*multi;
if( i == divisor) {
endValue = endValue +remainder;
}
#ifdef __DEBUG__
printf("%d.) start %d, end %d\n",i,startValue,endValue);
#endif
/* Pass parameters to thread as struct */
tp[i].conn = (struct conn_pair *)&temp;
tp[i].start = startValue;
tp[i].end = endValue;
tp[i].markState = markState;
thread_cr_res = pthread_create(&threads[i],
NULL,
thread_function,
(void*)&tp[i]);
if(thread_cr_res != 0){
fprintf(stderr,"THREAD CREATE ERROR");
return (-1);
}
}
//~ /* Joining and wait for the threads */
//~ for (i = 0; i < (THREADNUM-1); i++){
//~ int rc = pthread_join(threads[i], NULL);
//~ if (rc)
//~ {
//~ printf("# ERROR: Return code from pthread_join() is %d\n", rc);
//~ }
//~ }
/* Wait for the results since threads don't return a value */
printf("results: %d",results);
if(results > 0)
return results;
else
return modeCode;
}
Param:
struct thread_param {
struct conn_pair *conn;
int start;
int end;
enum conn_state markState;
};
Thread function
void *thread_function(void *arg){
int i = 0;
for(i = ((struct thread_param*)arg)->start; i < ((struct thread_param*)arg)->end; i++)
{
if( \
(!memcmp(&((struct thread_param*)arg)->conn->ip_src, &connection_arr[i].ip_src, sizeof(connection_arr[i].ip_src)) && \
!memcmp(&((struct thread_param*)arg)->conn->ip_dst, &connection_arr[i].ip_dst, sizeof(connection_arr[i].ip_dst))) && \
(((struct thread_param*)arg)->conn->ip_p == connection_arr[i].ip_p) && \
((((struct thread_param*)arg)->conn->th_dport == connection_arr[i].th_dport) || \
(((struct thread_param*)arg)->conn->th_sport == connection_arr[i].th_sport)))
{
#ifdef __DEBUG__
printf("matched something\n");
#endif
/* Special states to be marked for cleanup */
if (((connection_arr[i].state == rpc_tmp) || \
(connection_arr[i].state == ftp_actv_tmp) || \
(connection_arr[i].state == ftp_pasv_tmp)) && \
(((struct thread_param*)arg)->markState == marked_cleanup)
)
{
// connection_arr[i].state = marked_cleanup;
pthread_mutex_lock( &mutex1 );
results += 3;
dirtyElements=i;
pthread_mutex_unlock( &mutex1 );
pthread_exit((void *)0);
}
/* States that shouldn't be overwritten since we need to know these */
if(((struct thread_param*)arg)->conn->ip_p == IPPROTO_TCP){
pthread_mutex_lock( &mutex1 );
results += 2;
pthread_mutex_unlock( &mutex1 );
pthread_exit((void *)0);
}
/* Ultimately upsert the connection state */
//connection_arr[i].state = ((struct thread_param*)arg)->markState;
pthread_mutex_lock( &mutex1 );
results += 2;
pthread_mutex_unlock( &mutex1 );
pthread_exit((void *)0);
}
}
}
pthread_exit((void *)0);
}
Here is the error I see:
# ERROR: Return code from pthread_join() is 22
results: 0
0x11 192.168.1.100:54925 -> 239.255.255.250:1900 10
65535 allocated, 1 used
*** glibc detected *** ./prog: malloc(): memory corruption: 0x00000000
Upvotes: 1
Views: 1998
Reputation: 84159
Youd threads[0]
is not initialized, you start creating threads from index 1
.
Another problem - you pass address of a local variable temp
to the thread function:
search_funct(struct conn_pair temp, enum conn_state markState)
{
...
tp[i].conn = (struct conn_pair *)&temp;
That is BadTM.
Upvotes: 2