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

Programming Language: C Programming Instructions: Write a program, thrIncr, that

ID: 3849708 • Letter: P

Question

Programming Language: C

Programming 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?

This is my code: what's wrong with it?

----------------------------------------

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

pthread_mutex_t mutex;
int counter = 0;

int main() {

pthread(fpid, NULL, function, &iteration);
pthread.mutex_init(&mutex, NULL);

if (argc > 1) {
iteration = atoi(argv[1]); }
else {
iteration = 10000000; }
// argc is a counter argument.
// argv contains a pointer.
// Another way to write this: "iteration = (argc > 1) ? atoi(argv[1]) : 10000000;"

}

void * threadFunc(var * arg)
{

int loops = *(int *)arg;
int loc, j;

int loops *(int *) arg;
pthread_mutex_loc(&mutex);
// If mutex is inside the loop, it will slow down the program.
// That's due to too many logarithms being applied.

loc = counter;
loc = loc + 1;
counter = loc;
  
for(int i = 0; i < arg; i++) {
/* Code goes here. */
}
pthread_mutex_unlock(&mutex);
return NULL;

}

Explanation / Answer

try this one..:-

//gcc 5.4.0

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

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

// Let us create a static variable to observe its changes
//static int s = 0;
for(long i=0;i<loops;i++)
{
//copy conter into local
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;
}