Блог пользователя riela

Автор riela, история, 6 лет назад, По-английски

Some weeks ago, I was coding a solution in c++, and by mistake I was adding an infinite amount of numbers to a vector. Suddenly, my computer got frozen, and it didn't respond to keyboard neither mouse events. I had to reset my computer, and it took a lot of time fixing the disk.

Did you had similar experiences during important contests?

Is there a compìler configuration to prevent this kind of scenarios?

  • Проголосовать: нравится
  • +51
  • Проголосовать: не нравится

»
6 лет назад, # |
  Проголосовать: нравится -24 Проголосовать: не нравится

Sometimes my laptop crashes when I open up facebook. Never had any issue with programs though. I'm pretty sure the program's resources are limited by the system to prevent things like this from happening.

Are you using linux by any chance?

  • »
    »
    6 лет назад, # ^ |
      Проголосовать: нравится +1 Проголосовать: не нравится

    yes, I'm on linux, I compile and execute from the terminal

    • »
      »
      »
      6 лет назад, # ^ |
        Проголосовать: нравится -103 Проголосовать: не нравится

      I have only ever had bad experiences with linux, maybe you should switch to proprietary operating systems.

      • »
        »
        »
        »
        6 лет назад, # ^ |
          Проголосовать: нравится +16 Проголосовать: не нравится

        that is not a solution! ioi and icpc use both linux, so it is a good idea to get familiar with the environment

      • »
        »
        »
        »
        6 лет назад, # ^ |
        Rev. 2   Проголосовать: нравится +55 Проголосовать: не нравится

        I have only ever had good experiences with Linux. Maybe you should consider buying the bridges I'm selling.

        (Also, when your laptop now crashes because you open a site, I don't want to know what are "bad experiences" compared to that.)

        • »
          »
          »
          »
          »
          6 лет назад, # ^ |
            Проголосовать: нравится -43 Проголосовать: не нравится

          I had a lot of pretty nasty experiences trying to configure ubuntu (which is apparently supposed to be user-friendly).

          My laptop is 4 years old macbook pro and it craps out a fair bit (the facebook thing happened just now). I don't like it at all either. That's why I don't do important work on it.

          My desktop is 2 years old on windows and it has only crashed a couple times so far. Everything just works out the box.

          • »
            »
            »
            »
            »
            »
            6 лет назад, # ^ |
              Проголосовать: нравится +33 Проголосовать: не нравится

            it has only crashed a couple times so far

            Is this really the standard we should be setting for proprietary OSes...

            • »
              »
              »
              »
              »
              »
              »
              6 лет назад, # ^ |
              Rev. 5   Проголосовать: нравится -26 Проголосовать: не нравится

              it is better than having no standard at all for personal-use linux (of course, I'm not talking about general server/business usage where there are support teams and companies maintaining distributions and such)

              Plus, a crash in every couple of months to half-year isn't unbearably bad

          • »
            »
            »
            »
            »
            »
            6 лет назад, # ^ |
              Проголосовать: нравится -12 Проголосовать: не нравится

            Exactly nothing is easy to install. Nothing actually works on Ubuntu. Videos freeze time after time. Now I can't hear from the left earpiece (while on win10 it is working) out of nowhere.

            • »
              »
              »
              »
              »
              »
              »
              6 лет назад, # ^ |
              Rev. 2   Проголосовать: нравится -14 Проголосовать: не нравится

              Yeah, I tried to use ubuntu again recently and got hit by an avalanche of problems, chiefly the mouse losing focus and not clicking stuff behind a "stuck" application. I found the perfect stackoverflow answer, except it was for a different mouse brand and it didn't help at all...

              I find it interesting that there are so many proprietary haters here when in linux apparently your program can force you to reset your whole computer (how is that even remotely acceptable?!!?!??!?!), and the whole thing needs a force shutdown from an infinite loop. But apparently a crash every few months is a "bad standard". In my mind, it's better than losing your whole disk...

              I don't see why people hate the thought of proprietary os so much. Ok, some people can use linux successfully, grind through each error, and enjoy using it, but that doesn't mean switching is necessarily a bad idea.

»
6 лет назад, # |
  Проголосовать: нравится +8 Проголосовать: не нравится

Something similar happened to me once. One way to get around it is to limit memory per process and set timeout

»
6 лет назад, # |
  Проголосовать: нравится +23 Проголосовать: не нравится

ulimit -a will show you various limits. If you have unlimited stack size limit, I recommend changing it to e.g. 256MB (ulimit -s 256000). There are also some other limits for memory, but I never used/changed them.

»
6 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Many times due to infinite loop entire ubuntu stops responding. Then force shutdown, using power button is the only option.

  • »
    »
    6 лет назад, # ^ |
      Проголосовать: нравится +11 Проголосовать: не нравится

    Seems very strange. Sounds like infinite recursion and thus growing memory consumption. An infinite loop shouldn't take much resources, I think.

  • »
    »
    6 лет назад, # ^ |
      Проголосовать: нравится +22 Проголосовать: не нравится

    Tip: Often it is enough just to wait few minutes until the OOM rips your process.

    • »
      »
      »
      6 лет назад, # ^ |
      Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

      Does this work on debian based distros like ubuntu? I have seen much discussion on OOM killer not invoking on Ubuntu 14 and above.

      PS: I decided to give it a try and I am still waiting for OOM killer to kick in while writing this comment using mobile. Hope it gets invoked.

  • »
    »
    6 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    Sublime C++14 Build

    This Sublime build will stop executing the program if it's execution time goes more than 0.5s, thus saving you the trouble of restarting your machine. You can set the timeout to a higher value. 0.5s works fine in my laptop.

»
6 лет назад, # |
  Проголосовать: нравится +22 Проголосовать: не нравится

As I said much much earlier, advanced technology so often go wrong. You can never go wrong with notepad. Replace .txt with .cpp and submit. Now you might wonder: "what if i make bug?" Well tourist once said, best way to debug is to not make bug in first place. Notepad train your mind, and your coding skill, no downsides.

  • »
    »
    6 лет назад, # ^ |
      Проголосовать: нравится +2 Проголосовать: не нравится

    What about IOI? It uses linux so I should get used to it.

    • »
      »
      »
      6 лет назад, # ^ |
        Проголосовать: нравится +2 Проголосовать: не нравится

      Well, if you use notepad you can adapt to all environments ;)

      • »
        »
        »
        »
        6 лет назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

        Um, why are you talking to yourself?

        • »
          »
          »
          »
          »
          6 лет назад, # ^ |
            Проголосовать: нравится 0 Проголосовать: не нравится

          Well although I address his concerns, I also realize I did not answer the question about how this is useful for linux. So I decided to ask the poster (me). Of course, since I asked myself I also have to answer myself.

  • »
    »
    6 лет назад, # ^ |
      Проголосовать: нравится +7 Проголосовать: не нравится

    ... and brdy says that real programmers use notepad lol

    is good to get used to linux, maybe you can't even open notepad (or maybe you can realize that there is no notepad at all!)

    also it seems you don't undersand the problem, even if you code in "notepad",after compiling and executing it can crash your computer because a bug, and yes even tourist is human and write some bugs.

    IDEs are also a good thing (at least for java). I think petr could decrease his performance if he codes in notepad

    • »
      »
      »
      6 лет назад, # ^ |
        Проголосовать: нравится +12 Проголосовать: не нравится

      Sorry if I was not clear enough, as the technique I mentioned is very advance.

      The basic idea is that you cannot crash your computer if you never run your code in the first place. So that is why I say we use notepad, and then convert it to a cpp file submit it. Of course, we do not need to debug if do not make bugs in the first place, and thus we never need to run our code locally.

      If any other questions & concerns I will be in touch. Have nice day.

      • »
        »
        »
        »
        6 лет назад, # ^ |
          Проголосовать: нравится +64 Проголосовать: не нравится

        very advanced stuff, maybe I'll try that after reaching blue

        • »
          »
          »
          »
          »
          6 лет назад, # ^ |
            Проголосовать: нравится -18 Проголосовать: не нравится

          Why not use it now? It solves your problem, now your computer will not crash.

      • »
        »
        »
        »
        6 лет назад, # ^ |
          Проголосовать: нравится -7 Проголосовать: не нравится

        But why to use .txt at first place then? Use some editor with .cpp extension only and then directly submit that without running it.

        • »
          »
          »
          »
          »
          6 лет назад, # ^ |
            Проголосовать: нравится -8 Проголосовать: не нравится

          You sir, are genius. I congratulate you. But for some users this optimization is too advanced, to we start with the basics (.txt files)

»
6 лет назад, # |
  Проголосовать: нравится +18 Проголосовать: не нравится

But have you ever made a bug that crashed your OS? The one you described just caused slow or exhausted swap (maybe it was zero-size?). The OS didn’t crash until you hard reset it.

»
6 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

I didn't have a bug, but I was testing a function for a linked list for 2 billion nodes. Apparently, 2 billion nodes was too much (but I didn't know that!), and I had the same thing happen to me. It froze after about 10 million, and bad things happened. I'd be curious to see if there's a way to prevent these scenarios.

  • »
    »
    6 лет назад, # ^ |
      Проголосовать: нравится -8 Проголосовать: не нравится

    I'd be curious to see if there's a way to prevent these scenarios.

    Get a supercomputer?

