Advertisement
ahmad_zizo

SHM

Apr 30th, 2015
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.35 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <sys/mman.h>
  3. #include <sys/types.h>
  4. #include <unistd.h>
  5. #include <stdlib.h>
  6. #include <fcntl.h>
  7. #include <string.h>
  8. #include <sys/time.h>
  9. #include <sys/wait.h>
  10. #include <sys/shm.h>
  11. #include <sys/stat.h>
  12. #include <time.h>
  13.  
  14.  
  15.  
  16.  
  17. double *a,*b,*c;
  18.  
  19. int M;
  20.  
  21. void print_matrix(double *m)
  22. {
  23.     int i,j;
  24.     for(i=0; i<M; i++)
  25.     {
  26.         for(j=0; j<M; j++)
  27.         {
  28.             printf("%g\t" ,*(m+M*i+j));
  29.         }
  30.         printf("\n");
  31.     }
  32.  
  33.     printf("\n");
  34. }
  35.  
  36. void generate_matrix(double *m)
  37. {
  38.     int i;
  39.     for(i=0; i<M*M; i++)
  40.     {
  41.         *(m+i) = 1+rand()%100;
  42.     }
  43. }
  44.  
  45. void calc_row(int row)
  46. {
  47.     int j,k;
  48.     for(j=0; j<M; j++)
  49.     {
  50.         *(c+row*M+j)=0;
  51.         for(k=0; k<M; k++)
  52.         {
  53.             *(c+row*M+j)=*(c+row*M+j)+*(a+row*M+k)* *(b+k*M+j);
  54.         }
  55.     }
  56. }
  57.  
  58. int main(int argc, char *argv[])
  59. {
  60.      srand(time(NULL));
  61.      if(argc != 2){
  62.  
  63.         printf("\nERROR\n");
  64.  
  65.         exit(0);
  66.  
  67.     }
  68.  
  69.     if(atoi(argv[1]) < 1){
  70.  
  71.         printf("\nERROR\n");
  72.  
  73.         exit(0);
  74.  
  75.     }
  76.     int i;
  77.     M = atoi(argv[1]);
  78.     clock_t t1,t2;
  79.     int shm_fd;
  80.     double* shared_memory;
  81.  
  82.     int msize = 3*M*M*sizeof(double);
  83.     char *name = "MATRIX_MULT";
  84.  
  85.     shm_fd = shm_open (name, O_CREAT | O_EXCL | O_RDWR, S_IRWXU | S_IRWXG);
  86.  
  87.     if (shm_fd < 0)
  88.     {
  89.         perror("In shm_open()");
  90.         exit(1);
  91.     }
  92.  
  93.     ftruncate(shm_fd, msize);
  94.  
  95.     shared_memory = (double *) mmap(NULL, msize, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
  96.     if (shared_memory == NULL)
  97.     {
  98.         perror("In mmap()");
  99.         exit(1);
  100.     }
  101.  
  102.     a = shared_memory;
  103.     b = shared_memory + M*M;
  104.     c = b + M*M;
  105.  
  106.     generate_matrix(a);
  107.     generate_matrix(b);
  108.    
  109.     t1 = clock();
  110.     for(i=0; i<M; i++)
  111.     {
  112.         int childPID=fork();
  113.         if(childPID >= 0)
  114.         {
  115.             if(childPID==0)
  116.             {
  117.                 calc_row(i);
  118.                 exit(0);
  119.             }
  120.         }
  121.     }
  122.  
  123.  
  124.  
  125.     int x = 0;
  126.     int stat;
  127.     do
  128.     {
  129.         x = waitpid(-1,&stat,WNOHANG);
  130.     }
  131.     while (x==0);
  132.    
  133.     shm_unlink(name);
  134.     t2 = clock();
  135.  
  136.     print_matrix(a);
  137.  
  138.     print_matrix(b);
  139.  
  140.     print_matrix(c);
  141.  
  142.  
  143.     printf("\n\ntime taken = %lf\n",(double)(t2-t1)/(CLOCKS_PER_SEC / 1000.0));
  144.  
  145.     return 0;
  146.  
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement