Transcript of "Producer consumer-problem-in-c-using-semaphores-and-shared-memory (1)"
Producer Consumer Problem in C using Semaphores and Shared MemoryWritten by Abhishek Ghosh The classic bounded-buffer problem can be implemented using a variety of synchronizationmechanisms. One may use Monitors, disable scheduling, or even disable scheduling includinginterrupts to ensure critical region safety. A common mechanism is to use semaphores. I haveimplemented this classic problem in C using semaphores and shared memory. I have used thepthread library instead of sys/sem.h. However I have not used any threads and just used theoperations for sem_t type defined in semaphore.h from the pthread library. Inter processcommunication is done between heavyweight processes using shared memory.The source C file can be downloaded here .Executable generation process :gcc procon5.c -o procon5 -lpthreadExecutable file can be downloaded here . (to run : ./procon5 )Explanation :The master process starts by allocating sharedmemory for the buffer and other sharedvariables. The buffer is taken s an array basedstack.The functions used for this are :shmget() : to allocate shared memory.shmat() : to attach pointers to shared memory 1/6
Producer Consumer Problem in C using Semaphores and Shared MemoryWritten by Abhishek Ghoshfor performing operations on them.The master process then initializes semaphoresof type sem_t (defined in semaphore.h) usingsem_init() : used to initialize sem_t semaphore(provided in pthread.h). On providing thesecond argument as non zero, the semaphorecan 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 forprocess execution.After determining number of producers andconsumers desired from command line userinput, the master process goes on to create the 2/6
Producer Consumer Problem in C using Semaphores and Shared MemoryWritten by Abhishek Ghoshrequired number of producers and consumersusing the fork() call in a loop. The return valuefrom the fork call is used to control programflow beyond this instruction. On returning anon‐zero, positive value the process continuesas parent and the pid is stored in an array ofchildren_pids. On returning 0, a produce() orconsume() procedure is called based onprevious user input. The produce() andconsume() procedures do not start actualexecution at this time.After all the producer and consumer processeshave been created , the shared variable ‘run’ isset to 1, in order to start actual execution ofprocesses.The master process then waits for all the childprocesses to terminate using waitpid().Finally, the space for shared memory &semaphores is deallocated and the pointers aredetached, using shmdt(),shmctl() & 3/6
Producer Consumer Problem in C using Semaphores and Shared MemoryWritten by Abhishek Ghoshsem_destroy().Semaphores –Mutex – For mutual exclusion whileperforming actual operation on sharedmemory.Empty – For counting number ofempty slots and blocking consumers if0 empty slots.Full – For counting full slots andblocking producers when buffer is full.Producer Process –If (pr_countp > len)Then Exit (all items have been 4/6
Producer Consumer Problem in C using Semaphores and Shared MemoryWritten by Abhishek Ghoshproduced)Else If (buff_top Wait (empty)Wait (mutex)Produce item and put intobufferSignal (mutex)Signal (full)Consumer Process –If (co_countp > len)Then Exit (all items havebeen consumed)Else If (buff_top Wait (full) 5/6
Producer Consumer Problem in C using Semaphores and Shared MemoryWritten by Abhishek GhoshWait (mutex)Produce item and put intobufferSignal (mutex)Signal (empty) 6/6