Mininet环境下n个任务在周期T内均匀调度的技术咨询
Alright, let's figure out how to get those tasks evenly scheduled across your 10 Mininet hosts within that 180-second window. I've worked through similar setups before, so here's a practical, step-by-step approach tailored to your scenario:
First, let's nail down the timing math to ensure uniform distribution. You've got n tasks (each 2 seconds long) that need to finish within 180 seconds.
- Global start interval: To spread all tasks evenly across the full 180-second window, the time between starting consecutive tasks should be
interval = 180 / n. This works because:- The first task kicks off at 0 seconds
- The last task starts at
(n-1)*interval, which will be ≤ 178 seconds (sincen*interval = 180, so(n-1)*interval = 180 - interval; even ifn=90, interval=2, so last start time is 178s—perfect for a 2s task to finish at 180s)
- If you'd rather handle per-host scheduling (each host spreads its own assigned tasks evenly), for a host with
ktasks, its internal interval ishost_interval = 180 / k.
This is the cleanest way to ensure every task is evenly spaced across the entire 180-second window, regardless of which host it's on.
Step 2.1: Map Tasks to Timings & Hosts
First, create a list that pairs each task with:
- Its assigned Mininet host (e.g.,
h1,h2) - Its exact start time (
start_time = task_index * interval, where task_index ranges from 0 to n-1)
Step 2.2: Implement with Mininet API & Python
You can use the Mininet Python API to schedule tasks directly from the control node. Here's a stripped-down example (adjust it to match your actual task commands and host setup):
from mininet.net import Mininet import time import threading def run_task_scheduled(host, task_command, start_time): # Wait until the scheduled start time wait_duration = start_time - time.time() if wait_duration > 0: time.sleep(wait_duration) # Execute the task on the host host.cmd(f"{task_command} &") if __name__ == '__main__': # Initialize Mininet with 10 hosts (adjust if your host names differ) net = Mininet() hosts = [net.addHost(f'h{i+1}') for i in range(10)] net.start() # Configuration (replace with your actual values) total_tasks = 90 # Example: 9 tasks per host cycle_duration = 180 task_command = "./your_task_executable" # Replace with your task's command # Calculate global interval interval = cycle_duration / total_tasks # Generate task schedule (replace with your actual task-host assignments) task_schedule = [] task_idx = 0 for host in hosts: # Assign 9 tasks to each host (adjust based on your real distribution) for _ in range(9): start_time = task_idx * interval task_schedule.append( (host, task_command, start_time) ) task_idx += 1 # Launch all scheduled tasks in separate threads for task in task_schedule: host, cmd, st = task threading.Thread(target=run_task_scheduled, args=(host, cmd, st)).start() # Wait for all tasks to complete before stopping Mininet time.sleep(cycle_duration) net.stop()
If global scheduling feels overkill, you can let each host handle its own task distribution. This is simpler if your task assignments are already fixed.
Step 3.1: Create Per-Host Bash Scripts
For each host with k tasks, create a script like this (save it as schedule_tasks.sh):
#!/bin/bash # Replace these values with the host's task count and your command TASK_COUNT=9 TASK_CMD="./your_task_executable" CYCLE=180 INTERVAL=$((CYCLE / TASK_COUNT)) # Schedule each task with the calculated interval for i in $(seq 0 $((TASK_COUNT - 1))); do sleep $((i * INTERVAL)) $TASK_CMD & done # Wait for all background tasks to finish wait
Step 3.2: Deploy & Run Scripts in Mininet
From the Mininet control node, copy the script to each host (adjusting the task count for each) and execute it:
# Example for h1 with 9 tasks h1.cmd("echo '#!/bin/bash\nTASK_COUNT=9\nTASK_CMD=\"./your_task_executable\"\nCYCLE=180\nINTERVAL=$((CYCLE / TASK_COUNT))\nfor i in $(seq 0 $((TASK_COUNT - 1))); do\nsleep $((i * INTERVAL))\n$TASK_CMD &\ndone\nwait' > schedule_tasks.sh") h1.cmd("chmod +x schedule_tasks.sh && ./schedule_tasks.sh &") # Repeat for h2-h10 with their respective task counts
- Sync Host Clocks: Make sure all Mininet hosts have synchronized system time (run
ntpdate pool.ntp.orgon each host before starting scheduling to avoid timing drift). - Buffer for Task Variance: If your tasks sometimes take a little longer than 2 seconds, tweak the interval slightly (e.g.,
interval = 180 / n + 0.1) to add a small buffer. - Avoid Resource Spikes: Global scheduling is better if you want to prevent multiple tasks from starting at the same time (which could cause CPU/network spikes on a single host).
内容的提问来源于stack exchange,提问作者Niloy Saha

