Question

The goal of this assignment is to write a multithreaded program (Task3.c) that explores synchronization challenge 1. Assume t
0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
int NextID = 1, total1 = 0, total2 = 0, total3=0, total4 = 0, total5 =0;

void *myThreadFun(void *vargp)
{
int myid = (int)vargp;
if(NextID == myid)
{
   printf("My Turn %d\n", myid);
   switch(NextID)
   {
       case 1: total1++; break;
       case 2: total2++; break;
       case 3: total3++; break;
       case 4: total4++; break;
       case 5: total5++; break;
   }
   NextID++;
   if(NextID == 6)
       NextID = 1;
}
else
{
   printf("Not My Turn %d\n", myid);
}
return NULL;
}
  
int main()
{
  
pthread_t tid1;
pthread_t tid2;
pthread_t tid3;
pthread_t tid4;
pthread_t tid5;
printf("Starting...\n");
while(total1 <20 || total2 < 20 || total3<20 || total4<20 || total5<20)
{
   pthread_create(&tid1, NULL, myThreadFun, (void*)1);
   pthread_create(&tid2, NULL, myThreadFun, (void*)2);
       pthread_create(&tid3, NULL, myThreadFun, (void*)3);
       pthread_create(&tid4, NULL, myThreadFun, (void*)4);
       pthread_create(&tid5, NULL, myThreadFun, (void*)5);
}
printf("Finishing..\n");
exit(0);
}
OUTPUT:

Starting...

Not My Turn 2

Not My Turn 3

Not My Turn 4

Not My Turn 5

My Turn 1

My Turn 2

My Turn 3

My Turn 4

My Turn 5

My Turn 1

Not My Turn 5

Not My Turn 4

Not My Turn 3

Not My Turn 1

My Turn 2

Not My Turn 2

My Turn 3

My Turn 4

My Turn 5

My Turn 1

My Turn 5

My Turn 1

Not My Turn 5

Not My Turn 1

My Turn 2

My Turn 3

My Turn 4

My Turn 5

My Turn 1

My Turn 2

My Turn 3

My Turn 4

Not My Turn 2

Not My Turn 3

Not My Turn 4

My Turn 5

My Turn 1

Not My Turn 4

Not My Turn 5

Not My Turn 1

My Turn 2

My Turn 3

My Turn 4

My Turn 5

My Turn 1

My Turn 2

My Turn 3

Not My Turn 3

My Turn 4

My Turn 5

My Turn 1

My Turn 2

Not My Turn 2

My Turn 3

My Turn 4

My Turn 5

My Turn 1

My Turn 2

My Turn 3

My Turn 4

My Turn 5

My Turn 1

Not My Turn 1

My Turn 2

My Turn 3

My Turn 4

My Turn 5

Not My Turn 5

My Turn 1

My Turn 2

My Turn 3

My Turn 4

My Turn 5

My Turn 1

My Turn 2

My Turn 3

My Turn 4

My Turn 5

My Turn 1

My Turn 2

My Turn 3

My Turn 4

My Turn 5

My Turn 1

My Turn 2

My Turn 3

My Turn 4

My Turn 5

My Turn 1

My Turn 2

My Turn 3

Not My Turn 2

Not My Turn 5

Not My Turn 1

Not My Turn 2

Not My Turn 3

My Turn 4

Not My Turn 2

Not My Turn 3

Not My Turn 4

My Turn 5

My Turn 1

My Turn 2

My Turn 3

My Turn 4

My Turn 5

Not My Turn 2

Not My Turn 3

Not My Turn 4

Not My Turn 5

My Turn 1

My Turn 2

Not My Turn 5

Not My Turn 1

Not My Turn 2

My Turn 3

My Turn 4

My Turn 5

My Turn 1

My Turn 2

My Turn 3

Number of times Thread1 missed the turn: 13

Number of times Thread2 missed the turn: 13

Number of times Thread3 missed the turn: 5

Number of times Thread4 missed the turn: 6

Number of times Thread5 missed the turn: 6

Finishing.........

