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

Write a program whose main function creates a second thread and then use a globa

ID: 3587570 • Letter: W

Question

Write a program whose main function creates a second thread and then use a global variable to have the two threads ( main is always the starting point of the first thread, no?) communicate with each other. In main, after the (successful) call to pthread_create, have main prompt the user for a non-zero integer and store that non-zero value in the global variable so that the second thread can see it (since all threads of a process share the same heap and global sections). Then have main spin while the global variable is not zero — i.e., until something external to the main thread (like the second thread) sets the global variable back to zero. Spin? A thread is said to spin when it deliberately enters an apparently infinite loop until the hardware or some other process or thread, executing concurrently, changes the condition so that the spinning thread can exit its loop (which therefore wasn't really infinite after all, but it looks like it could go on forever, and in the absence of concurrency, it could and would). In the second thread, after it prints out its TID, have the second thread spin while the global varable is 0 thus "waiting" (technically, "busy waiting") to receive the non-zero value from the main thread. Once the second thread stops spinning, have it print out the value that caused it to exit from the loop and then set the global variable back to 0 so the main thread can exit itsspin and terminate. This busy-waiting is, of course a very inefficient method of synchronizing our concurrent threads. Later we'll look at better ways. Anyway, here's a sample printout, below.


Main: PID is 1915
Main: Successfully created a thread with TID of 1101519168Al
Main: Enter a non-zero integer so that the other thread can get out of its spin loop:
   Second thread: My TID is 1101519168Al, still in process #1915;
                    I'm about to spin until main enters a non-zero value into the shared variable.
56
   Second thread: Main set the shared variable to 56; I'll set it back to zero and then I'm done.
Main: Second thread has re-zeroed the shared variable, so I'm done.

Explanation / Answer

#include<pthread.h>

#include<stdlib.h>

#include<unistd.h>

//global variable

int number;

void* restValue(void *arg)

{

unsigned long i = 0;

pthread_t id = pthread_self();

printf("Thread id : %u ",id);

//reset number to zero

printf("Value of global variable number in thread = %d ",number);

printf("Thread resets gloal variable and exiting... ");

number = 0;

return NULL;

}

int main(void)

{

int i = 0;

int err;

pthread_t thread_id;

//now ask user for non zero number

printf("Enter non zero nmber: ");

scanf("%d",&number);

//create thread

err = pthread_create(&thread_id, NULL, &restValue, NULL);

if (err != 0)

printf(" can't create thread :[%s]", strerror(err));

else

printf(" Thread created successfully ");

pthread_join(thread_id, NULL);

//now

while(1)

{

printf("Main loop enters spin ");

if(number == 0)

{

printf("Main thread coming out of spin ");

break;

}

}

return 0;

}

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

//output

Enter non zero nmber:

Thread created successfully

Thread id : 4128732928

Value of global variable number in thread = 5

Thread resets gloal variable and exiting...

Main loop enters spin

Main thread coming out of spin

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