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

For this assignment you will create a group of threads and then try to coax them

ID: 3914574 • Letter: F

Question

For this assignment you will create a group of threads and then try to coax them into producing as many race conditions as possible. First, your program should declare a single shared integer variable (i.e. a global variable) with initial value zero; then your program should spawn off the number of threads specified in the source file using the MAXTHREADS defined constant. We will use a for-loop to create these threads. We will designate threads created on even numbered iterations as adders and threads created on odd iterations as subtractors.

Adder Threads: (Adding threads WILL have their own thread function)

Adder threads will execute the following steps:

1. Read global value and store it into temp

2. add 1 to the temp value

3. store temp value into global variable

4. print the following to the console:

“Current Value written to Global Variables by thread : tid is val”

Were tid is the thread id and val is the value of the temporary variable AFTER adding 1 to it.

5. repeats steps 1 through 4 for MAXITERATIONS (this is a defined constant in the given source code).

Subtractor Threads: (Subtracting threads WILL have their own thread function)

Subtractor threads will execute the following steps:

1. Read global value and store it into temp

2. subtract 1 from the temp value

3. store temp value into global variable

4. print the following to the console:

“Current Value written to Global Variables by thread : tid is val”

Were tid is the thread id and val is the value of the temporary variable AFTER subtracting 1 from it.

5. repeats steps 1 through 4 for MAXITERATIONS (this is a defined constant in the given source code).

Once all threads have finished, please print the value in main in the following way:

“Final Value of Shared Variable : val”

• Where val is the value stored in the shared variable.

Since these threads are unsynchronized, there is a possibility of race conditions occurring, depending on the actual interleaved execution order of the various threads. Insert nanosleep() commands into your thread code to induce as many race conditions as possible by forcing an (in)appropriate interleaved execution sequence. A timespec struct has been given to you. You can change the values in the struct. The first value is the amount of time to sleep in seconds and the second value is the amount of time to sleep in nanoseconds. Modify these values to try to get the final value the furthest away from 0 zero as possible in either the negative or positive direction. As your program executes, it will produce a trace of output from CSC 415 Operating Systems Principles Summer 2018, Anthony J Souza

each thread showing the actual interleaved order of execution – Where you place the nanosleep() call will affect the amount of race conditions that occur and how much your print statements are interleaved. Implement your solution using Pthreads on Linux

Explanation / Answer

CODE

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

#define MAXTHREADS 10 // maximum number of threads
#define MAXITERATIONS 100 // Max iterations per thread

// shared variable.
int globalValue = 0;

// adder thread
void* addThread(void *tid) {

   int threadId = *((int*)tid);
   int i;
   // timespec for delay
   struct timespec ts, ts2;
   ts.tv_sec = 0;
   ts.tv_nsec = 500000;// change time accordingly. not given in question.
      
   for(i = 0; i < MAXITERATIONS; i++) {
       // sleeping for some time
       // Not sure where to keep the delay.
       nanosleep(&ts, &ts2);
       // reading global value.
       int temp = globalValue;
       // adding 1 to temp
       temp += 1;
       // writing global value.
       globalValue = temp;
       printf("Current Value written to Global Variables by thread : %d is %d ",threadId, globalValue);
   }

   return NULL;
}

void* subThread(void *tid) {

   int threadId = *((int*)tid);
   int i;
   struct timespec ts, ts2;
   ts.tv_sec = 0;
   ts.tv_nsec = 500;
   for(i = 0; i < MAXITERATIONS; i++) {
       nanosleep(&ts, &ts2);
       // reading global value
       int temp = globalValue;
       // subtracting 1
       temp -= 1;
       // writing global value
       globalValue = temp;
       printf("Current Value written to Global Variables by thread : %d is %d ",threadId, globalValue);
   }
   return NULL;
}

int main() {

   int i;
   int err;
   // array for threads.
   pthread_t threads[MAXTHREADS+1];

   // create the given number of threads
   for(i = 1; i <= MAXTHREADS; i++) {

       // even thread should be adder thread.
       if(i%2 == 0) {
           err = pthread_create(&threads[i], NULL, addThread, (void*)&i);
           if(err){
               printf("ERROR creating THREADS, return code %d ",err);
               exit(-1);
           }
       }
       // odd thread should be subtract thread.
       else {
           err = pthread_create(&threads[i], NULL, subThread, (void*)&i);
           if(err){
               printf("ERROR creating THREADS, return code %d ",err);
               exit(-1);
           }
       }
   }

   // Waiting for all threads to finish.
   for(i = 1; i <= MAXTHREADS; i++) {
       pthread_join(threads[i], NULL);
   }

   // printing the final global value.
   printf("Final Value of Shared Variable : %d ",globalValue);

   return 0;
}

SAMPLE OUTPUT (Few lines of code)

Current Value written to Global Variables by thread : 5 is -28
Current Value written to Global Variables by thread : 7 is -24
Current Value written to Global Variables by thread : 9 is -23
Current Value written to Global Variables by thread : 1 is -22
Current Value written to Global Variables by thread : 3 is -21
Current Value written to Global Variables by thread : 7 is -20
Current Value written to Global Variables by thread : 9 is -19
Current Value written to Global Variables by thread : 1 is -18
Current Value written to Global Variables by thread : 7 is -17
Current Value written to Global Variables by thread : 7 is -16
Final Value of Shared Variable : -16

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote