循环计划批处理脚本的问题 - 等待和周转时间计算不正确

Issue with Round Robin Scheduling Batch Script - Incorrect Calculation of Waiting and Turnaround Times

提问人:Amanari 提问时间:11/17/2023 最后编辑:CompoAmanari 更新时间:11/17/2023 访问量:43

问:

我在批处理文件中为给定数量的进程、突发时间和时间量程实现了一个循环调度算法。

Reference

但是,输出显示等待时间和周转时间的计算不正确。

CMD Preview

我需要帮助来识别和修复问题。

@echo off
setlocal enabledelayedexpansion

REM Round Robin Scheduling Batch File

REM Input: Number of processes
set /p n=Enter the number of processes: 

REM Initialize arrays to store burst times, waiting times, and turnaround times
set "burst_times="
set /a total_waiting_time=0
set /a total_turnaround_time=0

REM Input: Burst times for each process
for /L %%i in (1,1,%n%) do (
    set /p "burst=Enter burst time for P[%%i]: "
    set "burst_times=!burst_times! !burst!"
)

REM Input: Time quantum
set /p quantum=Enter the time quantum: 

REM Ensure the input is not empty
if %quantum% lss 1 (
    echo Quantum time must be greater than 0. Please enter a valid quantum time.
    pause
    goto :start
)

echo.
echo Process     ^| Burst Time ^| Waiting Time ^| Turnaround Time
echo ---------------------------------------------------------

REM Perform Round Robin scheduling
for /L %%i in (1,1,%n%) do (
    set /a waiting_time=0
    set /a turnaround_time=0
    set "current_process=P[%%i]"

    for %%t in (!burst_times!) do (
        set /a remaining_burst=!burst!-%%t
        if !remaining_burst! geq 0 (
            set /a waiting_time+=%%t
            set /a turnaround_time=!waiting_time
            set "burst=!remaining_burst!"
            set "burst_times=!burst_times:*%%t=!"
        ) else (
            set /a waiting_time+=%quantum%
            set /a turnaround_time=!waiting_time
            set "burst_times=!burst_times:*%%t=! !burst_times:*%%t=!"
        )
    )

    set /a total_waiting_time+=waiting_time
    set /a total_turnaround_time+=turnaround_time

    echo !current_process!    ^|     !burst!    ^|        !waiting_time!              ^|        !turnaround_time!
)

REM Calculate and display average waiting time and average turnaround time
set /a avg_waiting_time=total_waiting_time/n
set /a avg_turnaround_time=total_turnaround_time/n

echo ---------------------------------------------------------
echo.
echo Average Waiting Time: %avg_waiting_time% ms 
echo Average Turnaround Time: %avg_turnaround_time% ms
pause
批处理文件 循环

评论

0赞 7three 11/17/2023
里面有什么? ?为什么代替 ?/ntotal_waiting_time/n!waiting_timeset "burst_times=!burst_times! !burst!"burst_times[%%i]
1赞 Compo 11/17/2023
您确实知道它仅适用于整数,因此只能返回整数,而不能返回浮点数。平均等待时间永远不会是 ,但是 ;不是,而是.set /aset /a avg_waiting_time=total_waiting_time/nset /a avg_turnaround_time=total_turnaround_time/n4.6426.333326
0赞 7three 11/17/2023
明白了。。。 是。要计算浮点数,您可以使用以下方法:stackoverflow.com/a/20531384/1378342 然后然后用作结果...精度在哪里。nNumber of processesCALL :calc_ 5 !avg_turnaround_time!/!n!!calc_v!5

答: 暂无答案