Aspiring Mad Scientist · Author has 1.4K answers and 3.2M answer views · 5y ·
Here’s help on your homework assignment…
- #include <pthread.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <time.h>
- int val=0;
- void *foo(void *arg)
- {
- sleep(*(int *)arg);
- val = *(int *)arg;
- return NULL;
- }
- int main()
- {
- // select two random numbers from 1 to 10
- srand(time(NULL));
- int a=1 + rand() % 10;
- int b=1 + rand() % 10;
- printf("inputs are %d %d\n\n", a, b);
- fflush(stdout);
- // print the larger of the two without using any conditional statements
- pthread_t t[2];
- pthread_create(&t[0],NULL,foo,(void *)&a);
- pthread_create(&t[1],NULL,foo,(void *)&b);
- pthread_join(t[0],NULL);
- pthread_join(t[1],NULL);
- printf("the larger number is %d\n",val);
- return 0;
- }
29.4K views ·
View upvotes
· View 1 share
· 1 of 18 answers
Something went wrong. Wait a moment and try again.