Page 1 of 1

Error Closing write_32 file! Xillinux

PostPosted:
by Guest
I have to read from two text files, then multiply them by matrix multiplication and then read the output from the resultant file. i running linux on zedboard and followed the procedures mentioned on xillibux website to install xillilinux on zedboard. I ran some examples and everything worked fine and also tried to understand how it works. i understanded upto some extent the working manner. The code which i am using to read and write from files is given below

Code: Select all
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdint.h>
#include <sys/time.h>
#include <math.h>
#include <string>
#include <cstdlib>
#define N ((cols+1)*rows + cols)

int main(int argc, char *argv[])
{
int fdr, fdw;
FILE *ftime_rw;
FILE *fp;
FILE *U, *V, *RI;
int rc, donebytes;
int *tologic, *fromlogic;
pid_t pid;
int i,j;
struct timeval tv1, tv2;
double time_rw;
char *buf;
int temp;
int rows= 575;
int cols= 18689;

fdr = open("/dev/xillybus_read_32", O_RDONLY);
fdw = open("/dev/xillybus_write_32", O_WRONLY);

if ((fdr < 0) || (fdw < 0))
{
perror("Failed to open Xillybus device file(s)");
exit(1);
}

U = fopen("U.txt", "r");
V = fopen("V.txt", "r"); 
RI = fopen("RI.txt", "a");

if(U==NULL || V==NULL || RI==NULL )
 { printf("Write in their respective files!\n");
   exit(1);
 }
for (i=0; i<rows; i++) // Read
 { fscanf(U,"%d", &temp);
   fprintf(RI,"%d\n", temp);
 }
for(j=0; j<rows; j++)
{ for (i=0; i<cols; i++) // Read
 { fscanf(V,"%d", &temp);
   fprintf(RI,"%d ", temp);
 }

fclose(U);
fclose(V);
fclose(RI);


tologic = (int*)malloc(sizeof(int)* N);
if (!tologic){
fprintf(stderr, "failed to allocate memory\n");
exit(1);
}
RI = fopen("RI.txt", "r");

for(j=0; j<rows; j++)
{ for (i=0; i<cols; i++)
 fscanf(RI,"%d", &tologic[i]);

fclose(RI);

pid = fork();  // writer + reader

if (pid < 0)
{ perror("Failed to fork()");
  exit(1);
}

if (pid) // writer process
{
  close(fdr);
buf = (char *)tologic;
donebytes=0;

gettimeofday(&tv1, NULL);  // start count time

while (donebytes < sizeof(int)*N)  // write N integers
   {
     rc = write(fdw, tologic + donebytes, sizeof(int)*N - donebytes);

     if ((rc < 0) && (errno == EINTR))
   continue;

     if (rc <= 0)
       {
     perror("write() failed");
     exit(1);
       }

      donebytes += rc;
      }
    gettimeofday(&tv2, NULL);  // stop count time

   time_rw = (double) (tv2.tv_usec-tv1.tv_usec);

   printf("Writer. Total time = %f usec\n", time_rw);
  if(close(fdw)==-1)
     printf("ERROR closing write_32 file!\n");

   return 0;
 }
 else  // reader process
 {
  close(fdw);

 fromlogic =(int*)malloc(sizeof(int)* N);
 if (! fromlogic){
 fprintf(stderr, "failed to allocate memory\n");
  exit(1);
 }

buf = (char *)fromlogic;
donebytes = 0;

  donebytes = 0;

  gettimeofday(&tv1, NULL);  // start count time

  while (donebytes < sizeof(int)*rows)  // read num_rows integers
    {
      rc = read(fdr, fromlogic + donebytes, sizeof(int)*rows - donebytes);

      if ((rc < 0) && (errno == EINTR))
    continue;

      if (rc < 0)
        {
      perror("read() failed");
      exit(1);
        }

      if (rc == 0)
        {
      fprintf(stderr, "Reached read EOF!? Should never happen.\n");
      exit(0);
        }

      donebytes += rc;
    }

  gettimeofday(&tv2, NULL);  // stop count time

  time_rw = (double) (tv2.tv_usec-tv1.tv_usec);

  printf("Reader. Total time = %f usec\n", time_rw);

  for (i=0; i<rows; i++)  // Integers read from logic
    printf("Reader process : %d\n", fromlogic[i]);

  sleep(1); // Let debug output drain (if used)

  if(close(fdr)==-1)
    printf("ERROR closing read_32 file!\n");

  return 0;
}
}
}
}

but now I am not only getting reading and writing process timings and the files when i read them are 0 and then at the end i get the error "Error Closing write_32 file!" timing values are Writer. Total time = 51122.00 usec Reader. Total time = 21.0000 usec

Please anyone help me in correcting my code.. thanks

Re: Error Closing write_32 file! Xillinux

PostPosted:
by support
Hello,

I suggest using perror() in the error message for the failed close() in order to obtain more information.

However the common reason for close() failing is because there is data that has been written to the device file, waiting in the DMA buffer, which hasn't been consumed by the FPGA (PL) side. Xillybus waits for 1000 ms when the file is closed to give the FPGA a last chance, but if there's still unconsumed data, close() returns an error and a message is issued in the kernel's log.

Regards,
Eli

Re: Error Closing write_32 file! Xillinux

PostPosted:
by Guest
can you suggest me how can i solve this issue.. what changes should i have to make..
thanks

Re: Error Closing write_32 file! Xillinux

PostPosted:
by support
Hello,

As I suggested, use perror() to get more info on the nature of the error.

And if it's indeed a timeout, check why the FPGA doesn't collect the data sent to it.

Regards,
Eli