("Failed to open Xillybus device file(s

Questions and discussions about the Xillybus IP core and drivers

("Failed to open Xillybus device file(s

Postby Guest » Tue Jul 24, 2018 4:56 pm

Hi.. i am writing a code where i have to read from input files and then read the output. one of the files is large and is around 38mbs. i not very good in programming so by looking at some tutotials and xillybux programming guide , I wrote the following code to read from two files, write them to the logic and then read them. my motive is to calculate the time of reading and writing for my future project. but when i try to run the program i get the following error " Failed to open Xillybus device file(s), device resource busy" I dont know what i have done wrong. the code which i had wrote is below

#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[N]);

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;
}
}
}
}
thanks.. for guidance
Guest
 

Re: ("Failed to open Xillybus device file(s

Postby support » Tue Jul 24, 2018 5:15 pm

Hello,

"Device or resource busy" errors are usually a result of the device file being open by another process. You may nail it down with the "lsof" command, but odds are that the fork's child from a previous run didn't terminate for whatever reason.

Regards,
Eli
support
 
Posts: 623
Joined: Tue Apr 24, 2012 3:46 pm


Return to Xillybus

cron