Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Programming Language: C Please do not copy it from another chegg answer, the pro

ID: 3849717 • Letter: P

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 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 <stdio.h>
#include <stdlib.h>
#include <pthread.h>

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<loops;i++)
{
//copy conter into local
loc=counter;
//increment local variable loc
loc++;
//copy loc to counter
loc=counter;
//increment local variable loc
loc++;
//copy loc to counter
counter=loc;
}

// 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;
}

Explanation / Answer

Your comments:

// To test the code, type: "gcc -o test thrIncr.c -lpthread".
// Then, type in "./thrIncr.c 1000000". 1000000 is the number.

After compiling code from the first comment, you are still trying the execute the Source file in the next line i.e. you should have written it as "./thrIncr 1000000", to run the executable file.

There's a lot to debug though, to get the correct output:

Here is an improved version of your code, including mutex. You still might have to work on the "logic" part, however the structure is correct, which you can use for reference:

// To test the code, type: "gcc -o test thrIncr.c -lpthread".
// Then, type in "./thrIncr 1000000". 1000000 is the number.

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

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
// 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)
{

//Acquiring Lock
pthread_mutex_lock(&mutex);


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<loops;i++)
{
//copy conter into local
loc=counter;
//increment local variable loc
loc++;
//copy loc to counter
counter=loc;
//increment local variable loc
loc++;
//copy loc to counter
counter=loc;
}

// Print the argument global variables
printf(" Global: %d ",++counter);
printf(" Loc value: %d ",loc);

//Lock Release
pthread_mutex_unlock(&mutex);
}

int main(int argc, char *argv[])
{

int i;
long loops;
pthread_t tid[2];

if(argc>1) {
exit(EXIT_FAILURE); }

if(argc==2)
{
int totalLoops = atoi(argv[0]);
loops = (long)totalLoops;
}
else {
loops=10000000; }


long *arg = malloc(sizeof(*arg));
*arg = loops;

// Let us create two threads
for (i = 0; i < 2; i++)
{
printf("creating thread ");
pthread_create(&(tid[i]), NULL, threadFunc, arg);

}

for (i = 0; i < 2; i++)
{
pthread_join(tid[i],NULL);
}

free(arg);

// pthread_exit(NULL);
return 0;
}