Answer the following questions based on the given c file: #include #include #include    int c = 0; void *fnC() {     int i;     for(i=0;i<10;i++)     {  ...

70.2K

Verified Solution

Question

Programming

Answer thefollowing questions based on the given c file:

#include

#include

#include  

int c = 0;

void *fnC()

{     int i;

   for(i=0;i<10;i++)

   {   c++;

        printf(\" %d\", c);

   }      

}

int main()

{

int rt1,rt2;   pthread_t t1, t2; int trial_count = 0;

// For every trial, lets zero out the counter and run the countroutine “twice”    

// as threads that can be scheduled onto independent coresinstead of running    

// in sequence.

for (trial_count = 0; trial_count < 1000; trial_count++)

{

c = 0;

           //Create two thread with the routine pthread_create(). You canuse

           //reference materials to get definitions of what the variousparameters            

// mean.

           if((rt1=pthread_create( &t1, NULL, &fnC,NULL)))              

printf(\"Threadcreation failed: %d\n\", rt1);

          if((rt2=pthread_create( &t2, NULL, &fnC,NULL)))              

printf(\"Threadcreation failed: %d\n\", rt2);

          // Wait forboth threads to finish. The main process thread will block

          // until thethreads that were launched terminate. Once they bothfinish,          

// then the “mainthread” unblocks and continues.

         pthread_join(t1, NULL);          

pthread_join(t2,NULL);          

printf(\"\n\");

        }

   return 0;

}

Activity A, Task 1: DiscussionQuestions (10/50 points):

  1. You should have observed output lines that were NOT alwaysprinted in integer order. This is because the code as written has acritical section that is being simultaneously run by more than onethread. What lines of the sample code constitute the criticalsection? You can cut and paste those lines into your answer, or youcan type them in.  

  1. Thread safety (https://en.wikipedia.org/wiki/Thread_safety) isa concept often used when discussing subroutines or other codesegments that are written in such way that they can be “multiplyrun” as parts of multiple threads that are being scheduledconcurrently or otherwise multiprogrammed. Do you think that thefunction printf() is thread safe? Do some researchand/or run some code experiments and provide a brief, but complete,answer that explains your view on the subject. Be sure to explainwhy you hold your position on the subject, no matter what thatis.

Answer & Explanation Solved by verified expert
4.0 Ratings (646 Votes)
Working code implemented in C and    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students