preda2or's blog

By preda2or, history, 5 years 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

| Write comment?
»
5 years 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).

»
5 years ago, # |
  Vote: I like it +2 Vote: I do not like it

Nice! I love this post!

»
5 years 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

  • »
    »
    5 years 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.

»
5 years 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.

»
5 years 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.

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Nice work !!

»
4 years 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...!!

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

How to make it suitable for work in windows??

»
4 years 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.

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

thanks

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Desperately needed, Thanks for the blog

»
4 years 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]

»
4 years 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.

»
4 years 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 :)

»
4 years 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.

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

can't you just press ctrl + c, and then enter(^C) ?

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

Thanks.

»
2 years ago, # |
  Vote: I like it -10 Vote: I do not like it

I think 2 sec would be better to use