Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » Parallel Processing ProgramsRSS Feeds

Program to implement a multi-access threaded queue with multiple threads inserting and multiple threads extracting from the queue

Posted By: Easy Tutor     Category: C Programming     Views: 3035

Write a program to implement a multi-access threaded queue with multiple threads inserting and multiple threads extracting from the queue

Code for Program to implement a multi-access threaded queue with multiple threads inserting and multiple threads extracting from the queue in C Programming

# include <stdio.h>
# include <pthread.h>
# define QUEUE_SIZE 10

int In=0,Out=0;
void *Producer(void *);
void *Consumer(void *);

pthread_mutex_t read_mutex=PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t write_mutex=PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t Queue_Not_Full=PTHREAD_COND_INITIALIZER;
pthread_cond_t Queue_Not_Empty=PTHREAD_COND_INITIALIZER
;
int Queue_Is_Empty()
{
    if(In==Out)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

int Queue_Is_Full()
{
    if(Out==((In+1)%QUEUE_SIZE))
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

int main()
{    
    pthread_t tid[4];
    int p_array[2];
    int c_array[2];
    int iCount;
    
    p_array[0]=1;
    p_array[1]=2;
    
    c_array[0]=1;
    c_array[1]=2;
        
    pthread_create(&tid[0],NULL,Consumer,(void *)c_array[0]);
    pthread_create(&tid[1],NULL,Consumer,(void *)c_array[1]);
    pthread_create(&tid[2],NULL,Producer,(void *)p_array[0]);
    pthread_create(&tid[3],NULL,Producer,(void *)p_array[1]);
    
    for(iCount=0;iCount<4;iCount++)
    {
        pthread_join(tid[iCount],NULL);
    }
    return 0;
}

void *Producer(void *arg)
{    
    int *Pno;
    Pno=(int *) arg;
    for(;;)
    {
        pthread_mutex_lock(&write_mutex);
        if(Queue_Is_Full())
        {                                    
            pthread_cond_wait(&Queue_Not_Full,&write_mutex);            
        }                
        printf("Produce [%d] :%d \n",Pno,In);
        In=(In + 1) % QUEUE_SIZE;
        pthread_mutex_unlock(&write_mutex);
        pthread_cond_signal(&Queue_Not_Empty);        
    }    
    
}

void *Consumer(void * arg)
{
    int *Cno;
    Cno=(int *) arg;
    for(;;)
    {
        pthread_mutex_lock(&read_mutex);
        if(Queue_Is_Empty)
        {                        
            pthread_cond_wait(&Queue_Not_Empty,&read_mutex);            
        }
        printf("Consume [%d] :%d \n",Cno,Out);
        Out=(Out + 1) % QUEUE_SIZE;
        pthread_mutex_unlock(&read_mutex);        
        pthread_cond_signal(&Queue_Not_Full);                
    }    
}
  
Share: 



Easy Tutor
Easy Tutor author of Program to implement a multi-access threaded queue with multiple threads inserting and multiple threads extracting from the queue is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!