Add a comment
Know the answer?
Add Answer to:
The goal of this assignment is to write a multithreaded program (Task3.c) that explores synchronization challenge 1. Assume that we have a shared variable CurrentiD. This is initialized to 1 at t...
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
  • C program: 1: Write a program that uses 5 threads. Initialize a shared variable with a...

    C program: 1: Write a program that uses 5 threads. Initialize a shared variable with a value of 100. Each thread must add its Thread ID (tid) to the shared variable. Once a thread has done the addition, print the ID of the thread. [Note: thread does the printing] Output the value of the shared variable once all threads have finished incrementing it. [Note: main code does the printing] Provide a text file with your comments on the results of...

  • Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the ...

    Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the merge sort algorithm. The basic steps of merge sort are: 1) divide a collection of items into two lists of equal size, 2) use merge sort to separately sort each of the two lists, and 3) combine the two sorted lists into one sorted list. Of course, if the collection of items is just asingle item then merge sort doesn’t need to perform the three steps,...

  • 1. The first task in this assignment creates the pid manager whose implementation can simply be...

    1. The first task in this assignment creates the pid manager whose implementation can simply be a single class. Of course, you can create any other classes you might need to implement the pid manager. You may use any data structure of your choice to represent the availability of process identifiers. One strategy adopts Linux’s approach of a bitmap in which a value of 0 at position i indicates that a process id of value i is available and a...

  • 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,...

  • I have a multithreaded java sorting program that works as follows: 1. A list of double...

    I have a multithreaded java sorting program that works as follows: 1. A list of double values is divided into two smaller lists of equal size 2. Two separate threads (which we will term sorting threads) sort each sublist using a sorting algorithm of your choice 3. The two sublists are then merged by a third thread merging thread that merges the two sublists into a single sorted list. SIMPLE EXECUTION >java SortParallel 1000 Sorting is done in 8.172561ms when...

  • How do I do this C++ in a Unix Environment assignment Given dot1m.c 1. The program (dot1m.c) uses...

    How do I do this C++ in a Unix Environment assignment Given dot1m.c 1. The program (dot1m.c) uses mutex to lock and unlock the shared resource (dotstr.sum) for access control as shown below. pthread_mutex_lock (&mutexsum); dotstr.sum += mysum; printf("Thread %ld did %d to %d: mysum=%f global sum=%f\n", offset,start,end,mysum,dotstr.sum); pthread_mutex_unlock (&mutexsum); 2. Modify dot1m.c program to use reader-writer lock (instead of mutex).      Replace the codes (for mutex) by the codes (for reader-writer lock).            To initialize reader-writer lock, pthread_rwlock_initializer.            At the end,...

  • I need help with a C++ assignment: Write a program containing the following: 1. Variable Definitions...

    I need help with a C++ assignment: Write a program containing the following: 1. Variable Definitions only as (DieRoll, Guess, cnt1, cnt2) followed by this statement: srand((unsigned int)time (NULL)); which will give the random number generator a random starting point. Note: srand and rand require the TIME.H (or iomanip) cnt1 and cnt2 will be used in Chapter 5 drop box as counters for loops. Do NOT create additional variables. Points will be taken off for any additional variable creation. 2....

  • Assignment Overview In Part 1 of this assignment, you will write a main program and several...

    Assignment Overview In Part 1 of this assignment, you will write a main program and several classes to create and print a small database of baseball player data. The assignment has been split into two parts to encourage you to code your program in an incremental fashion, a technique that will be increasingly important as the semester goes on. Purpose This assignment reviews object-oriented programming concepts such as classes, methods, constructors, accessor methods, and access modifiers. It makes use of...

  • For this c++ assignment, Overview write a program that will process two sets of numeric information....

    For this c++ assignment, Overview write a program that will process two sets of numeric information. The information will be needed for later processing, so it will be stored in two arrays that will be displayed, sorted, and displayed (again). One set of numeric information will be read from a file while the other will be randomly generated. The arrays that will be used in the assignment should be declared to hold a maximum of 50 double or float elements....

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