Answered step by step
Verified Expert Solution
Question
1 Approved Answer
URGENT! can anyone please solve this? I need it for this week. We have to complete the given code where it says code needed. This
URGENT! can anyone please solve this? I need it for this week. We have to complete the given code where it says code needed. This is in c. This is for Linux. will upvote.
pthread.c // *********************************************************************** // Thread function increments global int counter, but done incorrectly // *********************************************************************** #include#include #define Max 10 #define Limit 1000000 int counter = 0; // function that each thread will execute void *increment (void *arg) { int j, id = (intptr_t) arg; printf ("Thread %d started! ", id); for (j=0; j 5 Thread Creation Complete the code "pthread.c posted on the web page given above. Code segments should be added in pthread.c for thread creation and thread join at places marked "CODE NEEDED. Check the comments and see what are expected. After finishing editing, compile and run the program. During program execution, you may notice that the tcounter value is not summed correctly. You can lower the value Limit to something like 10 and see what happens. Try to figure out the cause for the problem and explain how it happens in a text file "pthread.problem. To make your explanation clear, you should give a very simple example of execution sequence of the instructions in multiple threads to illustrate the issue. You may want to consider only two threads and a very small Max value to simplify the example execution sequence. For example: Tl: load a: ... (describe what happens) T1: add b: T1: add c: Switch out Ti, switch in T2 T2: load d: ... . . To help with figuring out the problem, check the slides about the concepts of shared address space for multiple threads and consider thread switch at the instruction level. Note that thread.problem is a very important part in grading Task 5 of the project. In the thread function, you need to pay attention to how the variable id is assigned within the thread function and assign the input argument to the thread function in pthread_create() accordingly. Note that the local variables in the thread are thread safe, including the input parameter to the thread function. But the variables that are external to the thread function and shared by the threads are not thread safe. You should explore different ways to pass the input argument to the thread function and correspondingly change the way to assign the value for id in the thread function and, then, you can observe the program behaviors and try to understand why so. If you cannot get the input argument passed correctly, you still can observe the problem discussed above, finish the problem analysis in thread.problem document, and turn in your project
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started