C Programming Tutorial

 
 
 
 

Image

# 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);                
    }    
}

    Related Post:
  1. Program to inter change to number in each other

  2. Use of continue statement

  3. Program to overcome the forward dependency using block scheduling using the most equitable distribution of work

  4. Program to save the Interrupt Vector Table (IVT) in a file

  5. Program to do sum of the elements of the array by loop splitting and each process adds its partial sum to the final sum

  6. ILLUSTRATION OF fseek & ftell FUNCTIONS

  7. Program to add 4 integer values using 2 processes

  8. Program that will round a floating-point number to an indicated decimal place no. 17.457 would yield the value 17.46 when it is rounded off to 2 de

  9. TRAPEZOIDAL RULE

  10. Program of addition, subtraction,multiplication and division of rational numbers

  11. Program to compute sum of an array using parallel processing

  12. Program to Print English Alphabets from Printer using Interrupts

  13. Program that uses while loop and calculate sum of every third integer number with i=2 (i.e. sum = 2+5+8+11+.) for values of I that is less than 100

  14. Example of preprocessor - 6

  15. PROGRAM OF CREAT HEADER FILES FOR P_FORK,P_JOIN AND SHARED MEMORY

  16. Program that accepts characters in lower case, stores them in an array and prints them in the upper-case and vice - versa

  17. PROGRAM TO FIND SUM OF ALL INTEGER WHICH IS &gt; 100 AND &lt; 200 AND WHICH IS DIVISIBLE BY 7

  18. Program to print inventory report that counts total based on quantity and rate of items

  19. Program of histogram by calculating the partial histograms and consolidating in the end to get the final histogram

  20. PROGRAM TO FIND (X + Y) / (X - Y) , (X + Y) / 2 and (X + Y) * (X - Y)