Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This second assignment focus is on threads and their synchronization. pthreads o

ID: 3586281 • Letter: T

Question


This second assignment focus is on threads and their synchronization. pthreads on Linux to implement your assignment. Make use of You are asked to implement 4 (four) threads that we call "providers" and a simple-main-program for 260 (two-hundreds and sixty) threads that we call "buyers". The providers threads produce different integer-numbers and put them in a queue. Design and implement a program that will handle the synchronization se of the providers' threads as well as the buyers threads. Basically the program should make sure that only one provider inserts an item (here an integer identifying/representing the item) and prints it on the screen, and also makes sure only one buyer at a time buys the integer and prints it on the screen. The other providers (threads) should sleep for some time while the thread buyer is buying. When a thread wakes up, it tries to buy an integer from the queue. Make sure thatH each and all buyers get a chance to buy an item, represented by a number. No buyer should make the other ones starve. The provider should not stop providing items (i.e. numbers representing items). Make sure that that each and all of the providers gets a chance to insert items (i.c. numbers) whe hes rav Your program should have one command-line argument which specifies the bues notification to the provider threads to start providing integers and then notifies tite integers number N of buyer threads. When the program starts, it creates N+2 threads which place themselves in a thread pool. The main program issues the initial the main-program to start doing its job as described above by making sure that the N buyer threads have a chance to buy items (represented by numbers). 2. After keeping a copy of program-1, from part-1 above (of course that will be needed in the report for part-1), modify the program of part-1 and run it in the case of one single provider and 6 buyers. Explain in your report what will be one main significant change to your code to make it efficient, if any? Note: For extra credits you are encouraged to be creative. Crical You haue too Sema phaes Sect ion for I a need a mutex tor each location acces

Explanation / Answer

// Only part1 has been answered

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <time.h>
#include <unistd.h>

#define MAX_QUEUE_LENGTH    1000 // initially our queue will store a 1000 elements, to change, just change this #define
// construct the queue datastructure
struct Queue
{
    //
    int data[MAX_QUEUE_LENGTH];
    //
    int front;
    //
    int rear;
    //
}typedef Queue;
//

Queue providerQueue = {{}, -1, -1}; // create one queue type variable and initialise front and rear to -1 as queue is empty initially
//
//providerQueue.front = -1;
//providerQueue.front = -1; // initialize before start
//
//providerQueue.rear = -1; // initialize before start
//
pthread_mutex_t provider_mutex = PTHREAD_MUTEX_INITIALIZER; // create a mutex to be used by provider threads to regulate access to shared resource
//
// shared function that writes the random integer produced my provider thread functions to the queue, returns 0 for success, -1 for failure
int writeToQueue(int data)
{
    //
    if(providerQueue.rear == MAX_QUEUE_LENGTH-1)
    {
        //
        printf("Error : Queue full, cannot write ");
        //
        return -1;
    }
    //
    else
    {
        //
        providerQueue.rear++; // move rear to the next empty index
        //
        providerQueue.data[providerQueue.rear] = data;
        //
        printf("Data written to queue : %d ", data);
        //
        return 0;
    }


}
//
// thread function that is invoked when a thread is created (provider thread)
void* provider_thread_function(void* args)
{
    //
    int queueData, retVal;
    //
    while(1)
    {
        //
        queueData = rand(); // generate a random number to insert into the queue
        //
        pthread_mutex_lock(&provider_mutex); // acquire the mutex
        //
        retVal = writeToQueue(queueData); // call the shared function
        //
        pthread_mutex_unlock(&provider_mutex); // release the mutex for other threads once this is done
        //
        if (retVal == 0)
            printf("I am thread %x, data written : %d ", (int)pthread_self(), queueData); // print the thread id
        //
        else
            printf("I am thread %x, data writing failed ", (int)pthread_self());
        //
        usleep(10000);// sleep for 10ms
    }
}
//
//
int buyFromQueue()
{
    //
    if(providerQueue.rear == -1)
    {
        //
        printf("Error : queue empty ");
        //
        return -1;
    }
    //
    else
    {
        //
        providerQueue.front++; // advance front to the first available element
        //
        return providerQueue.data[providerQueue.front]; // return the element
    }
}
//
// thread function for buyer threads
void* buyer_thread_function(void* args)
{
    //
    int data;
    //
    while(1)
    {
        //
        pthread_mutex_lock(&provider_mutex); // acquire the same mutex as the shared resource is same for both provider and buyer
        //
        data = buyFromQueue();
        //
        if (data != -1)
            printf("I am buyer thread %x, data : %d ", (int)pthread_self(), data);
        //
        else
            printf("I am buyer thread %x, queue empty ", (int)pthread_self());
        //
        pthread_mutex_unlock(&provider_mutex); // release the mutex
        //
        usleep(100000); // sleep for 100ms
        //
    }
}

int main(int argc, char *argv[])
{
    //
    pthread_t provider_thread_id[4]; // array to store provider thread ids
    //
    int i, N;
    //
    // get the number of buyers from the commandline argument
    N = atoi(argv[1]);
    //
    pthread_t buyer_thread_id[N];
    //
    // create and start the provider threads
    for(i = 0; i < 4; i++)
    {
        //
        pthread_create(&provider_thread_id[i], NULL, provider_thread_function, NULL);
    }
    //
    // create and start the buyer threads
    for(i = 0; i < N; i++)
    {
        //
        pthread_create(&buyer_thread_id[i], NULL, buyer_thread_function, NULL);
    }
    //
    // wait only for the first thread to return, may do for all using a loop
    pthread_join(provider_thread_id[0], NULL);
    //
    pthread_join(buyer_thread_id[0], NULL);

    //printf("Hello world! ");
    return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote