Question

Program 2: Thread version int i = 100; char *buffer: void *tfuc(void *noarg) { int j = 0; printf(B:1-%d, j = %d\n,1,1); pri

(e) Can the output of the thread-based program 2 ever include (meaning they appear in this order, possibly with other output
0 0
Add a comment Improve this question Transcribed image text
Answer #1

The answer is NO for the questions E F G H. The thread based programme cannot include any of the output given in the question in the given order.

Explanation for the Answer of E F G H :

Initially value of J is set to 1 in main function and value of buffer string is set to "Blue" After creating the thread B using "pthread_create()" function the value of J is printed which is 1 at this point so current output becomes like this

OUTPUT

A: j=1

After the values of I is changed to 200 and value of J is changed to 2. Then the string buffer is printed. So current output becomes:

OUTPUT

A: j=1
A: buffer=Blue

Then value of J is changed to 4 and using the pthread_join function the control is sent to "tfuc" function. There value of J is changed to 0. And values of I and J is printed twice. So current output becomes

OUTPUT

A: j=1
A: buffer=Blue
B: i=200, j=0
B: i=200, j=0

Then value of J is changed to 3 and value of string buffer is changed to "red" and the thread exits and the programme returns to main function and terminates.

So none of the given output pattern matches with the output of the programme and hence the answer is NO for all of them.

FINAL OUTPUT:- O 0 C:\Users\KIIT\Documents Untitled 1.exe A: j=1 A: buffer=Blue B: i=200, j=0 B: i=200, j=0 - - - - -- --- - - - - - -- -

- O 0 C:\Users\KIIT\Documents Untitled 1.exe A: j=1 A: buffer=Blue B: i=200, j=0 B: i=200, j=0 - - - - -- --- - - - - - -- - - - - - -- - Process exited after 2.678 seconds with return value 3221225477 Press any key to continue...

- O 0 C:\Users\KIIT\Documents Untitled 1.exe A: j=1 A: buffer=Blue B: i=200, j=0 B: i=200, j=0 - - - - -- --- - - - - - -- - - - - - -- - Process exited after 2.678 seconds with return value 3221225477 Press any key to continue...

Add a comment
Know the answer?
Add Answer to:
Program 2: Thread version int i = 100; char *buffer: void *tfuc(void *noarg) { int j...
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
  • what is the output of the following program? #include<stdio.h> #include<string.h> int main(void){ char word[20]; int i...

    what is the output of the following program? #include<stdio.h> #include<string.h> int main(void){ char word[20]; int i =0    strcpy(word, "ORGANISE"); while(word[i] !='\0'){ if(i%2 ==1) word[i] = 'C'; i++; } printf("%s",word); return 0; }

  • Please change this 2 thread program to a 4 thread program. /*compile as "gcc thread.c -pthread"...

    Please change this 2 thread program to a 4 thread program. /*compile as "gcc thread.c -pthread" */ #include <pthread.h> #include <stdio.h> #include <time.h> #define SIZE 100000000 //100 M //#define SIZE 10000 #define NUMBER_OF_TIMES 100 float a[SIZE]; typedef struct {        int start;        int end;        double sum;        int pid;    } range; void *thread_function(void *arg) //define a function that will be executed from //within a thread {    range *incoming = (range *) arg;...

  • include<stdio.h> #include<pthread.h> int i=4 ,j=10; void*childfunc(void *p) {    printf("child here.i=%d,j=%d\n",i,j);    i*=3;    j*=3;   ...

    include<stdio.h> #include<pthread.h> int i=4 ,j=10; void*childfunc(void *p) {    printf("child here.i=%d,j=%d\n",i,j);    i*=3;    j*=3;    printf("child here.i=%d,j=%d\n",i,j); } void main() {    pthread_t child1;    pthread_create(&child, Null,childfunc, Null) pthread_join(child,Null);    printf("parent here.i=%d,j=%d\n",i,j);    i*=2;    j*=2;    printf("end of program .i=%d,j=%d\n",i,j); } what is output?

  • #include<stdio.h> #include<stdio.h> int main(){ int i; //initialize array char array[10] = {“Smith”, “Owen”, “Kowalczyk”, “Glass”, “Bierling”,...

    #include<stdio.h> #include<stdio.h> int main(){ int i; //initialize array char array[10] = {“Smith”, “Owen”, “Kowalczyk”, “Glass”, “Bierling”, “Hanenburg”, “Rhoderick”, “Pearce”, “Raymond”, “Kamphuis”}; for(int i=0; i<8;i++){ for(int j=0; j<9; j++){ if(strcmp(array[j],array[j+1])>0){ char temp[20]; strcpy(temp,array[j]); strcpy(array[j],array[j+1]); strcpy(array[j+1],temp); } } } printf(“---------File Names---------\n”); for(inti=0; i<9; i++){ printf(“\t%s\n”,array[i]); } printf(-------5 Largest Files according to sorting----\n”); for(int i=0;i>=5;i--) { printf(“\t%s\n”,array[i]); } return0; } Consider the "sort" program (using with void* parameters in the bubblesort function) from the week 10 "sort void" lecture. Modify it as follows...

  • i want to fix the solution and provide an explanation why the pthread_join is not working...

    i want to fix the solution and provide an explanation why the pthread_join is not working as intended? #include <stdio.h> /* standard I/O routines */ #include <pthread.h> /* pthread functions and data structures */ void* PrintHello(void* data) { pthread_t tid = (pthread_t)data; /* data received by thread */ pthread_join(tid, NULL); /* wait for thread tid */ printf("Hello from new thread %u - got %u\n", pthread_self(), data); pthread_exit(NULL); /* terminate the thread */ } /* like any C program, program's execution...

  • Please finish the following program /** * Basic POSIX Thread (pthread) Usage 1. * * By...

    Please finish the following program /** * Basic POSIX Thread (pthread) Usage 1. * * By walking through this example you’ll learn: * - How to use pthread_create(). * - How to use pthread_exit(). * - How to use pthread_join(). * */ #include <stdio.h> #include <unistd.h> #include <pthread.h> #include <sys/wait.h> void* ninja(void* arg){ printf("Who’s there?"); fflush(stdout); pthread_exit("ninja"); } // // Expected output: // // Knock knock. // Who's there? - from ninja // Knuc...kles. // int main(int argc, char* argv[]){...

  • I must execute in C using parallel Programming techniques the following serial program: void producer_consumer(int *buffer,...

    I must execute in C using parallel Programming techniques the following serial program: void producer_consumer(int *buffer, int size, int *vec, int n) {    int i, j;    long long unsigned int sum = 0;    for(i=0;i<n;i++) {        if(i % 2 == 0) {   // PRODUCER            for(j=0;j<size;j++) {                buffer[j] = vec[i] + j*vec[i+1];            }        }        else {   // CONSUMER            for(j=0;j<size;j++) {...

  • OPERATING SYSTWM Question 31 What is the output of this C program? #include #include void main()...

    OPERATING SYSTWM Question 31 What is the output of this C program? #include #include void main() int mptr, *cptr mptr = (int*)malloc(sizeof(int)); printf("%d", "mptr); cptr = (int)calloc(sizeof(int),1); printf("%d","cptr); garbage 0 000 O garbage segmentation fault Question 8 1 pts If this program "Hello" is run from the command line as "Hello 12 3" what is the output? (char '1'is ascii value 49. char '2' is ascii value 50 char'3' is ascii value 51): #include<stdio.h> int main (int argc, char*argv[]) int...

  • 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);   ...

  • 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...

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