Question: I need help with my assignment. Assignment 3: Synchronization with Semaphores Objective: The goal of this assignment is to get you familiar with writing multithreaded
I need help with my assignment.
Assignment 3: Synchronization with Semaphores
Objective: The goal of this assignment is to get you familiar with writing multithreaded programs where the threads need to be synchronized using semaphores. You will implement a simple version of the producer-consumer problem for this assignment.
Producer-Consumer Problem: We will deal with a simple version of producer-consumer problem. We will be focusing on the bounded buffer version of the problem. So, assume that the common buffer that the producer and the consumer share is an array of some fixed size (say 10). You shall implement the producer and consumer as two threads of the same process.
The producer will be generating x number of random numbers, where x is a command line parameter to your program. The producer will simply place the random number generated into the buffer. The producer should also write the number generated into a file called production. The producer should also write each number produced on the terminal with the prefix producer. The random number generated should be an integer between 1 and 100.
Note that random numbers can be produced in C using rand function. In order to generate a different sequence of random numbers every time you run the program, you can use the srand function to seed the random number generator with a value that will be different in each run (for example, you can seed with the current time obtained by calling the time function).
The consumer will simply pick up an item from the buffer and determine its square. The consumer should also write the number picked up and its square into a file called consumption. The consumer should also write each number consumed and its square on the terminal with the prefix consumer. The consumer should consume the items in exactly the same order in which they are produced.
The buffer array and any other variables used by both threads can be declared as global variables. Make sure to synchronize the producer thread and the consumer thread properly using semaphores. Note that x has to be much larger in comparison to buffer size to test the synchronization of the threads.
Semaphore Primer: You will use the implementation of semaphore that is supported by POSIX threads library. You will also be needing the other thread calls.
The header file semaphore.h contains definitions and operations prototypes for semaphore. Semaphore descriptions are declared global o threads that will use them as in
sem_t mutex;
A descriptor is initialized by calling the em_init function. For example, the following initializes mutex to 1
sem_init(&mutex, SHARED,1);
If SHARED is nonzero, the semaphore can be shared between processes; otherwise it can only be shared between threads in the same process. The Pthreads equivalent of the P operation is em_wait and the equivalent of the V operation is em_post. Thus, one way to protect a critical section of code is to use semaphore mutex as follows:
sem_wait (&mutex); /*P(mutex);*/
critical section;
sem_post (&mutex); /*V(mutex);*/
Warning: Do not use mutex locks and condition variables as discussed in the textbook. Instead use general semaphores in the implementation. Basically, this means your implementation should just mimic the conceptual solution given in the class and found in the textbook.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
