Question

Multi-threaded programming help!!! Can anyone solve this problem? It has to be written in C, runs on Linux.

his time you need to rewrite a multithreaded Pthread program that works with sleep() or pthread_join() in order to print out

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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

pthread_mutex_t lock;

struct process
{
   char *processName;
   int fibonacciNumber;
};

void *printShare(void *p)
{
   pthread_mutex_lock(&lock);

   struct process pro = *(struct process*)p;

   //Following code will be responsible for printing the Fibonacci sequence
   int a = 1, b = 1, temp;
   printf("%d %d ", a, b);
   while(1)
   {
       temp = b;
       b = a + b;
       a = temp;
       if(b > pro.fibonacciNumber)
       {
           break;
       }
       printf("%d ", b);
   }
   printf("\n");
   pthread_mutex_unlock(&lock);
   pthread_exit(NULL);
}

int main(int argc, char* argv[])
{
   // Check if arguments aren't provided
   if(argc < 2)
   {
       printf("Usage: %s threadName1 fibonacciNumber1 threadName2 fibonacciNumber2....\n", argv[0]);
       exit(1);
   }
   // Check if arguments aren't provided in pair i.e. Thread name and the Fibonacci number
   if(argc % 2 == 0)
   {
       printf("Usage: %s threadName1 fibonacciNumber1 threadName2 fibonacciNumber2....\n", argv[0]);
       exit(1);
   }

   if((pthread_mutex_init(&lock, NULL)) != 0)
   {
       printf("Mutex init failed. Exiting");
       exit(1);
   }

   int numberOfThreads = (argc-1) / 2;
   struct process p[numberOfThreads];

   int j = 1;
   for(int i = 0; i < numberOfThreads; i++)
   {
       p[i].processName = strdup(argv[j]);
       p[i].fibonacciNumber = atoi(argv[j+1]);
       j += 2;
   }

   pthread_t threads[numberOfThreads];
   int rc;
  

   // Following loop creates multiple threads
   for(int i = 0; i < numberOfThreads; i++)
   {
       printf("In main: creating thread %s\n", p[i].processName);
       rc = pthread_create(&threads[i], NULL, printShare, (void*) &p[i]);
       if (rc)
      {
           printf("Error, return code: %d", rc);
           exit(1);
       }
       // sleep while the newly created thread finishes
       sleep(1);
   }

   pthread_mutex_destroy(&lock);
   pthread_exit(NULL);
   return 0;
}

Add a comment
Know the answer?
Add Answer to:
Multi-threaded programming help!!! Can anyone solve this problem? It has to be written in C, runs...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Can someone help create this program for Linux. Part 3. IPC (InterProcess Communication) Programming In Part...

    Can someone help create this program for Linux. Part 3. IPC (InterProcess Communication) Programming In Part 3, you are asked to develop a multithreaded program and a Makefile to automate the compilation on Linux platform. This assignment assists you for better understanding of processes and threads management, multithreaded programming, and enhancing programming skills and experience with programming on a Unix-like environment. You are asked to implement a multithreaded producer-consumer problem with PThreads library in this programming assignment. The producer-consumer is...

  • Description In this homework, you are asked to implement a multithreaded program that will allow ...

    Description In this homework, you are asked to implement a multithreaded program that will allow us to measure the performance (i.e, CPU utilization, Throughput, Turnaround time, and Waiting time in Ready Queue) of the four basic CPU scheduling algorithms (namely, FIFO, SJE PR, and RR). Your program will be emulating/simulating the processes whose priority, sequence of CPU burst time(ms) and I'O burst time(ms) will be given in an input file. Assume that all scheduling algorithms except RR will be non-preemptive,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT