Thread: Sleep stops working after 15 minutes without wifi

  1. #1
    Registered User
    Join Date
    Dec 2021
    Posts
    8

    Question Sleep stops working after 15 minutes without wifi

    Hello everyone, my first post here

    I have a strange problem. I have a c program on raspberry pi which sends temperature data every 32 seconds to LoRa device and I receive it on other raspberry pi.

    The code works fine when raspberry pi is connected to wifi, but stops after 15 minutes without wifi, and that always happens. My code is a mess right now, but I will put here the part where the program stops working. It's the part where I wait for 32 seconds to ask for another reading from thermometer. It just stops in the middle of "do while" loop


    Code:
     
    do{           
                time(&stop);
                tdif=difftime(stop, start);
                log=fopen("./log.txt","a");
                fprintf(log," %.1f", tdif);
                fclose(log);
                sleep(1);
            }
    while(tdif<32);
    This code just calculates a time difference between "start" time, which I took in the beginning, and "stop" time until that difference exceedes 32 seconds.

    I've tried this with "delay()" as well, but I get the same thing. I tried with clock() too, and you guessed it, same thing.

    I'm writing to a file because I don't have ssh when I shut down wifi for that one raspberry pi. Last time it stopped, for example on 5th second in the loop.
    Last edited by GhostLoveScore; 12-27-2021 at 03:36 PM.

  2. #2
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    932
    When you say that the program stops, what do you mean exactly?

    I'm imagining that your program is receiving a signal from the operating system. That's likely to happen anywhere you sleep, since most of the program's wall time is likely spent in the sleep.

    Does your program print anything before stopping? Do you know the process's exit status?
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  3. #3
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    946
    You haven't provided the entire program. You don't need to open and close the file each time you run the loop. tdif is a long int, NOT a float.

    Try my version, which works on Linux with gcc, and compare the code with yours:
    Code:
    #include <stdio.h>
    #include <time.h>
    #include <unistd.h>
    
    int main(void)
    {
       FILE *log = NULL;
       time_t start = 0;
       time_t stop = 0;
       time_t tdif = 0;
    
       time(&start);
       log=fopen("./log.txt","a");
    
       do{
          time(&stop);
          tdif=difftime(stop, start);
          fprintf(log," %ld\n", tdif);
          sleep(1);
       }
       while(tdif<32);
       
       fclose(log);
    
       return 0;
    }

  4. #4
    Registered User
    Join Date
    Dec 2021
    Posts
    8
    Quote Originally Posted by CodeMonkey View Post
    When you say that the program stops, what do you mean exactly?

    I'm imagining that your program is receiving a signal from the operating system. That's likely to happen anywhere you sleep, since most of the program's wall time is likely spent in the sleep.

    Does your program print anything before stopping? Do you know the process's exit status?
    I can't tell you exactly what happens because all I have is a log with fprintf's in it. All I can see is that last thing in the log is fprintf from do-while loop at some random seconds.

    And by stopping I mean, the program doesn't go to next iteration of the loop. It doesn't break out of do-while loop, because if it did that it would print out a line to a log. It just disappears from htop, like it crashed.

    I'm not sure how to check process's exit status.

    This is raspberry pi, as far as I know, it doesn't have a real time clock. Could that be an issue when it doesn't have wifi to check the time?

  5. #5
    Registered User
    Join Date
    Dec 2017
    Posts
    1,330
    You are saying that if you start the program it will run for 15 minutes and then "stop". I'm not familiar with the rasberry pi. Is it possible the program is segfaulting? Can you tell? If it is segfaulting then the problem is probably elsewhere and just happens to show up where you found it. Is it always exactly the same amount of time? What happens if you replace the chunk of code that you've shown with sleep(32)?
    Sign at the Sichuan Tobacco Project Hope Primary School:
    "Work hard for society. Tobacco can help you become an achiever!"

  6. #6
    Registered User
    Join Date
    Dec 2021
    Posts
    8
    Quote Originally Posted by john.c View Post
    You are saying that if you start the program it will run for 15 minutes and then "stop". I'm not familiar with the rasberry pi. Is it possible the program is segfaulting? Can you tell? If it is segfaulting then the problem is probably elsewhere and just happens to show up where you found it. Is it always exactly the same amount of time? What happens if you replace the chunk of code that you've shown with sleep(32)?
    Yep, that's what I'm saying. sleep(32) does the same thing. What I did here is second or third attempt to make things work.
    It's not always the same amount of time. It's roughly 15 minutes, but sometimes it will crash at 6th second, sometimes at 31st second, sometimes at 14th second etc... I could measure it by a stopwatch though.

    I don't know how to tell if it's segfaulting. I just enable the wifi after 20 minutes and see what happened.

    rstanley, my code also works, when the wifi is on . I put your code on my raspberry pi to see what happens, let you know in 15 minutes. And I know I don't need to open and close the file that often, but I did that in case when the program crashes in the middle of the loop, I would at least see it entered the loop and spent some amount of seconds in it.

  7. #7
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    946
    Quote Originally Posted by GhostLoveScore View Post
    rstanley, my code also works, when the wifi is on . I put your code on my raspberry pi to see what happens, let you know in 15 minutes. And I know I don't need to open and close the file that often, but I did that in case when the program crashes in the middle of the loop, I would at least see it entered the loop and spent some amount of seconds in it.
    Post a full program that fails, using your code, and let us know what compiler you are using.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,076
    > I'm writing to a file because I don't have ssh when I shut down wifi for that one raspberry pi.
    So how are you running your program from your ssh session?

    If you're just putting it into the background using say
    ./myprog &

    That isn't going to be enough if you then exit ssh.

    You need this.
    Linux nohup command help and examples

    nohup ./myprog &
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Dec 2021
    Posts
    8
    Quote Originally Posted by rstanley View Post
    Post a full program that fails, using your code, and let us know what compiler you are using.

    This is your code, but I added endless while loop and print to file. It also stops working after about 15 minutes. gcc compiler.

    Code:
    #include <stdio.h>
    #include <time.h>
    #include <unistd.h>
     
    int main(void)
    {
       FILE *log = NULL;
       time_t start = 0;
       time_t stop = 0;
       time_t tdif = 0;
     while(1){
       time(&start);
       log=fopen("./log.txt","a");
     
       do{
          time(&stop);
          tdif=difftime(stop, start);
          printf(" %ld, localtime %s\n", tdif, asctime(localtime(&stop)));
          fprintf(log," %ld\n, localtime %s\n", tdif,asctime(localtime(&stop)));
          sleep(1);
       }
       while(tdif<32);
        
       fclose(log);
     }
       return 0;
    }



    Quote Originally Posted by Salem View Post
    > I'm writing to a file because I don't have ssh when I shut down wifi for that one raspberry pi.
    So how are you running your program from your ssh session?

    If you're just putting it into the background using say
    ./myprog &

    That isn't going to be enough if you then exit ssh.

    You need this.
    Linux nohup command help and examples

    nohup ./myprog &
    Yes, I did exactly that. I thought the program will continue runing after ssh pipe breaks. Of course, later I planned to start it at startup. I'll try this.

  10. #10
    Registered User
    Join Date
    Dec 2021
    Posts
    8
    Seems to be working with nohup. I'll try a longer test.

  11. #11
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    946
    Quote Originally Posted by GhostLoveScore View Post
    This is your code, but I added endless while loop and print to file. It also stops working after about 15 minutes. gcc compiler.
    I too do not use a Raspberry Pi system, don't know what O/S you are using, nor do I know why it should be ending after 15 minutes. Is there some power setting in the O/S that puts the O/S in sleep mode or shutting down the O/S?

  12. #12
    Registered User
    Join Date
    Dec 2021
    Posts
    8
    nohup gave me something to work with. Now it works longer than 15 minutes. This time it stopped after about 3 hours, and nohup's output log says "sigHandler: Unhandled signal 18, terminating".
    This seems to be related to pigpio library. I'm unable to start the program again after this. Only after I reboot the system.

    Code:
    2021-12-28 22:25:41 initInitialise: Can't lock /var/run/pigpio.pid
    2021-12-28 22:25:41 serOpen: pigpio uninitialised, call gpioInitialise()
    
    

    Quote Originally Posted by rstanley View Post
    I too do not use a Raspberry Pi system, don't know what O/S you are using, nor do I know why it should be ending after 15 minutes. Is there some power setting in the O/S that puts the O/S in sleep mode or shutting down the O/S?
    I don't think so. I think the problem was that process shut down after ssh pipe broke. What I'm getting now may not be the same problem as the one that stopped the script after 15 minutes.
    Last edited by GhostLoveScore; 12-28-2021 at 03:34 PM.

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,076
    Does it work for more than 15 minutes logged in?

    Signal Handler * Issue #6 * fivdi/pigpio * GitHub
    > 2021-12-28 22:25:41 initInitialise: Can't lock /var/run/pigpio.pid
    It looks like a resource leak of some kind, to the point you eventually run out of "something" and then it stops.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  14. #14
    Registered User
    Join Date
    Dec 2021
    Posts
    8
    Quote Originally Posted by Salem View Post
    Does it work for more than 15 minutes logged in?

    Signal Handler * Issue #6 * fivdi/pigpio * GitHub
    > 2021-12-28 22:25:41 initInitialise: Can't lock /var/run/pigpio.pid
    It looks like a resource leak of some kind, to the point you eventually run out of "something" and then it stops.
    Yes, now it works fine for a couple of hours. I'm testing it again. Unfortunately, if it will crash, now it will crash after couple of hours.

    I thought so too. It runs out of something. I've now updated my code so that I don't use pigpio library for serial communication. Instead I'm writing directly to port as described here ​Linux Serial Ports Using C/C++ | mbedded.ninja

  15. #15
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    946
    Quote Originally Posted by GhostLoveScore View Post
    Yes, now it works fine for a couple of hours. I'm testing it again. Unfortunately, if it will crash, now it will crash after couple of hours.

    I thought so too. It runs out of something. I've now updated my code so that I don't use pigpio library for serial communication. Instead I'm writing directly to port as described here ​Linux Serial Ports Using C/C++ | mbedded.ninja
    Have you tried to use vlagrind to detect any memory errors? You can write errors and warnings to a log file to examine after the program ends.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C stops working ad 2nd scanf
    By Alcatraz in forum C Programming
    Replies: 4
    Last Post: 09-05-2017, 09:26 PM
  2. Program stops working when it gets to function
    By Joshino520 in forum C Programming
    Replies: 2
    Last Post: 01-19-2017, 09:00 AM
  3. Program suddenly stops working.
    By KittenAqua in forum C Programming
    Replies: 5
    Last Post: 10-18-2011, 05:45 AM
  4. compiler stops working
    By akkiphadnis in forum C Programming
    Replies: 2
    Last Post: 04-04-2011, 03:42 AM
  5. Program stops working as soon as I input value?..
    By darkmagic in forum C++ Programming
    Replies: 1
    Last Post: 03-08-2011, 02:18 AM

Tags for this Thread