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?
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);
}

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; ...
Read the following code, threaded recursive calculation of Fibonacci number of n: #include <stdio.h> #include <stdlib.h> #include <pthread.h> void *fib(void *arg); int main(int argc, char **argv){ int n = atoi(argv[1]); printf("%d\n", (int)fib(n)); } void *fib(void *arg){ int n; pthread_t thread1; pthread_t thread2; void *a; void *b; int c; n = (int)arg; if (n <= 0) return 0; if (n == 1) return 1; pthread_create(&thread1, NULL, fib, n-1); ...
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 = 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,...
can someone help me with changing this to c++ language #include <stdlib.h> #include <stdio.h> #include <pthread.h> #include <string.h> #include <dirent.h> #include <sys/wait.h> #include <time.h> #include <sys/stat.h> #include <unistd.h> char *pathLog; char *pathRep; struct stat attr; int fileNum, curNum; char *create_logPath(char *directory); void *funcChecker(void *pathSub); void *funcprintTimeAndChanges(void *pathSub); void main(int argc, char *argv[]) { if(argc < 3) { printf("%s must be executed with exactly two additional arguments (pathRep, pathSub)!\n", argv[0]); printf("Typed count is %d\n", argc); printf("Aborted...
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() { ...
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 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 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). 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"); 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...