Seg Fault - Can not access memory ?

  • Thread starter Mahendra Kumar Kutare
  • Start date
M

Mahendra Kumar Kutare

Hi,

I am trying to implement a webserver with boss-worker model thread pool
implementation -

I have a header declaration threadpool.h as -

typedef struct threadpool_work {
void (*routine) ();
void *arg;
struct threadpool_work *next;
} threadpool_work_t;

typedef struct threadpool {
/* Pool Characteristics */
int num_threads;
int max_queue_size;

int do_not_block_when_full;
pthread_t *threads;
int cur_queue_size;
threadpool_work_t *queue_head;
threadpool_work_t *queue_tail;

pthread_mutex_t queue_lock;
pthread_cond_t queue_not_empty;
pthread_cond_t queue_not_full;
pthread_cond_t queue_empty;

int queue_closed;
int shutdown;
} *threadpool_t;

void threadpool_init(threadpool_t *threadpoolp,
int num_worker_threads,
int max_queue_size,
int do_not_block_when_full);

int threadpool_add_work(threadpool_t threadpool,
void *routine,
void *arg);

int threadpool_destroy(threadpool_t threadpoolp, int finish);

void threadpool_thread(threadpool_t threadpool);

In my server_threadp.c main method , I do following -

threadpool_t threadpool;

/* Allocate a pool data structure */
if ((threadpool = (threadpool_t) malloc(sizeof(struct threadpool))) ==
NULL) {
perror("malloc");
exit(1);
}

threadpool_add_work function has following code -

if (threadpool->cur_queue_size == 0) {
threadpool->queue_tail = threadpool->queue_head =workp;
printf("Signal for pthread_cond_wait waiting on
queue_not_empty:: %d\n", threadpool->queue_not_empty);
pthread_cond_signal(&threadpool->queue_not_empty);
} else {
(threadpool->queue_tail)-> next = workp;
threadpool->queue_tail = workp;
}

For the first HTTP request, it will find threadpool->cur_queue_size == 0
and hence will execute the IF block of the above code, but for any
subsequent request it will execute ELSE block above.

When I am sending 2nd request, ITS SEGFAULTING at
(threadpool->queue_tail)-> next = workp;

I debugged it through gdb and when trying to print following -

(threadpool->queue_tail)-> next

it throws - Can not access memory

Which i believe is because - Memory is not allocated to the structure
threadpool self element.

My Question is - When i allocate memory in the begining to threadpool
structure do i have to still allocate memory for any element which is
itself in this case. ? If so, how do i do that ?

Or is there something else which I am missing here ?

Thanks
Mahendra
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,183
Messages
2,570,965
Members
47,511
Latest member
svareza

Latest Threads

Top