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).
jaake aaram kar bhai , kitna code karega ! sublime problem karta hai to codeblocks ya eclipse use kar.
Nahi to Clion .
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??
cooldude_69 Go to Preferences -> Key Bindings.
shr3y4sh
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://codeforces.com/blog/entry/65048?#comment-526915 and https://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.
But,it works!
C++ Sublime Build (windows)
I tried but it didn't work
thanks a lot it was like a infinity stone for me
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
.I am watching this during a online contest ->xd
puns
thanks dude,this helped a lot!
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.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
thanks
batana bhai?
Build not working Guys ,,, any help ... i want program itself to stop after some time !!!
Exactly , build is not working in my pc too? what to do?
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
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.
VS Code is Great Editor which never Breakdown(Freezes) even If I Write Infinite Loop!
Hey, I am using VS code and I am facing the same issue, my pc is getting hung and I have to restart it when I accidently implement a while loop, can someone help me please?
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.
so where do I paste that cmd code ...
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.
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.
Is there any way to stop the infinite loop in VS code?
above build is not working in my pc, i don't know why , my output file size become 4GB
I think 2 sec would be better to use
i have error of this type after some change in path. any suggestion???
ERROR: Invalid syntax. Default option is not allowed more than '1' time(s). Type "TIMEOUT /?" for usage. [Finished in 1.8s with exit code 1] [shell_cmd: g++ -std=c++14 "C:\Users\samad\Desktop\c++\vector\pairs.cpp" -o "C:\Users\samad\Desktop\c++\vector/pairs" && timeout 5.0s "C:\Users\samad\Desktop\c++\vector/pairs"] [dir: C:\Users\samad\Desktop\c++\vector] [path: C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files\Git\cmd;C:\MinGW\bin;;C:\Users\samad\AppData\Local\Programs\Microsoft VS Code\bin]
How to run this in windows?? Saying /bin/bash: g++: command not found
links are not working. Can anyone please provide me the build code?
In MacOs if timeout is not working than you have to install this package additionally. Coreutils