»
6 лет назад, # |
Rev. 2   Проголосовать: нравится +14 Проголосовать: не нравится

you can use setrlimit syscall on linux.

#include <vector>                                                                                     
#include <sys/resource.h>                                                                             

int main() {                                                                                          
  rlimit cpu_time{.rlim_cur = 1, .rlim_max = 2};                                                      
  setrlimit(RLIMIT_CPU, &cpu_time);                                                                   

  std::vector<int> v;                                                                                      
  while (true) v.push_back(1);                                                                        
}

this program would not be allowed to consume more than 2 seconds (rlim_max) of CPU time. You can set other limits too. Reference: http://man7.org/linux/man-pages/man2/setrlimit.2.html

»
6 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

This happened, because of the way C++ std::vector works internally. It allocates an array of objects of required size dynamically (on heap). When the array becomes full (using push_back ), it allocates one with double the original size, and copies all the elements. Since heap size is bounded only by the size of RAM (or virtual memory) on your PC, it kept allocating infinite memory leading to lack of space of all other processes, and crash. So, a solution to this would be 1. Not having a vector (or any container) of objects of size more than 10^7. 2. Keeping time and memory limits on your code like others have suggested. 3. Keeping your memory usage in check.

»
6 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

I have a problem during the last subregional of ICPC South America. 15 minutes left to finish the contest a bug caused the computer to got frozen. It took a good time to reset the computer.

»
6 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

It happened to me 2 years ago in an offline contest.

I wrote a code that generate Fibonacci strings (i.e. s1 = "a", s2 = "b", sn = sn - 2 + sn - 1, the limit of n was such high that the string might have about 1018 characters, if enough memories could be provided).

And when I ran it, the computer I used froze immediately, forcing me to force-restart it :D

  • »
    »
    6 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    Why doesn't the OS provide anything to control the amount of memory used by a process?

    • »
      »
      »
      6 лет назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      I'm not even sure that it was my fault or a notorious coincidence, but yup, it did happen.

      Tested it again several times with my personal laptop, and none of the time it froze like expected (RAM usage always at approx. 1GB)