
Write a
integer array in parallel using 4 threads. The program must divide the work between 4
threads which run simultaneously. For simplicity, you can assume that the size of the
array is 100. Note that the integer array must be declared as a global data structure.
Initially code your solution so that the sum of elements is maintained in a global shared
variable. Each thread modifies the same shared variable as it sums up elements from
the array. Use a suitable synchronization primitive (mutex) to ensure safe access to the
global variable. (A sample code of Mutex is attached for your reference)

Trending nowThis is a popular solution!
Step by stepSolved in 4 steps with 3 images

- Write a c++ code that implement a loop that accesses a number of different locations in an array over-and-over again, and see how the average access time changes as the array size increases, from a size small enough to fit in L1 cache to a size too large to fit in L2 (or L3) cache. Try to estimate the sizes of your caches.arrow_forwardWhich of the following statement or statements are true ? Please check all that apply. Both the linkedlist and array size needs to be pre-decided Arrays have better cache locality that can make them better in terms of performance. It's easier to insert and delete elements from array than from the linkedlist Data type of the linked list doesnot need to be same, whereas in array, it must be the same memory can be allocated at runtime both for linkedlist and dynamic array Which of the following statements are true? Check all that applies 5 points Accessing any element of an array has the time complexity of O(n) Searching for an element in array and linkedlist has the same time complexity Inserting or Deleting an element from a stack or queue has the same time complexity Accessing an element from a singly…arrow_forwardComplete the following code. The goal is to implement the producer-consumer problem. You are expected to extend the provided C code to synchronize the thread operations consumer() and producer() such that an underflow and overflow of the queue is prevented. You are not allowed to change the code for implementing the queue operations, that is the code between lines 25 and 126 as shown in the screenshot. You must complete the missing parts as shown in the screenshot as well as complete the missing codes of producer and consumer. #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <time.h> #include <pthread.h> #include <semaphore.h> #include <errno.h> #include <fcntl.h> #define MAX_LENGTH_CAP 100 #define INIT -127 #define UNDERFLOW (0x80 + 0x02) #define OVERFLOW 0x80 + 0x01 #define BADPTR (0x80 + 0x03) #define CONSUMER_TERMINATION_PROBABILITY 40 #define PRODUCER_TERMINATION_PROBABILITY 30 // ============= LOCKED…arrow_forward
- In the third step, you will review the Quicksort and the Insertion Sort code. You will recall from the class presentation that it was suggested that Quicksort is not very efficient when the partition sizes become very small. It was also suggested in class that most implementations of Quicksort switch to another method when the partition size gets small. Create a new version of the Quicksort code to switch to Insertion sort on partitions less than 32 data items in size. 1. Confirm, using a small data set, that your version works. 2. Run a comparison between the original Quicksort and your new version in a way like the way you did comparisons in Step 2. Use the RANDOM.TXT file for the data to test both versions - with at least 50000 items of data in your input file.arrow_forwardModify and run the program shown below in the following way. There is an array of 20 elements defined in the program. The elements of the array are: [20 18 16 14 12 10 8 6 4 2 -20 -18 -16 -14 -12 -10 -8 -6 -4 -2]. Thread 1 adds the first five elements [20, 18, 16, 14, 12], Thread 2 adds the next five elements [10, 8, 6, 4, 2], ..., Thread 4 adds the last five elements [-10 -8 -6 -4 -2]. Finally, the sum of all the 20 elements is printed by the main program. include <pthread.h> #include <stdio.h> #include <stdlib.h> #define NUM_THREADS 3 int counter=1; void *PrintHello(void *threadid) { counter = 2*counter+ (int) threadid; printf("\n Thread Id: %d Counter: %d\n", threadid, counter); pthread_exit(NULL); } int main(int argc, char *argv[]) { pthread_t threads[NUM_THREADS]; int rc, t; for(t=0;t<NUM_THREADS;t++){ printf("Creating thread %d\n", t); rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t); if (rc){ printf("ERROR; return…arrow_forwardMulti-tasking can not be achieved with a single processor machine. True False 2. Async/Await is best for network bound operations while multi-threading and parallel programming is best for CPU-bound operations. True False 3. The following is a characteristic of an async method:The name of an async method, by convention, ends with an "Async" suffix. True False 4. Using asynchronous code for network bound operations can speed up the time needed to contact the server and get the data back. True False 5. Asynchronous programming has been there for a long time but has tremendously been improved by the simplified approach of async programming in C# 5 through the introduction of: The Task class True Falsearrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education





