title: get_pixel function raises bus error: 10
body: <pre><code>    #include    &lt;sys/types.h&gt;
      #include    &lt;sys/stat.h&gt;
      #include    &lt;sys/mman.h&gt;    
      #include    &lt;fcntl.h&gt;
      #include    &lt;stdio.h&gt;   
      #include    &lt;stdlib.h&gt;  
      #include    &lt;string.h&gt;  
      #include    &lt;unistd.h&gt;
      #include    &lt;limits.h&gt;  
      #include    &lt;pthread.h&gt;
  
      #define FILE_MODE   (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
  
      #ifndef MAP_FILE    /* 44BSD defines this &amp; requires it to mmap files */
      #define MAP_FILE    0   /* to compile under systems other than 44BSD */
      #endif
  
      #define SIZE 20
  
      #define N 3
  
      struct stat statbuf;
  
      struct pixel
      {
          char R;
          char G;
          char B;
      };
  
      char * src;
      int size;
  
      struct pixel *get_pixel(char *buf, int *pos)
      {
          struct pixel pix;
          struct pixel *ppix = &amp;pix;
          ppix-&gt;R = buf[*pos];
          ppix-&gt;G = buf[(*pos)+1];
          ppix-&gt;B = buf[(*pos)+2];
          (*pos) += 3;
          return ppix;
      }
  
      void write_pixel(struct pixel *ppix, char *buf, int *pos)
      {
          buf[*pos] = ppix-&gt;G;
          buf[(*pos)+1] = ppix-&gt;B;
          buf[(*pos)+2] = ppix-&gt;R;
          (*pos) += 3;
      }
  
      void * do_rotation(void *rotation)
      {
          int fdout;
          char    *dst;
          char fich[SIZE];
  
          int counter,index, pos, x, y;
          int xmax = 0;
          int ymax = 0;
          int colormax = 0;
  
          /* Construção do nome do ficheiro de saída */
          sprintf(fich, "rot%d.ppm", *((int*)rotation));
  
          /* Zona #2 */
  
          if ( (fdout = open(fich, O_RDWR | O_CREAT | O_TRUNC,FILE_MODE)) &lt; 0)
          {
              fprintf(stderr,"can't creat %s for writing\n", fich);
              exit(1);
          }
  
          /* set size of output file */
          if (lseek(fdout, size - 1, SEEK_SET) == -1)
          {
              fprintf(stderr,"lseek error\n");
              exit(1);
          }
          if (write(fdout, "", 1) != 1)
          {
              fprintf(stderr,"write error\n");
              exit(1);
          }
  
  
          if ( (dst = mmap(0, size, PROT_READ | PROT_WRITE,MAP_SHARED,fdout, 0)) == (caddr_t) -1)
          {
              fprintf(stderr,"mmap error for output\n");
              exit(1);
          }
          /* Fim da Zona #2 */ 
  
          sscanf(src,"P6\n%d %d\n%d\n",&amp;xmax,&amp;ymax,&amp;colormax);
  
          struct pixel imagem [ymax][xmax];
  
          for (counter=0, index=0; counter&lt;3;index++)
          {
              if (src[index]=='\n')
                  ++counter;
          }   
          pos=index-1;
          for (y=0;y&lt;ymax;y++)
              for (x=0;x&lt;xmax;x++)
                  imagem[y][x] = *(get_pixel(src,&amp;pos));
          pos=index;
  
          switch (*((int*)rotation))
          {
              case 0 :
                  //Rotação 90º clockwise
                  sprintf(dst,"P6\n%d %d\n%d\n",ymax,xmax,colormax);
                  for (x=0; x&lt;xmax; x++)
                      for (y=ymax-1; y&gt; -1;y--)
                          write_pixel(&amp;(imagem[y][x]),dst,&amp;pos);
                  break;
              case 1:
                  //Rotação 180º
                  sprintf(dst,"P6\n%d %d\n%d\n",xmax,ymax,colormax);
                  for (y=ymax-1;y&gt;-1;y--)
                      for (x = xmax-1; x&gt;-1; x--)
                          write_pixel(&amp;(imagem[y][x]),dst,&amp;pos);
                  break;
              case 2:
                  //Rotação 270º clockwise
                  sprintf(dst,"P6\n%d %d\n%d\n",ymax,xmax,colormax);
                  for (x=xmax-1; x&gt;-1; x--)
                      for (y=0; y&lt;ymax;y++)
                          write_pixel(&amp;(imagem[y][x]),dst,&amp;pos);
                  break;
  
  
          }
  
      /* Zona #3 */
          munmap(dst,size);
          close(fdout);
      /* Fim da Zona #3 */
  
          pthread_exit(NULL);
  
      }
  
      int get_stat(int fdin)
      {
          struct stat pstatbuf;   
          if (fstat(fdin, &amp;pstatbuf) &lt; 0) /* need size of input file */
          {
              fprintf(stderr,"fstat error\n");
              exit(1);
          }
          return pstatbuf.st_size;
      }
  
  
      int main(int argc, char *argv[])
      {
          int id[N];
          int fdin, i;
  
          if (argc != 2)
          {
              fprintf(stderr,"usage: trab11 &lt;fromfile&gt;\n");
              exit(1);
          }
  
          if ( (fdin = open(argv[1], O_RDONLY)) &lt; 0)
          {
              fprintf(stderr,"can't open %s for reading\n", argv[2]);
              exit(1);
          }
  
          size = get_stat(fdin);
  
      /* Zona # 1*/
          if ( (src = mmap(0, size, PROT_READ, MAP_FILE | MAP_SHARED, fdin, 0)) == (caddr_t) -1)
          {
              fprintf(stderr,"mmap error for input\n");
              exit(1);
          }
      /* Fim da Zona #1 */
  
      /* Zona #5 */
          pthread_t tid[N];
          for (i=0; i&lt;N; i++){
              id[i]=i;
              pthread_create (&amp;tid[i], NULL, do_rotation, &amp;id[i]);
          }
          for (i=0; i&lt;N; i++)
              pthread_join (tid[i], NULL);
      /* Fim da Zona #5 */
  
      /* Zona #4 */
          munmap(src,size);
          close(fdin);
      /* Fim da Zona #4 */
  
          exit(0);
      }
  </code></pre>
  
  <p>Every time I run this on Mac it gives me <em>Bus error: 10</em>, but if I run it on Linux it doesn't give me any error can someone give me a solution.</p>
  
  <p>PS: The error is on <code>get_pixel</code> function but I still can't figure it out.</p>