Apr 19, 2012

C Program to solve the producer consumer problem

Write a C Program to solve the producer consumer problem with two processes using semaphores.
Producer-consumer problem is the standard example of multiple process synchronization problem. The problem occurs when concurrently producer and consumer tries to fill the data and pick the data when it is full or empty. producer consumer problem is also known as bounded-buffer problem. In this program We use the semaphores, to solve the problem.Read more about C Programming Language . and read the C Programming Language (2nd Edition) by K and R.
/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>


#define NUM_LOOPS 20
int main(int argc, char* argv[])
{
    int sem_set_id;
    union semun sem_val;
    int child_pid;
    int i;
    struct sembuf sem_op;
    int rc;
    struct timespec delay;
    
    
    sem_set_id = semget(IPC_PRIVATE, 1, 0600);
    if (sem_set_id == -1) {
        perror("main: semget");
        exit(1);
    }
    printf("Semaphore set created,
    semaphore set id '%d'.\n", sem_set_id);
    
    
    sem_val.val = 0;
    rc = semctl(sem_set_id, 0, SETVAL, sem_val);
    child_pid = fork();
    switch (child_pid) {
        case -1:
        perror("fork");
        exit(1);
        case 0:
        for (i=0; i<NUM_LOOPS; i++) {
            sem_op.sem_num = 0;
            sem_op.sem_op = -1;
            sem_op.sem_flg = 0;
            semop(sem_set_id, &sem_op, 1);
            printf("consumer: '%d'\n", i);
            fflush(stdout);
            sleep(3);
        }
        break;
        default:
        for (i=0; i<NUM_LOOPS; i++)
        {
            printf("producer: '%d'\n", i);
            fflush(stdout);
            sem_op.sem_num = 0;
            sem_op.sem_op = 1;
            sem_op.sem_flg = 0;
            semop(sem_set_id, &sem_op, 1);
            sleep(2);
            if (rand() > 3*(RAND_MAX/4))
            {
                delay.tv_sec = 0;
                delay.tv_nsec = 10;
                nanosleep(&delay, NULL);
            }
        }
        break;
    }
    
    
    return 0;
}
Read more c programs
C Basic
C Strings
You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link