Question
Programming Language: C Please do not copy it from another chegg answer, the program runs but it's not printing out the right output! When I
Programming Language: C
Please do not copy it from another chegg answer, the program runs but it's not printing out the right output!
When I test it, it prints out "Permission Denied!" Please help to fix this code instead, do not copy from other chegg answers that don't work.
Instructions:
Write a program, thrIncr, that creates two threads, each of which executes the same function. The function, threadFunc(), executes a loop that repeatedly increments a global variable, counter, by copying counter into the local variable, loc, incrementing loc, and copying loc back to counter. (Since loc is an automatic variable allocated on the pthread stack, each thread has its own copy of this variable.) The number of iterations of the loop is determined by the command-line argument supplied to the program, or by a default value (10,000,000) if no argument is supplied.
Run the program first by specifying the value 1000 as the number of iterations of the loop. Capture the output. The second run of the program uses the default value for the number of iterations of the loop. Capture the output. Do the outputs in both the runs look correct?
To avoid the problems that can occur when threads try to update a shared variable, we must use a mutex to ensure that only one thread at a time can access the variable. Modify the program to protect the critical section by a mutex. Run the program twice again as before, first by using a value of 1000 for the number of iterations and then by using the default value. Capture both outputs. What changes in the outputs do you see after using the mutex?
---------------
Please troubleshoot and fix code if you can:
--------------
// To test the code, type: "gcc -o test thrIncr.c -lpthread". // Then, type in "./thrIncr.c 1000000". 1000000 is the number.
#include
pthread_mutex_t mutex; // Let us create a global variable to change it in threads int counter = 0;
// The function to be executed by all threads void * threadFunc(void *vargp) { int loc=0; // Store the value argument passed to this thread long loops = (long)vargp; long i; // Let us create a static variable to observe its changes //static int s = 0; for(i=0;i // Print the argument global variables //printf(" Global: %d ",++counter); } int main(int argc, char *argv[]) { int i; long loops; pthread_t tid[2]; if(argc>1) { exit(EXIT_FAILURE); } if(argc==1) { int totalLoops = atoi(argv[0]); loops = (long)totalLoops; } else { loops=10000000; } // Let us create two threads for (i = 0; i < 1; i++) { pthread_create(&(tid[i]), NULL, threadFunc, (void *)loops); printf("creating thread"); } for (i = 0; i < 1; i++) { pthread_join(tid[i],NULL); } // pthread_exit(NULL); return 0; }
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