Hi! I'm trying to emplement the job queue. Somehow i can't initialize it: the queue is empty
Inline Code Example Here
//INCLUDES
//GLOBALS
pthread_mutex_t job_queue_mutex = PTHREAD_MUTEX_INITIALIZER;
int i;
int val=0;
//STRUCTURES
typedef struct job
{
int data;
struct job *next;
}job_t;
// A linked list of jobs.
job_t *job_queue=NULL;
// A semaphore counting the number of jobs in the queue.
sem_t job_queue_count;
//FUNCTION'S DECLARATION
int init_job_queue(job_t **head,job_t **tail);//initialize the queue
int add_node_to_head(job_t head);//add job to queue
void remove_node_from_tail(job_t **head);//remove job from queue in FIFO order
int process_job(job_t *the_job);//thread function itself
void thread_func(void* arg);
int main()
{
pthread_t thr_id[N_threads];//array of thread id's
int i=0;
int status=-1;
job_t (head)=NULL;
job_t (tail)=NULL;
sem_init(&job_queue_count,0,1);//initializing semaphore, 1 token is available
status= init_job_queue(&head,&tail);
if(status==-1)
{
printf("Error in memory allocation\n");
return -1;
}
for(i=0;i<N_threads;i++)
{
pthread_create(&thr_id[i],NULL,thread_func,NULL);
}
for(i=0;i<N_threads;i++)
{
pthread_join(thr_id[i],NULL);
}
printf("MAIN:all threads have been terminated\n");
return 0;
}
void* thread_func(void* arg)
{
job_t* current_job = NULL;
printf("THREAD FUNC: thread_id = 0x%x job_queue=0x%d \n", pthread_self(),job_queue);
for(;;)
{
sem_wait(&job_queue_count);
pthread_mutex_lock (&job_queue_mutex);
//safe
if (job_queue == NULL)
{
current_job = NULL;
printf("There is nothing to do. Sorry\n");
}
else
{
//taking the job
current_job = job_queue;
job_queue = job_queue->next;//remooving the job from the job queue
}
pthread_mutex_unlock (&job_queue_mutex);
sem_post(&job_queue_count);
//in case that queue was empty:
if (current_job == NULL)
break;
//processing the job
process_job (current_job);
free (current_job);
}
return NULL;
}
//functions definition
int init_job_queue(job_t **head,job_t **tail)
{
int flag=-1;
int i;
i=0;
head=(job_t)malloc(sizeof(job_t));
(head)->next =NULL;
(head)->data = 0;
for(i=0;i<N_threads;i++)
{
flag=add_node_to_head(&head);
if(flag==-1)
{
printf("Error in memory allocation.\n");
return -1;
}
}
return 0;
}
int add_node_to_head(job_t** head)
{
job_t* temp=NULL;
temp=(job_t*)malloc(sizeof(job_t));
if(temp==NULL)
{
printf("Error in memory allocation.\n");
return -1;
}
temp->next=head;
temp->data=val+1;
*head=temp;
sem_post (&job_queue_count);
return 0;
}
void remove_node_from_tail(job_t** head)
{
job_t *p1=head;
job_t p2=p1->next;
while(p2->next!=0)
{
p1=p1->next;
p2=p2->next;
}
p1->next=NULL;
free(p2);
return;
}
int process_job(job_t the_job)
{
printf("JOB NUMBER :%d process_job() thread_id=0x%d the_job=0x%d data = %d\n",i,pthread_self(),the_job,the_job->data);
return 0;
}
Help,please.....