Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

CS3733: Homework/Practice 09 Complete the following programs to show how to pass

ID: 3704391 • Letter: C

Question

CS3733: Homework/Practice 09 Complete the following programs to show how to pass various parameters to a thread, which will simply prints out the given parameter(s) Pass an integer value to a thread: main void "myth (void arg) pthread t tidi int i 3773 Pass a double value to a thread: main void *mythread (void *arg) f pthread t tid: double d3773.001; Pass an array of double values to a thread: main) void "mythread (void *arg) ( pthreadt tid; double d[3)3.14, 2.5, 1.1; Pass an integer value and double value to a thread: void mythread (void arg) main) ( pthread t tid: int double d3773.001; i3773

Explanation / Answer

here is your solution : ---------->>>>>>>>>>>

#include<stdio.h>
#include<pthread.h>

typedef struct value{
int val1;
double val2;
}Pass;

void *myth(void *arg){
printf("%d ",*((int*)arg));
}
//for double value
void *mythread1(void *arg){
printf("%0.2f ",*((double*)arg));
}
//for array of double value
void *mythread2(void *arg){
int i;
double *arr = (double*)arg;
for(i = 0;i<5;i++){
  printf("%0.2f ",*(arr+i));
}
}
//for an and and double value
void *mythread3(void *arg){
printf("%d %0.2f ",((Pass*)arg)->val1,((Pass*)arg)->val2);
}

int main(){
Pass *arg = (Pass *)malloc(sizeof(Pass));
double *arr = (double *)malloc(sizeof(double)*5);
int i;
for(i = 0;i<5;i++){
  arr[i] = 6.5;
}
double arg1 = 5.6;
int arg2 = 34.6;
arg->val1 = 23;
arg->val2 = 3.89;

pthread_t t1,t2,t3,t4;
pthread_create(&t1,NULL,myth,(void *)(&arg2));
pthread_create(&t2,NULL,mythread1,(void *)(&arg1));
pthread_create(&t3,NULL,mythread2,(void *)(arr));
pthread_create(&t4,NULL,mythread3,(void *)(arg));

pthread_join(t1,NULL);
pthread_join(t2,NULL);
pthread_join(t3,NULL);
pthread_join(t4,NULL);
}