#!/bin/bash
# Function to kill script and its child processes
kill_script() {
    local parent_pid=$1

    # Get all child processes of the parent PID
    child_pids=$(pgrep -P $parent_pid)

    # Kill the parent and all its children
    pkill -TERM -P $parent_pid

    # Wait for processes to be killed
    sleep 5

    # Forcefully kill any remaining processes
    pkill -KILL -P $parent_pid
}

model=$1
round=$2

./run_AFL_loop_v6.sh /home/debian/SearchGEM5/src/custom_mutators/ /home/debian/AFLplusplus/afl-fuzz /home/debian/experiment/$model-cmin/input/ \
	/home/debian/experiment/$model-cmin/output-r_$round 0 /home/debian/gem5-ssbse-challenge-2023/build/ARM/gem5.opt > afl-$round-$model.txt 2>&1 &

# Capture the process ID of the background process
script_pid=$!

# Sleep for 24 hours
sleep 86400

# Kill the script after 60 minutes
kill_script $script_pid
