fork download
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<mpi.h>
  4. #include<pthread.h>
  5. #define N 100
  6. #define T 20
  7.  
  8. int *arr1,*arr2,*arr3;
  9. void *hello(void *threadId){
  10. long tid = (long)threadId;
  11. long localSum = 0;
  12. int chunk_size = N / T;
  13. int start = tid * chunk_size;
  14. int end = (tid + 1) * chunk_size;
  15.  
  16. //ensure the last thread processes the remaining elements
  17. if(tid == T - 1){
  18. end = N;
  19. }
  20. for(int i=start; i<end;i++){
  21. arr3[i] = arr1[i] + arr2[i];
  22. }
  23. return NULL;
  24.  
  25. }
  26. int main(){
  27. arr1 = (int *)malloc(sizeof(int) * N);
  28. arr2 = (int *)malloc(sizeof(int) * N);
  29. arr3 = (int *)malloc(sizeof(int) * N);
  30. for(int i=0;i<N;i++){
  31. arr1[i] = i+ 1;
  32. arr2[i] = i + 1;
  33. arr3[i] = 0;
  34. }
  35. pthread_t threads[T];
  36. //create threads
  37. for(long i=0;i<T;i++){
  38. pthread_create(&threads[i],NULL,hello,(void*)i);
  39. }
  40. //join threads and aggregate the local sums
  41. for(long i=0;i<T;i++){
  42. pthread_join(threads[i],NULL);
  43. }
  44. for(int i=0i<N;i++){
  45. printf("%d",arr3[i]);
  46. }
  47. return 0;
  48.  
  49. }
Success #stdin #stdout #stderr 0.25s 40540KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error: unexpected ',' in "int *arr1,"
Execution halted