preda2or's blog

By preda2or, history, 21 month(s) ago, In English

Many users who use sublime text to code in C++ might have faced the infinite loop problem in which the system freezes and the only way out is to restart the machine. If this happens during an ongoing contest it wastes a lot of time. I have a permanent fix for this problem which will safeguard the program from accidentally running into infinite loop.

Sublime C++14 build with timeout : LINK

You can test the above build by running this code.

#include <bits/stdc++.h>   
using namespace std;   

int main() {   
    ios::sync_with_stdio(0);   
    cin.tie(0);   
   
    #ifndef ONLINE_JUDGE   
    freopen("input.txt", "r", stdin);   
    freopen("output.txt", "w", stdout);   
    #endif   
    
    while (true) {   
        cout << "Hello World\n";   
    }   
        
}   

Alternative C++14 Build which will take input from input.txt and store output in output.txt files with timeout: LINK

You can test the above build by running this code.

#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    
    while (true) {
        cout << "Hello World\n";
    }
    
}

This Sublime build will stop executing the program if it's execution time goes more than 0.5s, thus preventing the system freeze. Usually when we run a sample test in our local machine this limit of 0.5s is more than enough. You can set the timeout to a higher value. 0.5s works fine in my machine.

I hope some users might find this useful.

P.S — I have tried these builds in linux(Ubuntu).

 
 
 
 
  • Vote: I like it
  • +11
  • Vote: I do not like it

»
21 month(s) ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by preda2or (previous revision, new revision, compare).

»
21 month(s) ago, # |
  Vote: I like it -17 Vote: I do not like it

jaake aaram kar bhai , kitna code karega ! sublime problem karta hai to codeblocks ya eclipse use kar.

Nahi to Clion .

»
21 month(s) ago, # |
  Vote: I like it +2 Vote: I do not like it

Nice! I love this post!

»
19 months ago, # |
  Vote: I like it +6 Vote: I do not like it

Nice, it helped me a lot :) though you don't have to restart your machine , you can kill that process through terminal using kill command.

step 1 : use 'top' command to see the process id

step 2 : use 'kill pid' command to kill that process(replace pid with process id you found in step 1)

it is similar to task manager in windows :p

  • »
    »
    19 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Well i knew this method but my machine is low on storage memory. So I had to do it really fast otherwise the size of the ouptut file (in case the code is printing something) became so large that the entire system became unresponsive.

»
15 months ago, # |
Rev. 2   Vote: I like it +5 Vote: I do not like it

Or u can do this

adding this { "keys": ["ctrl+q"], "command": "cancel_build" } line to key bindings and press ctrl+q to stop infinite loop.

»
15 months ago, # |
  Vote: I like it 0 Vote: I do not like it

I think, that doesn't detect hangups in the program (i.e it won't kill the program if it is hung). Also your program may take too much memory and block the RAM and crush your PC. This script offers more functionalities. You can limit the memory usages too. I use it like this: timeout --no-info-on-success --detect-hangups -m 1000000 -t 10 executable_file.

»
6 months ago, # |
  Vote: I like it -42 Vote: I do not like it

I am watching this during a online contest ->xd

»
6 months ago, # |
  Vote: I like it 0 Vote: I do not like it
  • »
    »
    6 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    thanks dude,this helped a lot!

»
6 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Nice work !!

»
6 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Awesome dude.. This was the reason why i stopped using sublime. Gotta start using it again now...!!

»
6 months ago, # |
  Vote: I like it 0 Vote: I do not like it

How to make it suitable for work in windows??

»
6 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Can't we simply use the CANCEL BUILD option under TOOLS, we can set the keyboard shortcut for the same as follows: Preferences -> Key Bindings --> Then Paste this [{ "keys" : ["ctrl+alt+r"], "command": "cancel_build"}]. I use this for Python in Sublime Text.

  • »
    »
    6 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Yes You Can, but suppose if u try to break after 10-12 sec after the execution of the program then it will not work properly, thats the drawback of cancel build

»
6 months ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

thanks

»
6 months ago, # |
Rev. 4   Vote: I like it 0 Vote: I do not like it

Build not working Guys ,,, any help ... i want program itself to stop after some time !!!

»
6 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Desperately needed, Thanks for the blog

»
5 months ago, # |
  Vote: I like it 0 Vote: I do not like it

I am Getting the Following Error on Building with above Code.Can Anyone Suggest me the Solution?

[WinError 2] The system cannot find the file specified

[cmd: ['bash', '-c', "g++ -std=c++14 'C:\Users\Admin\Desktop\Check\Simple.cpp' -o 'C:\Users\Admin\Desktop\Check/Simple' && timeout 0.5s 'C:\Users\Admin\Desktop\Check/Simple' <input.txt >output.txt"]]

[dir: C:\Users\Admin\Desktop\Check]

[path: C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\Git\cmd;C:\Users\Admin\AppData\Local\Programs\Python\Python38;C:\Program Files\nodejs\;C:\MinGW\bin;C:\Users\Admin\AppData\Local\Programs\Python\Python38\Scripts\;C:\Users\Admin\AppData\Local\Programs\Python\Python38\;C:\Users\Admin\AppData\Local\Microsoft\WindowsApps;C:\flutter\flutter\bin;C:\Users\Admin\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\Admin\AppData\Roaming\npm;C:\Users\Admin\AppData\Local\GitHubDesktop\bin;C:\MinGW\bin;]

[Finished]

  • »
    »
    5 months ago, # ^ |
      Vote: I like it +3 Vote: I do not like it

    So there is another way

    Spoiler
    • »
      »
      »
      5 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Yes, You're Right! Got Frustrated from Sublime due to Freezing Issue ans Switched to VS Code..Its Just Awesome with Great Plugins and Now I am able to Code and Debug Faster.

      Always_Use
»
5 months ago, # |
  Vote: I like it 0 Vote: I do not like it

I use sublime. Here's how I do it: I installed the plugin Terminal, and made a small script (and added it to $PATH) which compiles and then runs the code in the terminal. Now to test my solution, all I have to do is to press Ctrl+Shift+T (to open the terminal) and run [script_name] [file_name] (and if I did it recently, then I don't even need to type it, pressing the up arrow key brings it up). Since everything runs in the terminal, if it takes too long, I can just Ctrl+C it, so no worries about infinite loop.

»
5 months ago, # |
Rev. 3   Vote: I like it 0 Vote: I do not like it

so where do I paste that cmd code ...

»
3 months ago, # |
  Vote: I like it 0 Vote: I do not like it

can you show your "- "cmd":["bash", "-c", "g++ -std=c++14 -Wall '$$${file}' -o '$$${file_path}/$$${file_base_name}' && timeout 0.5s '$$${file_path}/${file_base_name}'"], i mean how you filled this file_path and file_base_name in your new build file.

»
6 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

thank you!! my laptop used to freeze out before i could kill the process, i got frustrated today and found your blog :)

»
3 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

simply go in Task Manager and close the output.txt file .... Simple as that.