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://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/