Abhinav_007 wrote
I'm sorry for not providing a full explanation. Here is what I was trying to achieve in my project:
(1) The ChickenFarm uses a pricing model to calculate the chicken price. If the new price is lower than the previous price, it emits an event and calls the event handlers in the retailers that have subscribed to the event.
(2) A Retailer evaluates the price, generates an OrderObject (consisting of multiple values), and sends the order to the Encoder to convert the order object into a plain string.
(3) The Encoder converts the object into a string.
(4) The Encoder sends the encoded string back to the caller.
(5) The Retailer sends the encoded string to one of the free cells in the MultiCellBuffer.
(6) The ChickenFarm receives the encoded string from the MultiCellBuffer and sends the string to the Decoder for decoding.
(7) The Decoder sends the OrderObject to the ChickenFarm. The decoded object must contain the same values generated by the Retailer.
(8) The ChickenFarm creates a new thread to process the order;
(9) The OrderProcessingThread processes the order, e.g., checks the credit card number and calculates the amount.
(10) The OrderProcessingThread sends a confirmation to the retailer and prints the order.
Now, the chickenOnSale(int p) method inside Retailer class is printing the Chicken price and then releasing the no. of retailers or threadcount which is 8 in this case. But I'm not able to figure out what is causing the semaphore exception.When I run the program, it executes for sometime and then semaphore exception is encountered. Please have a look again and give your inputs SA.
This is my feedback:
(1) Event is not "emitted", it is invoked. The term "emit" is reserved for generation of the code directly in memory, see System.Reflection.Emit.
(2) I don't know what exactly do you mean by "plain string", but I would strongly advise not to invent your own string-based format; base it on XML or JSON.
(3) This is called "serialization" and cannot be called "convert. Use some standard serialization, preferably Data Contract. Please see:
(4) Why sending anything back?!
(5-7) ... It looks way to convoluted/redundant.
(8) This leads to uncontrolled creation of threads. Reasonable architecture should rather use fixed number of thread (perhaps desided after reading some configuration), and, on the task of procesing some order, the orders should be queues and threads reused.
(9) The problems of exchange of the card information is not appropriately considered. This is a very serious security item, more important than all of the above.
(10) Nothing should be directly printed. The customer should get a printable message and decide what to print and what not.
You never indicated how it can be possibly related to the semaphore and what is the semaphore problem. I seems to me that you are not aware of real use of semaphores. From what I can see no, the regular
lock
statements would suffice.
Overall, I have an impression that you are not yet prepared for this project. Your architecture is not developed to the level close to the point where you can start implementation. Instead of trying to implement the whole scenario, you should probably try to prototype some techniques on separate prototype projects. Even in this case, I am not sure if you have enough skills for that. At least try to proceed very cautiously and not try to put all things together at once.
—SA