Monday Sep 06
Move
Display 0 | 5 | 10 | 15 Stories

Gadget

Topics
Top Story

Waterproof LCD for Bathrooms

Waterproof LCD for Bathrooms

  Have you been wishing for a waterproof LCD TV for use in you bathroom? Good for you, Vu has launched...

PlayStation 4 Release date and Features

PlayStation 4 Release date and Features

  The next generation PlayStation may arrive sooner than we think.. maybe even in 2009! But based...

XBox 720 Release date and Features

  In an interview with the head of Microsoft’s Interactive Entertainment Business division, Peter...

Wii 2 Release date and Features

  Nintendo are selling Wii’s faster than stores can stock them and some may believe that they...

PSP 2 Release date and Features

  The PS2P should be available in 2009 - 10, to compete with the upgraded Nintendo DS. Some features...

Upcoming Game Consoles

Gadget

Move
Display 0 | 5 | 10 | 15 Stories

Software Tutorials

Topics
Top Story

How to Enable / Disable Autoformat in word 2007

How to Enable / Disable Autoformat in word 2007

  The interface of Office 2007 does does not offer an intuitive tool to enable or disable the autocorrect...

How to use old MSN Messenger without upgrading

  If you dont like the newer version of MSN Live Messenger and want to use the old MSN Messenger...

Software Tutorials

 

Move
Display 0 | 5 | 10 | 15 Stories

General

Topics
Top Story

PRAM model of Parallel Computation

PRAM model of Parallel Computation

  A PRAM consists of a control unit, global memory, and an unbounded set of processors,each with its...

The Crisis of Credit Visualized

A Vimeo video by Jonathan Jarvis attempting to visually explain the credit crisis -   The Crisis...

How to carry money while travelling

If you are planning to go abroad for any purpose, like higher studies, tourism or business purpose,...

How to call from US to india

  There are many ways to call back to india You can take a sim card from the Matrix company and...

Translation of source code to object module : The Preprocessor Compilation Process

The preposessor (We'll be talking of the C preprocessor) is a seperate program invoked by the compiler...

Virtua Kitchen - The Kitchen Operating System

What is a Synergistic Processing Element / SPE

What is a Power Processor Element / PPE

What is the Element Interconnect Bus / EIB

What is the Cell Architecture

Why use Automated Testing Tools

What is the Software Testing Life Cycle (STLC)

What is Software Testing

AudioSurf - Ride Your Music !

Ubuntu 8.10 is out!

Move
Display 0 | 5 | 10 | 15 Stories

Programming

Topics
Top Story

Code for Writing a xlsx file in java

Here is a small code to write xlsx file though java code.    You may need to include some of...

Code for Reading a xlsx file in java

Here is a small code to read xlsx file thoug java code.    You may need to include some of the...

How to read and write data from socket in java

   ServerSocket serverSocket=new ServerSocket(port);  // The below statement will wait for the...

Producer Consumer Problem in C using Semaphores and Shared Memory

  The classic bounded-buffer problem can be implemented using a variety of synchronization mechanisms....

Common Intermediate Language

Common Intermediate Language (formerly called Microsoft Intermediate Language or MSIL) is the lowest-level...

Common Type System in C#

Common Language Runtime

A Quick Introduction to C# Features

Code for updating database using hibernate framework in java

Conversion of int to byte and vice versa

Web Crawler in Python

Neural Network in Python

Programming

Move
Display 0 | 5 | 10 | 15 Stories

Graphics

Topics
Top Story
Move
Display 0 | 5 | 10 | 15 Stories

Linux

Topics
Top Story

Linux

 

Move
Display 0 | 5 | 10 | 15 Stories

Web

Topics
Top Story

What is Kosmix

  I am guessing that you looked up the term ' What is Kosmix ' on google after hearing somewhere about...

What is a meta tag

  There was a time when I didnt know of Meta tags myself. I'm glad I do now as they have really helped...

Why some search results have no description

  Before I go on to explain why some search results have no description , I woud like to state that...

What is the Facebook Bill of Rights

Have you also been wondering what the facebook bill of rights is? Here is the short answer to the popular...

The Fastest Safari Browser

  Apple has announced the public beta of Safari 4 for Macintosh and Windows PCs, which is the fastest...

Web

Move
Display 0 | 5 | 10 | 15 Stories

Games

Topics
Top Story

Games

 

Move
Display 0 | 5 | 10 | 15 Stories

Windows

Topics
Top Story

Windows

 

Producer Consumer Problem in C using Semaphores and Shared Memory

(6 votes, average: 4.33 out of 5)





 

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)


Add this page to your favorite Social Bookmarking websites
Reddit! Del.icio.us! Google! Live! Facebook! Slashdot! Technorati! StumbleUpon! Spurl! Furl! Yahoo! Squidoo! Ask! DZone! Free Joomla PHP extensions, software, information and tutorials.



Comments

avatar Learner
+3
 
 
Thanks for sharing this!
B
i
u
Quote
Code
List
List item
URL
Name *
Code   
ChronoComments by Joomla Professional Solutions
Submit Comment
Cancel
avatar AJAY KUMAR
0
 
 
nice material..
B
i
u
Quote
Code
List
List item
URL
Name *
Code   
ChronoComments by Joomla Professional Solutions
Submit Comment
Cancel
avatar bullet_theGoli
0
 
 
good
B
i
u
Quote
Code
List
List item
URL
Name *
Code   
ChronoComments by Joomla Professional Solutions
Submit Comment
Cancel
avatar Martin Velez
-1
 
 
This is wrong.
B
i
u
Quote
Code
List
List item
URL
Name *
Code   
ChronoComments by Joomla Professional Solutions
Submit Comment
Cancel
avatar Abdul Wahid Khan
0
 
 
Why this is wrong?

http://wahid311.blogspot.com" rel="nofollow" target="_blank">http://wahid311.blogspot.com
B
i
u
Quote
Code
List
List item
URL
Name *
Code   
ChronoComments by Joomla Professional Solutions
Submit Comment
Cancel
avatar vid
0
 
 
why is it wrong!?
B
i
u
Quote
Code
List
List item
URL
Name *
Code   
ChronoComments by Joomla Professional Solutions
Submit Comment
Cancel
B
i
u
Quote
Code
List
List item
URL
Name *
Code   
ChronoComments by Joomla Professional Solutions
Submit Comment

Tag Cloud

Login Form