/Producer-Consumer-Problem

Semaphore Solution to the classic Producer-Consumer Problem, solved inside the linux kernel.

Primary LanguageCMIT LicenseMIT

Producer consumer Problem in Kernel Space

Testing the Modifications


Apply the Patches to the stock kernel
Run the makefile given
run the command

./p and ./c in different terminals

Kill the both the processes and run

sudo dmesg -c

p is the producer which Enqueues in the queue and c is the consumer which dequeues from the queue.


Modifying the Kernel

syscall_64.tbl

Added the name of syscalls writer and reader and their syscall numbers

sys.c

Added semaphore headerfiles

Added the definition of the syscalls writer and reader.

Added Global Variables front, rear, semaphores, circQueue pointer, size of queue.

writer

This is used by the producer to enqueue elements into the queue. 

reader

This is used by the consumer to dequeue elements from the queue

Use of Semaphores

Semaphore s1 = N : Number of empty slots in buffer

Semaphore s2 = 0 : Number of slots filled

Semaphore s3 : Mutex, used to provide mutual exclusion

Random Values in Producer

refer to getRand() function defined in Producer.

Read an 8 byte number into a character buffer from /dev/urandom

Convert that character buffer to an unsigned long long using bitwise manipulation

References

https://shivammitra.com/c/producer-consumer-problem-in-c/
https://www.programiz.com/dsa/circular-queue
https://stackoverflow.com/questions/30720546/converting-8-byte-char-array-into-long