Write Program in C not C++ (Should Run On Linux) Use Shared Memory -Create 8 thr
ID: 3882485 • Letter: W
Question
Write Program in C not C++ (Should Run On Linux)
Use Shared Memory
-Create 8 threads [[east to change 2 to 12]]
-Each thread with tid 1 to 12
Odd Threads:
-compute tid factorial 1*2*3*4*5
Even Threads:
-compute tid summation 1+2+3+4+5+6
Testing: Threads can print
Final: all output from main
-Main computes sum of the thread values
- report each value & Sum
-report overall sum
Explanation / Answer
main.c
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
int q = 0;
void *myThreads(void *varg)
{
int *tid = (int *)varg;
static int str = 0;
++str; ++q;
printf("Thread ID: %d, Static: %d, Global: %d ", *tid, ++str, ++q);
}
int main()
{
int uu;
pthread_t tid;
for (uu = 0; uu < 3; uu++)
pthread_create(&tid, NULL, myThreads, (void *)uu);
pthread_exit(NULL);
return 0;
}
Rate an upvote.....Thankyou
Hope this helps.....
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.