Question

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?

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

There were some errors in the program which I have corrected. The output is at the bottom.

#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(&child1, NULL, childfunc, NULL);
pthread_join(child1, 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);
}

Add a comment
Know the answer?
Add Answer to:
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;   ...
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
  • 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...

  • Program 2: Thread version int i = 100; char *buffer: void *tfuc(void *noarg) { int j...

    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); printf("B: I = %d, j = %d\n",1,1); j = 3; strcpy(buffer, "red"); Pthread exit(NULL); //print the string (show values of i, j) //print the string (show values of i,1) //copy the string "red" to buffer. int main(void) { pthread_t tid; //declaring vars int j = 1; buffer strcpy(malloc(100), "blue"); //Initialize buffer and copy the "blue" to it pthread_create(&tid,...

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

  • #include<stdio.h> int main() {       int data[10], i, j, temp;       printf("Enter 10 random number to...

    #include<stdio.h> int main() {       int data[10], i, j, temp;       printf("Enter 10 random number to sort in ascending order:\n");       for(i = 0; i < 10; i++)             scanf("%d", &data[i]);       /* Sorting process start */     ****** INSERT YOUR CODE TO COMPLETE THE PROGRAM ******       printf("After sort\n");       for(i = 0; i < 10; i++)             printf("%d\n",data[i]);       return 0; } Looking for alternative solutions for the program code.

  • 1) Compile thread2.c (remember the -lpthread flag). Run it numerous times, and notice the output usually...

    1) Compile thread2.c (remember the -lpthread flag). Run it numerous times, and notice the output usually differs. a) Why is the variable myglobal usually not 40? Be specific, be precise. b) In what special case would myglobal be 40? Be specific, be precise. #include <pthread.h> #include <stdlib.h> #include <unistd.h> #include <stdio.h> #include <fcntl.h> /* Purpose: Use 2 threads to increment myglobal exactly 40 times in total. Compile: using -pthread option */ int myglobal = 0; void *thread_function(void *arg) {   ...

  • Please explain your answers thoroughly for the following question. 1) Compile thread2.c (remember the -lpthread flag)....

    Please explain your answers thoroughly for the following question. 1) Compile thread2.c (remember the -lpthread flag). Run it numerous times, and notice the output usually differs. a) Why is the variable myglobal usually not 40? Be specific, be precise. b) In what special case would myglobal be 40? Be specific, be precise. #include <pthread.h> #include <stdlib.h> #include <unistd.h> #include <stdio.h> #include <fcntl.h> /* Purpose: Use 2 threads to increment myglobal exactly 40 times in total. Compile: using -pthread option */...

  • what is output #include<stdio.h> pint main() { int i, j, total; for ( 2 { printf("\n");...

    what is output #include<stdio.h> pint main() { int i, j, total; for ( 2 { printf("\n"); for ( 2 { total = i + j; printf("%d", total); بہا } getchar(); return 0; } Find the for loop content | Output of progrm 5 15 1 11 21 31 41 51 61 2 12 22 32 25 3 13 23 33 43 53 63 4 14 24 34 44 54 64 35 45 6 16 26 36 46 56 66 7...

  • MOdify the program below to do 3x3 matrix mutiplications: #include <iostream> #include <cstdlib> #include <pthread.h> using...

    MOdify the program below to do 3x3 matrix mutiplications: #include <iostream> #include <cstdlib> #include <pthread.h> using namespace std; int A[3] [3] = {{1,2},{3,4}}; int B[3] [3] = {{5,6},{7,8}}; int C[3] [3]; struct Position{     int row;     int col; }; void *CalculateElement(void *pos){     Position *p = (Position *)pos;     C[p->row][p->col] = 0;     for(int i = 0; i < 3; i++){         C[p->row][p->col] += A[p->row][i]*B[i][p->col];     }     pthread_exit(NULL); } const int NUM_THREADS = 4; int main() {    ...

  • 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[]){...

  • p1.c outputs _________ and p2.c outputs__________ p1.c: int total = 0; int main( ) { if...

    p1.c outputs _________ and p2.c outputs__________ p1.c: int total = 0; int main( ) { if (fork( ) == 0) { total +=4; exit (0); } wait( NULL); total += 3; printf( “%d\n”, total); } p2.c: int total = 0; void *thread_start(void *v) { total += 4; } int main ( ) { pthread_t tid; pthread_create(&tis, NULL, thread_start, NULL); pthread_join(tid, NULL); total += 3; printf(“%d\n”, total); }

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