Thread: Linux device files

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    14

    Linux device files

    Hi,

    Is there a limit to how many times you can open or write to a device file? I have a program which is a continuous loop and is always writing or reading an I2C device file. After about the 1000th time it fails to open the device.

    Anybody have an idea?

    Here's the code:

    Code:
    /************************************************************************/
    /* read_ad								*/
    /* inputs: I2C address to write to (addr)				*/
    /* outputs: Reading from the A/D in hex (ad_output)			*/
    /* descritpion: Reads I2C A/D chip		*/
    /************************************************************************/
    int read_ad(char addr){
    
    	sprintf(filename, "/dev/i2c-%d", adapter_nr);
    	if ((file = open(filename,O_RDWR)) < 0) 
    	{
    		printf("Error openning device file in read_ad()\n");
    		exit(1);
    	}
    		else{
    		//printf("Device file opened\n");
    		}
    	
    	if (ioctl(file,I2C_SLAVE,addr) < 0) 
    	{
    		printf("Error with ioctl function in read()\n");
    		exit(1);
    	}
    		
      	if (read(file,ad_output,2) != 2) 
    	{
        		printf("Error reading A/D chip\n");
      	}
    		else{
    		printf("Device file (A/D chip) reads %x\n", ad_output[0]);
    		}
    
    }

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    What is the declaration for filename -- does it have enough room for the text of "/dev/i2c-1000" (at least 14 characters)?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    And are you close()ing the open file every time through the loop? There's a limit to how many open files you can have:
    Code:
    itsme@dreams:~$ ulimit -a
    ...
    open files                  1024
    ...
    Also, why don't you use errno to see why open() is failing? Then you'd know if it's hitting that limit. open() sets errno to:
    Code:
           EMFILE The process already has the maximum number of files
                  open.
    ...if it fails because of that.
    If you understand what you're doing, you're not learning anything.

  4. #4
    .
    Join Date
    Nov 2003
    Posts
    307
    Code:
    ulimit -aH
    will show you the current hard limit for open files (file descriptors).

    If error checking shows that is the problem you can :

    Edit /etc/security/limits.conf and add the lines:
    * soft nofile 1024
    * hard nofile 65535

    Edit /etc/pam.d/login, adding the line:

    session required /lib/security/pam_limits.so

    The system file descriptor limit is set in
    /proc/sys/fs/file-max.

    This will increase the limit to 65535:

    echo 65535 > /proc/sys/fs/file-max

    Then increase the file descriptor limits for the process

    [quote[ulimit -n unlimited[/quote]

    (--- after you log out and then back in.)

    The command will set the limits to the hard limit in /etc/security/limits.conf.

    You really should consider a different program design - maybe forking some new processes, or closing and reusing fd's. Using 65335 for file limit is usually not a good idea system-wide.

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    14
    Thanks for the reply guys,

    I went back and closed the file I opened and it seems to work.

    Thanks for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 03-10-2008, 12:08 PM
  2. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 08:44 AM
  3. Linux (2.6.10+) device driver: multiple classes?
    By filker0 in forum Linux Programming
    Replies: 3
    Last Post: 09-26-2005, 08:46 PM
  4. Is Linux Really A Programmers OS?
    By SourceCode in forum A Brief History of Cprogramming.com
    Replies: 43
    Last Post: 04-07-2003, 09:24 PM
  5. files in linux
    By GaPe in forum C Programming
    Replies: 3
    Last Post: 03-30-2003, 05:11 PM