Code Monkey home page Code Monkey logo

samkitjain / producer-consumer-posix-code Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 0.0 140 KB

Consider a system comprising of two producer processes, one consumer process, and two buffers as shown above. The size of each buffer is 10. Each producer process randomly creates a specific colored item: Producer_Red creates red colored items, and Producer_Blue creates blue-colored items. Each producer deposits its item in any one of the two buffers, whichever has space available. If both buffers have space available, then one of them is selected arbitrarily.. Associated with each item created by a specific producer is a local UNIX timestamp in microseconds (use the gettimeofday() function) which records the time that item was placed in the buffer. The items produced by the producers will be two strings of the form shown below: “RED unix_timestamp” or “BLUE unix_timestamp” Each producer process, after successfully depositing an item in the buffer, will write that item (essentially the “COLOR Unix_timestamp” string) to its log file called Producer_COLOR.txt (e.g. Producer_RED.txt). The consumer retrieves an item from whichever buffer an item is available, and writes it to a file named “Consumer.txt”.

Python 4.93% C 90.91% C++ 3.93% Shell 0.23%

producer-consumer-posix-code's Introduction

/* Name: Samkit Jain
*/

Instruction for compiling and running source file:
Execute following 2 commands in sequence from terminal by keeping the currect directory : the submission directory
1) make all
2) ./main.o

Upon successful run 3 text files are generated in current folder:
1. Producer_RED.txt
2. Producer_BLUE.txt
3. Consumer.txt


General Info :
There 4 files in the submission folder:
1. main.c		: Main code. Forks all processes ( consumer , Red-producer and Blue-producer )
2. prodcon.c	: Contains code for launching one thread per process and the task consumer , Red-producer and Blue-producer will perform
3. prodcon.h	: Contains the structures of all buffres , variable , locks , condition variables and flags . Also contains constant declaration. ITEMNo is the no. of items that will be produced by red and blue producers individually. For submission its value is set to 10000.
4. Makefile		: For compiling the code


Code Flow :
The main initializes a shared memory structure which contains 2 buffer (buffer1 and buffer 2 of length 10 and depth 40) , flags( redproddone , blueproddone) , condition variables (SpaceAvailable1 , SpaceAvailable2 and ItemAvailable) , header variables ( [redconhead , blueconhead] for referncing in buffer1 and buffer 2 by consumer ; [redprodhead , blueprodhead] for referncing in buffer1 and buffer 2 by redproducer and blue producer resp. ) and counters (Ccount1 and count2 keep track of empty spaces in buffer1 and buffer2 resp. ). 

It then forks 3 processes : Consumer process , Red-producer process and Blue-producer process and each process executes prodcon.c code . During call the processes specify whether they are consumer or producer and color of ittem they will produce i case if they are producer.

Each process then creates a thread and call respective function ( consumer process thread call consumerfn , producer process call producerfn). The producer process thread also pass pass a variable itemtype as function parameter. itemtype=0 for Red-producer thraed and item-type=1 for blu-producer thread. Depending on this value producer thread generate either RED item(in case of Red-producer thread) or BLUE item(in case of Blue-producer thread).

The producer function loop till they don't generate ITEMNO items whereas Consumer process loops till it hasn't consumed all items in buffer1 and buffer2 and both producers have exited.
The functionailty of producers and consumers is exactly as asked in assigment definition.





 


producer-consumer-posix-code's People

Contributors

samkitjain avatar

Stargazers

Jay-sanjay avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.