I am trying to write the Operating systems producer consumer problem in C. I am getting the above mentioned "Segmentation Fault". Its hard to explain everything here, so I have posted the code on github. A look would give you a better picture of what I want to ask.
Github Link: https://github.com/Shad0walker/Producer-Consumer.git
Order of my files:
prod_cons_s.c
(Main file)
sem.h (includes threads.h)
threads.h (includes q.h)
q.h (includes TCB.h)
Here is the exact problem I am facing.
I am getting this error when I try to perform a V operation and bring a sleeping process back from SemQ to RunQ. Here is the scenario for producer consumer.
I have a RunQ with Producer -> Consumer -> Consumer -> Producer -> Consumer.
- The first producer produces an item. SUCCESS
- Consumer consumes that item. SUCCESS
- This consumer calls
P(&EMPTY)
and bumps down the semaphore counter to -1. thereby deleting this consumer process from RunQ and adding it to SemQ. Context is swapped with CPU running the next process (Producer) in the RunQ. SUCCESS - Now when the next producer runs, in the end it should call
V(&EMPTY)
bumping the semaphore counter back to 0 and bringing the sleeping process in STEP 3 back to RunQ.ERROR
Here is where I am getting this error. Any help would be highly appreciated !!!