Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
ele709 lab 3.2
#include <stdio.h> #include <time.h> #include <stdlib.h> #include <pthread.h> struct thread_info { double a, b; // The numbers a and b char op; // The math iteration to be performed: +,-,*,/ int maxitr; // The number of iterations to be performed double c; // The result: c = a <op> b double exec_time; // The execution time per iteration in nsec }; typedef struct thread_info thread_info_t; void *func(void *arg) { struct timespec time_1, time_2; double exec_time; thread_info_t *info; int i, maxitr; volatile double a, b, c, count; // char op; part of a convoluted plan I had. Ignore for the moment. Remove in final copy. info = (thread_info_t *)arg; maxitr = info->maxitr; b = 2.3; a = 4.5; exec_time = 0.0; clock_gettime(CLOCK_REALTIME, &time_1); if (count == 0){ for (i = 0; i < maxitr; i++) { c = a + b; } count = 1; } else if (count == 1) { for (i = 0; i < maxitr; i++) { c = a - b; } count = 2; } else if (count == 2) { for (i = 0; i < maxitr; i++) { c = a * b; } count = 3; } else if (count == 3){ for (i = 0; i < maxitr; i++) { c = a / b; } count = 0; } else{ printf("A failure, your efforts notwithstanding.\n"); count = 0; } printf("Computation: %lf\n", c); clock_gettime(CLOCK_REALTIME, &time_2); exec_time = (time_2.tv_sec - time_1.tv_sec); exec_time = exec_time + (time_2.tv_nsec - time_1.tv_nsec)/1.0e9; info->exec_time = exec_time; pthread_exit(NULL); } int main(void) { pthread_t thread1; thread_info_t info1; double maxitr; maxitr = 5.0e8; info1.maxitr = (int)maxitr; /* printf("Welcome to the Experiment.\n"); printf("Please enter the equation: \n"); scanf("%lf %c %lf",&a, &op, &b); This might be more useful later? Or I am pointlessly increasing the complexity of this assignment. Pass the input to the structure via pointers alternatively, set up a loop that generates four pthreads - one per operation. Have them run sequentially, by putting the pthread_join before the loop cycles. Pass the loop counter to the function which using a if else nest sets the op to be performed 1 -> + etc. */ for (int t=0; t<4; t++){ if (pthread_create(&thread1, NULL, &func, &info1) != 0) { printf("Error in creating thread 1\n"); exit(1); } pthread_join(thread1, NULL); printf("Exec time for thread1 = %lf sec\n", info1.exec_time); } pthread_exit(NULL); //main(); pointlessly trying to cycle. }
run
|
edit
|
history
|
help
0
18BCE2182 MIDTERM QUES-2
mypro
a3
20171117_FUNCIONES
DUE TUESDAY
switch_nota
Program 1 - 2D array and memory pointers
Arrays Grtade and Toppers
140114_CribaErasthotenes
#Define / Case/ If practice