a) Write a program that has two pthreads - original thread (thread A) and a new
ID: 3735459 • Letter: A
Question
a) Write a program that has two pthreads - original thread (thread A) and a new thread (thread B). After creating thread B, thread A waits for SIGINT using sigwait(). On receiving SIGINT, thread A waits for thread B to terminate and then it prints the double value returned by thread B and terminates. Thread B executes an infinite for loop in which it continuously increments (by 0.0000001 or some even small value) the value of a double variable (say x). On receiving SIGINT, thread B terminates after returning the current value of x. Thread B also prints its current life time after each 10 seconds of its life. Implement threads A and B with contentionScope "process".
b) Now change thread A and B's contentionScope to "system" and compare the value thread B returns with the value returned when it had contentionScope "process". Explain your observations.
PLEASE READ THE QUESTION AND ACTUALLY ANSWER IT. THE QUESTION IS NOT ASKING FOR MATH OPERATORS OR CREATING NODES. THE QUESTION IS ASKING TO CREATE THREADS, USE SIGINT, SIGWAIT. PLEASE DO NOT COPY AND PASTE A RESPONSE THAT IS INCORRECT, I WILL REPORT IT. THANKS.
Explanation / Answer
#include<stdio.h>
#include<pthread.h>
void* thread(void* data) //a function which is executed by the new thread
{
int i,j;
int no=*((int*)data);
for(i=0;i<10;i++)
for(j=0;j<1000;j++)
printf("%d %d",no,i);
}
pthread_exit(NULL); // to terminate the thread
}
int main(int argc,char* argv[])
{
int new_thread_id;
int a=1;
int b=2;
new_thread_id=pthreade_create(&p_thread,NULL,do_loop,(void*)&a); //to create a new thread
do_loop((void*)&b);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.