if(tot == 0)
{
return 0;
}

should be

if(tot == 0)
{
close(fil);
return 0;
}

you open and never close the file !! this is a handle leak and normal  
userland instances are only allowed ot have 1024 open fds




Quoting "jonas.elofsson" <[email protected]>:

>
>
> Ok, tried some other stuff, see test code below. When the counter   
> 'i' reaches 128 (* 8 = 1024 files opened and closed) i get bad   
> printouts from the open and read calls.
>
>
> My test code:
>
> (panna.c)
>
> #include <stdio.h>
> #include <fcntl.h>
>
> void msDelay(int ms)
> {
>   int i,a;
>   int delayvar=10;
>
>   for (a=0;a<ms;a++)
>   {
>     for (i=0;i<33084;i++)
>     {
>       delayvar*=2;
>       delayvar/=2;
>     }
>   }
> }
>
> float readtemp(char* pin)
> {
>       int fil;
>       char filename[100];
>       float tot = 0, high = 0, temp, dc;
>
>       char c;
>
>       sprintf(filename, "/proc/diskstats");
>
>       fil = open(filename, O_RDONLY);
>
>       if(fil < 0) printf("Open error!");
>
>       if(read(fil, &c, 1) != 1) printf("Read error!");
>
>       if(tot == 0)
>       {
>               return 0;
>       }
>
>       dc = high/tot;
>
>       temp = (dc - 0.32) / 0.0047;
>
>       close(fil);
>
>       return temp;
> }
>
> int main(void)
> {
>       //printf("\x1B[1;0;0;10;31;14;4;0;0Q");
>       //printf("\x1B[2J\x1B[4;8H=)\x1B[2;3HJonas \x01 Emma");
>
>       float temp[8];
>       char* pins[8] = {"PG01", "PG02", "PG03", "PG04", "PG05", "PG16",   
> "PG17", "PG18"};
>       int i, j = 0;
>
>       printf("\x1B[2J"); //ClrScr
>
>       while(1)
>       {
>               for(i = 0; i <= 7; i++)
>               {
>                       temp[i] = readtemp(pins[i]);
>               }
>
>               j++;
>
>               printf("\x1B[0;0H"); //GotoXY 0,0
>
>               printf(" %02.0f %02.0f %02.0f %02.0f %02.0f\n", temp[0], 
> temp[1],   
> temp[2], temp[3], temp[4]);
>               printf(" %02.0f %02.0f    %04.1f\n", temp[5], temp[6], temp[7]);
>               printf(" %d", j);
>
>               fflush(stdout);
>
>               msDelay(10);
>
>       }
>
>       return 0;
>
> }
>
> Makefile:
>
> AXIS_USABLE_LIBS = UCLIBC GLIBC
> include $(AXIS_TOP_DIR)/tools/build/Rules.axis
>
> PROGS = panna
>
> all: $(PROGS)
> $(PROGS): $(PROGS).o
>       $(CC) $(LDFLAGS) $^ $(LDLIBS) -o $@
>
> clean:
>       rm -f $(PROGS) *.o core
>
>
> Best regards, Jonas
>
> --- In [email protected], j...@... wrote:
>>
>> on proc you should use open() close() read()
>>
>> please post the complete code
>>
>>
>>
>>
>> Quoting "jonas.elofsson" <jelofs...@...>:
>>
>> > Hi!
>> >
>> > I have a problem reading files in the /proc directory in C.
>> >
>> > I made a module for reading SMARTTEMP temperature sensors, and it
>> > works great. I made it to create one file for each I/O pin under
>> > /proc/smarttemp/. Working beautifully.
>> >
>> > I also made a module for controlling my LCD display, and that to
>> > works like a charm.
>> >
>> > I put together a small test program to read 8 different temperaturs
>> > from different sensors and then display them on the display updating
>> >  once every second.
>> >
>> > Works great, for two minutes...
>> >
>> > Then, when one of the /proc files is read for the 1024:th time
>> > problems occurs. It turns out the file descriptors are out..! They
>> > don't seem to get relesaed when I do a fclose (I use fopen and
>> > fclose on them, is this a problem?).
>> >
>> > I get no problems if I change the program to read from a file on the
>> >  normal FS instead.
>> >
>> > If I try to read some other file in /proc i get the same error after
>> >  1024 loops... So it seems it's not in my driver code.
>> >
>> > I tried to do a system("cp /proc/smarrtemp/PG01 /var/temp"); for
>> > each measurement and then reading the file in /var instead, trying
>> > to fool it a bit, just for testing. But after about 1024 copies I
>> > still get problems.
>> >
>> > I would be very thankful for any ideas about why this is and how to
>> > get it working.
>> >
>> > I use the image on ACMEs homepage, 2.6 something. =)
>> >
>> > Best regards, Jonas
>> >
>> >
>> >
>> >
>>
>>
>>
>> --
>> John Crispin
>> hacking, coding, etc
>> blogic on irc
>>
>>     _______                     ________        __
>>    |       |.-----.-----.-----.|  |  |  |.----.|  |_
>>    |   -   ||  _  |  -__|     ||  |  |  ||   _||   _|
>>    |_______||   __|_____|__|__||________||__|  |____|
>>             |__| W I R E L E S S   F R E E D O M
>>    KAMIKAZE (bleeding edge) -----------------------
>>     * 10 oz Vodka       Shake well with ice and strain
>>     * 10 oz Triple sec  mixture into 10 shot glasses.
>>     * 10 oz lime juice  Salute!
>>    ---------------------------------------------------
>>
>
>
>



-- 
John Crispin
hacking, coding, etc
blogic on irc

    _______                     ________        __
   |       |.-----.-----.-----.|  |  |  |.----.|  |_
   |   -   ||  _  |  -__|     ||  |  |  ||   _||   _|
   |_______||   __|_____|__|__||________||__|  |____|
            |__| W I R E L E S S   F R E E D O M
   KAMIKAZE (bleeding edge) -----------------------
    * 10 oz Vodka       Shake well with ice and strain
    * 10 oz Triple sec  mixture into 10 shot glasses.
    * 10 oz lime juice  Salute!
   ---------------------------------------------------

Reply via email to