[SOLVED] Threading Producer-Consumer C++
I dont know how to handle this Computer Science question and need guidance.
1. Consider a Producer-Consumer problem where M Producer threads run simultaneously to fill up a buffer. Then, exactly N Consumer threads will run simultaneously to take all the data out from the buffer. An example of such scenario is a file is striped into M different site. A downloader program starts M producer threads to get these chunks independently. Once M download threads (from a pool of threads) finish producing the items, the N Consumer threads simultaneously scan the data in their own way. Again, note that N out of many possible consumers succeed. Therefore, you cannot assume that only M producers and N consumers are presents. Look at the given program 1PNC.cpp(attached with semaphore.h which is needed to run with this) that works for 1 Producer and N Consumer threads. Be sure to run the program first to see how it behaves. You need to extend the program such that it works for all M producers instead of just 1. Add necessary semaphores to the program. However, you will lose points if you add unnecessary Semaphore. To keep things simple, declare the mutexes as semaphores as well. Then test your program to make sure that it is correct. In your submission directory, include a file called Q5.cpp that contains the correct program.
2. There are 3 sets of threads A, B, C. First 1 instance of A has to run, then 2 instances of B and then 1 instance of C, then the cycle repeats. This emulates a chain of producer-consumer relationship that we learned in class, but between multiple pairs of threads. Write code to run these set of threads.
Assumptions and Instructions: There are 100s of A, B, C threads trying to run. Write only the thread functions with proper wait and signal operation in terms of semaphores. You can use the necessary number of semaphores as long as you declare them in global and initialize them properly with correct values. The actual operations done by A, B and C does not really matter. Submit a separate C++ file called Q6.cpp that includes the solution.
3. Implement a Mutex using the atomic swap(variable, register) instruction in x86. Your mutex can use busy-spin. But assume that you cannot access a register directly other than using this swap instruction. (Note in some lecture, we solved this problem with direct access to the register allowed. For instance, you could simply write $register = 1. But you are not allowed to do that directly. You must do the following: int x = 1; swap (x, $reg) .