Find the answer to your Linux question:
Results 1 to 5 of 5
i have a program which communicates with another file to get info from a config file periodically, the other file checks ever 5 seconds if the file has changed...this one ...
  1. #1
    Just Joined!
    Join Date
    Dec 2006
    Posts
    85

    "too many open files"

    i have a program which communicates with another file to get info from a config file periodically, the other file checks ever 5 seconds if the file has changed...this one opens the file and reads it, and writes its data (it only reads if the lockfile was created...this means the file has been changed (it used to work like a locfile thats why it has that name...now it just exists to tell tell you when a change has been made and read it...otherwise it just writes its data)

    the problem is i keep getting this error saying "too many files open" and so it only reads it the first time the program runs...after that it errors...

    included is the code for the function that is erroring...



    Code:
    int fd,lockfile;
    if((lockfile=open("lock.dat",O_WRONLY|O_CREAT|O_EXCL))==-1)
    {
    printf("reading\n");
    fd=open("data.dat",O_RDONLY);
    if(fd==-1)
    {
    perror("open");
    }
    if(read(fd,&this->data,sizeof(wiimotedata))>0)
    {
    printf("button a is: %d\n",data.buttons.button_a);
    }
    close(fd);
    remove("lock.dat");
    }
    else{
    fd=open("data.dat",O_WRONLY);
    write(fd,&this->data,sizeof(wiimotedata));
    close(fd);
    remove("lock.dat");
    }
    }

  2. #2
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568

    Smile

    As errors says you have opened too many files,
    I think issue is with lockfile try and close it

    close(lockfile)
    If you want to know which file is opened try running strace with binary,

    example to trace open system calls ,

    strace -e trace=open ./a.out
    if u want to trace all system calls

    strace -e trace=all./a.out
    HTH
    - Lakshmipathi.G
    -------------------
    FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
    First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -------------------

  3. #3
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Lakshmipathi is correct on all counts. The only thing I would add is that this:
    Code:
    strace -e trace=all ./a.out
    can also be said like this:
    Code:
    strace ./a.out
    since the default for trace is all.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  4. #4
    Just Joined!
    Join Date
    Dec 2006
    Posts
    85
    oh wow...i deleted the file but didnt close it...*doh*

    thanks i'll try it with closeing...

    yeah that fixed it...cool thanks

  5. #5
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568

    Post

    Quote Originally Posted by wje_lf View Post
    [B]can also be said like this:
    Code:
    strace ./a.out
    since the default for trace is all.
    Thanks wje_lf ...I'll use it hereafter
    - Lakshmipathi.G
    -------------------
    FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
    First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -------------------

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...