Question

Read given code RaceOrNot3.c and write all possible outputs of the program. Assume there will be...

Read given code RaceOrNot3.c and write all possible outputs of the program. Assume there will be no thread creation or joining failures or semaphore failures. If you believe there is only one possible output, you just need to write that output.

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

sem_t s1;
int c=0,x=0;

void *UpdateC1(void *arg)
{
   int i;
   for(i=0;i<2000000;i++)   {
       sem_wait(&s1);
       c++;   x++;
       sem_post(&s1);
   }
}

void *UpdateC2(void *arg)
{
   int i,x=0;
   for(i=0;i<2999999;i++)   {
       sem_wait(&s1);
       c++;   x++;
       sem_post(&s1);
   }
}

int main(int argc, char *argv[])
{
   int rt,i;
   pthread_t t[2];

   if(sem_init(&s1, 0, 1)==-1)//Initialize the semaphore;
   {
       fprintf(stderr,"sem_init failed. errno=%d\n",errno);
       exit(1);
   }
   rt=pthread_create( &t[0], NULL, UpdateC1, NULL);
   if( rt!=0 )
       fprintf(stderr,"Thread %d creation failed: %d\n", 0,rt);
   rt=pthread_create( &t[1], NULL, UpdateC2, NULL);
   if( rt!=0 )
       fprintf(stderr,"Thread %d creation failed: %d\n", 1,rt);

   for(i=0;i<2;i++)
   {
       rt=pthread_join(t[i], NULL);
       if( rt!=0 )
           fprintf(stderr,"Wait for thread %d failed: %d\n", i,rt);
   }

   printf ("\t%c\t%c\n",'b'-(c%2),'b'-(x%2));
   return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

There is only one possible out for this program it is:

a       b

This is because, the variable x which is global, is incremented in thread C1 only, and at the end of this thread the value of x is 2000000, as this variable is incremented by 1 for 2000000 times in the for loop. The variable x used in thread C2 is a local variable which is visible in that thread alone. Thus the variable x used in the main function in printf statement take the value of x which is updated in thread C1.

Hence x%2 is 0 as 2000000%2 is 0. Therefore 'b' - (x%2) will be 'b' - 0 which will be same as character 'b'.

Whereas the variable c is a global variable and both the threads C1 and C2 modify this variable. Thus the variable c is incremented by 1 for 2000000 times in for loop in thread C1 and it is incremented by 1 for 2999999 times in for loop in thread C2, thus in total 2000000 + 2999999 = 4999999 times incremented by 1. Therefore when both the threads finished their execution, the value of variable c is 4999999. As this variable is global, it is visible in main function with the value 4999999.

Hence c%2 is 1 as 4999999%2 is 1. Therefore 'b' - (c%2) will be 'b' - 1 which will be the character 'a'.

Thus the output:    a       b

Add a comment
Know the answer?
Add Answer to:
Read given code RaceOrNot3.c and write all possible outputs of the program. Assume there will be...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Read given code and write all possible outputs of the program. Assume there will be no...

    Read given code and write all possible outputs of the program. Assume there will be no thread creation or joining failures or mutex failures. If you believe there i only one possible output, you just need to write that output. Code: #include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <semaphore.h> #include <errno.h> sem_t s1; int c[2] = {0,1}; void *UpdateC1(void *arg) { int i; for(i=0;i<1000000;i++) { sem_wait(&s1); c[0]=(c[0]+1)%2; c[1]=(c[1]+1)%2; sem_post(&s1); } return NULL; } void *UpdateC2(void *arg) { int i; for(i=0;i<2000000;i++)...

  • Read given code RaceOrNot1.c and write all possible outputs of the program. Assume there will be...

    Read given code RaceOrNot1.c and write all possible outputs of the program. Assume there will be no thread creation or joining failures or mutex failures. If you believe there is only one possible output, you just need to write that output. #include <stdio.h> #include <stdlib.h> #include <pthread.h> pthread_mutex_t count_mutex = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t count_mutex3 = PTHREAD_MUTEX_INITIALIZER; int c[2] = {1,0}; void *UpdateC1(void *arg) {    int i;    for(i=0;i<1000000;i++)    {        pthread_mutex_lock(&count_mutex);        c[0]=(c[0]+1)%2;        c[1]=(c[1]+1)%2;        pthread_mutex_unlock(&count_mutex);    }    return NULL; } void *UpdateC2(void *arg) {    int...

  • I am supposed to write documentation and report for the code below but I am new...

    I am supposed to write documentation and report for the code below but I am new to operating system concepts I will appreciate if someone can help make a detailed comment on each line of code for better understanding. Thanks #include <pthread.h> #include <string.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <ctype.h> #define handle_error_en(en, msg) \ do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0) #define handle_error(msg) \ do { perror(msg); exit(EXIT_FAILURE); } while (0) struct thread_info...

  • so in this code, it computes the sum 1+2+....+n but i want it to compute 2*(1+2+....+n)...

    so in this code, it computes the sum 1+2+....+n but i want it to compute 2*(1+2+....+n) using semaphores implement solution to the critical section problem #include #include int sum; /* this data is shared by the thread(s) */ void *runner(void *param); /* threads call this function */ int main(int argc, char *argv[]) { pthread_t tid; /* the thread identifier */ pthread_attr_t attr; /* set of thread attributes */ if (argc != 2) { fprintf(stderr,"usage: a.out \n"); return -1; } if...

  • In c programming The Consumer Submits processing requests to the producer by supplying a file name, its location and a character. It also outputs the contents of the file provided by the producer...

    In c programming The Consumer Submits processing requests to the producer by supplying a file name, its location and a character. It also outputs the contents of the file provided by the producer to the standard output. The Producer Accepts multiple consumer requests and processes each request by creating the following four threads. The reader thread will read an input file, one line at a time. It will pass each line of input to the character thread through a queue...

  • Pleas help I need to create and array to add to my code something like this...

    Pleas help I need to create and array to add to my code something like this for (i = 0; i < NUM_THREADS; ++i) { thr_data[i].tid = i; if ((rc = pthread_create(&thr[i], NULL, thr_func, &thr_data[i]))) { fprintf(stderr, "error: pthread_create, rc: %d\n", rc); return EXIT_FAILURE; ::::::::: CODE ::::::::::::: #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <pthread.h>    int sharedVar = 0; pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; /* thread handler */ void *threadHandler(void *vargp) {    int i = 0;    pthread_mutex_lock(&mutex);   ...

  • 1. (50 pts) Write a C or C++ program A6p1.c(pp) that accepts one command line argument which is an integer...

    1. (50 pts) Write a C or C++ program A6p1.c(pp) that accepts one command line argument which is an integer n between 2 and 6 inclusive. Generate a string of 60 random upper case English characters and store them somewhere (e.g. in a char array). Use pthread to create n threads to convert the string into a complementary string ('A'<>'Z', 'B'<->'Y', 'C''X', etc). You should divide this conversion task among the n threads as evenly as possible, Print out the...

  • In C using the following 2 files to create a 3rd file that uses multiple threads...

    In C using the following 2 files to create a 3rd file that uses multiple threads to improve performance. Split the array into pieces and each piece is handled by a different thread. Use 8 threads. run and compile in linux. #include <stdio.h> #include <sys/time.h> #define BUFFER_SIZE 4000000 int countPrime=0; int numbers[BUFFER_SIZE]; int isPrime(int n) { int i; for(i=2;i<n;i++) if (n%i==0) return 0; return 1; } int main() { int i; // fill the buffer for(i=0;i<BUFFER_SIZE;i++) numbers[i] = (i+100)%100000; //...

  • Would u help me fixing this CODE With Debugging Code with GDB #include <stdio.h> #include <stdlib.h>...

    Would u help me fixing this CODE With Debugging Code with GDB #include <stdio.h> #include <stdlib.h> #define SIZE (10) typedef struct _debugLab { int i; char c; } debugLab; // Prototypes void PrintUsage(char *); void DebugOption1(void); void DebugOption2(void); int main(int argc, char **argv) { int option = 0; if (argc == 1) { PrintUsage(argv[0]); exit(0); } option = atoi(argv[1]); if (option == 1) { DebugOption1(); } else if (option == 2) { DebugOption2(); } else { PrintUsage(argv[0]); exit(0); } }...

  • Modify the programs so that each program can both receive and send messages alternatively. Note that when you run the two programs, you should run them in two different windows ( terminals). Y...

    Modify the programs so that each program can both receive and send messages alternatively. Note that when you run the two programs, you should run them in two different windows ( terminals). You should be able to send messages from one to the other and terminate them by entering "end" //msgl.cpp / Here's the receiver program. / #include #include #include #1nclude #include <stdlib.h> <stdio.h> <string.h> <errno.h> <unistd.h> #include <sys/types.h> #include <sys/ipc.h> Winclude <sys/msg.h> struct my msg st f long int...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT