Producer Consumer Problem in C using Semaphores and Shared Memory
Written by Abhishek Ghosh
The classic bounded-buffer problem can be implemented using a variety of synchronization mechanisms. One may use Monitors, disable scheduling, or even disable scheduling including interrupts to ensure critical region safety. A common mechanism is to use semaphores. I have implemented this classic problem in C using semaphores and shared memory. I have used the pthread library instead of sys/sem.h. However I have not used any threads and just used the operations for sem_t type defined in semaphore.h from the pthread library. Inter process communication is done between heavyweight processes using shared memory.
The source C file can be downloaded here.
Executable generation process :
gcc procon5.c -o procon5 -lpthread
Executable file can be downloaded here. (to run : ./procon5 )
Explanation :
The master process starts by allocating shared memory for the buffer and other shared variables. The buffer is taken s an array based stack.
The functions used for this are :
shmget() : to allocate shared memory.
shmat() : to attach pointers to shared memory for performing operations on them.
The master process then initializes semaphores of type sem_t (defined in semaphore.h) using
sem_init() : used to initialize sem_t semaphore (provided in pthread.h). On providing the second argument as non zero, the semaphore can be used in IPC using shared memory.
Then it initializes the following shared variables :
len - Length of input string entered.
pr_countp - Number of items produced.
co_countp - Number of items consumed.
buff_top - Poition of top indicator in buffer.
run - Control variable, required to be set for process execution.
After determining number of producers and consumers desired from command line user input, the master process goes on to create the required number of producers and consumers using the fork() call in a loop. The return value from the fork call is used to control program flow beyond this instruction. On returning a non‐zero, positive value the process continues as parent and the pid is stored in an array of children_pids. On returning 0, a produce() or consume() procedure is called based on previous user input. The produce() and consume() procedures do not start actual execution at this time.
After all the producer and consumer processes have been created , the shared variable ‘run’ is set to 1, in order to start actual execution of processes.
The master process then waits for all the child processes to terminate using waitpid().
Finally, the space for shared memory & semaphores is deallocated and the pointers are detached, using shmdt(),shmctl() & sem_destroy().
Semaphores –
Mutex – For mutual exclusion while performing actual operation on shared memory.
Empty – For counting number of empty slots and blocking consumers if 0 empty slots.
Full – For counting full slots and blocking producers when buffer is full.
Producer Process –
If (pr_countp > len)
Then Exit (all items have been produced)
Else If (buff_top < buff_size && run == 1)
Wait (empty)
Wait (mutex)
Produce item and put into buffer
Signal (mutex)
Signal (full)
Consumer Process –
If (co_countp > len)
Then Exit (all items have been consumed)
Else If (buff_top < buff_size && run == 1)
Wait (full)
Wait (mutex)
Produce item and put into buffer
Signal (mutex)
Signal (empty)
















Comments
http://www.monclerhut.com/
Write blog articles about subjects that readers will be curious about. Everyone has standard daily duties, including washing the dishes or cleaning. Most readers aren't likely to be considering studying about these chores. Of course, provided you can present a unique spin on carrying out chores, do it. Opt for topics that will make site visitors truly feel included, fascinated, or intent on studying further. The principle target of blogging is to find viewers for your website.
When commencing a blog, start out with several great-good quality posts that introduce equally you and also the topic of your blog site. This really is crucial for displaying people what your site is centered on. When creating new articles, include pictures that speak with you. If you do not like the idea of placing a snapshot of on your own up, you can use other images that translate the mission of your own weblog. Select pictures that are simple and easy reach the various meats of your respective blog's issue.
With you have received with this post you should now have a solid knowledge of operating a blog and exactly how it can help you. Be sure that you utilize all which you have acquired, and remember determination is crucial make use of your knowledge and before very long people will be eagerly waiting to view your future articles or content.
Continue to keep backlinks to your social media web pages visible and easily available to visitors, for them to maintain you. Should you regularly use social networking sites such as Facebook and Twitter to market your site, you'll produce a good name for on your own being an publisher. This will aid boost your accomplishment like a blog writer. Back links and sites such as these are a great way to arrive at a lot more people essay writing at http://www.kissmyessays.com custom essays.
Profitable running a blog begins with desire for your concept. Individuals will appreciate your site content more, should you discuss something that you have a real attention. Your potential customers will enjoy this and you will definitely connect with them much better, that can assist you create a weblog that does properly.
http://wahid311.blogspot.com" rel="nofollow" target="_blank">http://wahid311.blogspot.com
Vivian
Marks Web
www.imarksweb.net
http://asia-fashion-wh olesale.com
http://www.oyeshop.com
http://www.shopindream.com
http://www.milanoo.com/wholesale-Pajam as-Sleepwear-c1036
http://www.giftbyoccasion.com
http://www.airconditionerc hina.com
http://www.christian4sales.com
http://www.ozjewelry.com.au
http://www.airconditionerc hina.com refrigerant
http://www.giftbyoccasion.com thank you gifts
http://www.xfreesoft.com mac dvd ripper
http://www.fr-airmax.com air max chaussures
http://www.parkajackets.biz canada goose
http://www.ozjewelry.com.au women's jewellery
http://www.airconditionerc hina.com refrigerant
http://www.giftbyoccasion.com thank you gifts
http://www.xfreesoft.com mac dvd ripper
http://www.fr-airmax.com air max chaussures
http://www.parkajackets.biz canada goose
http://www.ozjewelry.com.au women's jewellery
http://www.dissertations-w riting.co.uk" rel="nofollow" target="_blank">UK Dissertation writing
India Tours Guide Offers - Vardhman Vacations, Vacation Tours India, Rajasthan Holiday Package, Rajasthan Tour Packages, Rajasthan Tourism, Rajasthan Tours, Adventure Tourism India, Taj Mahal Tour Package, India, Wildlife National Parks, Special Ladakh Tours, India Religious Tours, India Adventure Tours, Rajasthan Train Tours, Golden Triangle Tours, Heritage Tour Rajasthan, Kerala Tourism, Budget Tour Package India.
Rajasthan Luxury Tours Offering - Rajasthan Luxury Tour Package, Luxury Tours to Rajasthan, Rajasthan Tour Guide, Rajasthan Luxury Tour Packages, Luxury Tours to Rajasthan India, Rajasthan Tour Packages, Rajasthan Best Tour Packages, Best Packages Rajasthan, Rajasthan Best Packages Tour, Safari Tours of Rajasthan, Luxury Rajasthan Destinations, Rajasthan Pilgrimage Vacations, Culture of Rajasthan, Rajasthan Fairs and Festivals, Rajasthan Wildlife Vacations, Luxury Rajasthan Trains Tours.
http://www.weegadgets.com
http://www.tengzebaler.com/vertical-balers_sp vertical balers
http://www.tengzebaler.com/Horizontal-Bale rs_sp horizontal balers
http://www.shoplouisvuitto nsave.com/
These stores will also sell Uggs at low costs during discount sales. You can also find these at
online stores. Like auction sites, you should do a price comparison among the various
stores.Where to Find Very Cheap JordansAs a rule,
Wholesale Tresor Paris jewellery offers bracelets,necklaces and packings-Tresor Paris
jewellery all by hand-made with a variety of precious gemstones crystals magnetite balls and fabrics.
Cheap Hip Hop Jewelry is mainly for sale in fashion Hip Hop Chains,and Hip Hop Pendant
and Disco Ball Chain as well,with brass, cubic zirconia stones and nylon string all by hand-made.
we have a large amount of "stocklist", dont hesitate to mail us
http://www.zhongpu-caps.com/
http://www.zp-nfljerseys.com/nfl-jerseys-c-91.html
http://www.zp-nfljerseys.com/nba-jerseys-c-181.html
http://www.zp-nfljerseys.com/mlb-jerseys-c-140.html
http://www.zp-nfljerseys.com/nhl-jerseys-c-219.html
http://www.eexploria.com/producer-consum er-problem-in-c/