简体   繁体   English

如何使用多线程使系统崩溃

[英]How to crash system using Multithreading

I start learning Multi-threading in C++ and I' m trying to crash and block my system by occupying all the processors. 我开始学习C ++中的多线程,并且试图通过占用所有处理器来崩溃并阻塞系统。 As fact I tried to create many threads and run them, but I didn't get what I need 实际上,我尝试创建许多线程并运行它们,但没有得到所需的东西

void func()
{
    std::cout << "C++11 MULTITHREADING\n";
    for (int i = 1; i < INT_MAX; i++)
    {
        for (int j = 1; j < INT_MAX; j++)
            std::cout << i / j << " ";
    }
}


int main()
{

    for (int i = 0; i < INT_MAX; i++)
    {
        std::thread t(func);
        t.join();
    }
    std::cout << " ***END OF A PROGRAM***\n";

    return 0;
}
t.join();

Is going to "Join" the thread you just created into the current thread. 将“加入”刚刚创建的线程到当前线程中。 This means that you will run the entire function and return before you create the next thread. 这意味着您将运行整个函数并在创建下一个线程之前返回。 It would be the same as doing: 它与执行操作相同:

for (int i = 0; i < INT_MAX; i++)
{
    func();
}

If you want to spawn multiple threads and run them then store them in a std::vector and then call join() on all of them after you create them. 如果要产生多个线程并运行它们,则将它们存储在std::vector ,然后在创建它们后在所有它们上调用join()

std::vector<std::thread> threads;
threads.reserve(INT_MAX);  // save reallocations
for (int i = 0; i < INT_MAX; i++)
{
    threads.push_back(std::thread(func));
}

for (int i = 0; i < INT_MAX; i++)
{
    threads[i].join();
}

As Basile Starynkevitch's answer points out you will need to create less threads to avoid system errors. 正如Basile Starynkevitch的 答案指出的那样,您将需要创建更少的线程来避免系统错误。

You can't expect to create a lot (eg millions) of threads successfully (eg because each thread needs some call stack space, typically a few megabytes per thread). 您不能指望成功创建大量(例如数百万个) 线程 (例如,因为每个线程都需要一些调用堆栈空间,每个线程通常需要几兆字节)。 You typically can create only a few hundreds, or a few thousands of threads. 通常,您只能创建数百个或数千个线程。 So INT_MAX is not reasonable, for the number of threads (1000 or 100 should be enough). 因此对于线程数(1000或100应该足够), INT_MAX是不合理的。

Threads are a quite costly resource, probably more heavy that an opened file descriptor . 线程是非常昂贵的资源,可能比打开的文件描述符还要重。 In practice, you want only a dozen of threads. 实际上,您只需要十几个线程。

Then if you want to overload your computer, you should not join a thread as soon as you have created it. 然后,如果您想使计算机过载,则不应在创建线程后立即加入该线程。 You want to have all threads running in parallel, and join once every thread has been created. 您希望所有线程并行运行,并在创建每个线程后加入。

So use Nathan Oliver's answer but replace INT_MAX by MY_THREADS_MAX after adding 因此,请使用Nathan Oliver的答案,但在添加INT_MAX MY_THREADS_MAX替换为MY_THREADS_MAX

 #define MY_THREADS_MAX 1000

(or even less, probably 50 should be more than enough) (甚至更少,大概50应该足够了)

If on Linux, see also setrlimit(2) ; 如果在Linux上,另请参见setrlimit(2) probably, the default limits prohibit you from crashing the system (unless you are root) but might make it unresponsive. 可能是,默认限制禁止您使系统崩溃(除非您是root用户),但可能会使它无响应。

It could happen that on your system, you'll need special administrator privileges to crash your computer. 可能会发生在系统上,您需要特殊的管理员特权才能使计算机崩溃。

As @NathanOliver suggested you'll want to finish running your program rather than just detaching it. 正如@NathanOliver所建议的那样,您将要完成程序的运行,而不仅仅是分离它。

But you should also consider using high-priority processes. 但是,您还应该考虑使用高优先级的过程。 This implementation is platform-specific, but you can check out some good answers: 该实现是特定于平台的,但是您可以检查出一些好的答案:

What is the 'realtime' process priority setting for? “实时”过程优先级设置是什么? or http://www.nixtutor.com/linux/changing-priority-on-linux-processes/ http://www.nixtutor.com/linux/changing-priority-on-linux-processes/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM