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).
Auto comment: topic has been updated by preda2or (previous revision, new revision, compare).
Nice! I love this post!
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
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.
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.where to add these lines??
Thanks, I am so happy after configuring the above, that i won't have to restart on a wrong code It works!
Where to include this script bro?
It is clearly stated in the comment. Look carefully.
Didn't find it. Will be thankful if you could help.
https://mirror.codeforces.com/blog/entry/65048?#comment-526915 and https://mirror.codeforces.com/blog/entry/65048?#comment-608292
I was asking about the script mentioned in the main blog.
These are just key-bindings that are default.
C++ Sublime Build (windows)
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
.Nice work !!
Awesome dude.. This was the reason why i stopped using sublime. Gotta start using it again now...!!
Or u can simply " cancel build " .
How to make it suitable for work in windows??
use the method mentioned by srinivas1999 (above in this blog)
Can't we simply use the
CANCEL BUILD
option underTOOLS
, 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.thanks
Desperately needed, Thanks for the blog
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]
So there is another way
install vs code
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.
thank you!! my laptop used to freeze out before i could kill the process, i got frustrated today and found your blog :)
simply go in Task Manager and close the output.txt file .... Simple as that.
would you please give us screenshot!
can't you just press ctrl + c, and then enter(^C) ?
Thanks.
I think 2 sec would be better to use