While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit case $button1 if $n<6 Then GUICtrlSetData($label7,GUICtrlRead($Label7)&@CRLF&"|"&Center_txt(GUICtrlRead($input1),24)&"|"&Center_txt(GUICtrlRead($input2),32)&"|"&Center_txt(GUICtrlRead($input3),32)&"|"&Center_txt(GUICtrlRead($input4),26)&"|") $n=$n+1 $process[$n]=DllStructCreate("char Ten[10];int Tong; int TDiem; int Tgian; int ConLai; int end;"); DllStructSetData($process[$n],"Ten",GUICtrlRead($input1)) DllStructSetData($process[$n],"Tong",int(GUICtrlRead($input2))) DllStructSetData($process[$n],"TDiem",int(GUICtrlRead($input3))) DllStructSetData($process[$n],"Tgian",int(GUICtrlRead($input4))) DllStructSetData($process[$n],"ConLai",Int(GUICtrlRead($input2))-(Int(GUICtrlRead($input3))+Int(GUICtrlRead($input4)))) DllStructSetData($process[$n],"end",0); GUICtrlSetData($input1,Chr(Asc(GUICtrlRead($input1))+1)) Else If $n=6 then GUICtrlSetData($label7,GUICtrlRead($Label7)&@CRLF&"+-----------------+------------------------+-----------------------+-------------------+") MsgBox(0,"Cảnh báo", "Vượt quá cho phép "& $n) GUICtrlSetState($button1,$GUI_DISABLE) EndIf case $button2 GUICtrlSetState($button1,$GUI_DISABLE) GUICtrlSetState($button2,$GUI_DISABLE) FileDelete("log_fifo.txt") $file = FileOpen("log_fifo.txt", 129)
; Check if file opened for writing OK If $file = -1 Then MsgBox(0, "Lỗi", "Không thể mở file log.txt") Exit EndIf
Local $end=false,$t=0,$cpu=0,$i_o=0,$cpu_rah=true,$i_o_rah=true; $t_check=InputBox("Nhập thời gian t=?","Nhập thời gian t=?",0); GUICtrlCreateLabel("*** Xử lí tại thời điểm t="&($t_check)&" ***", 550, 72, 200, 17) $Label17 = GUICtrlCreateLabel("+-------+", 500, 96, 300, 129) For $i=1 to $n step 1 _ArrayAdd($Ready_list,$i) GUICtrlCreateLabel(DllStructGetData($process[$i],"Ten")&": ", (80+$i*50), 620, 100, 17) GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT) GUICtrlCreateGraphic((80+$i*50)+20, 623, 20, 10) GUICtrlSetBkColor(-1, $color[$i]) Next $Ready_list[0]=$n; ArrayPrint($label9,$t,$Ready_list,$Ready_list[0]); While not($end) FileWriteLine($file,"---******Thời điểm: t="&$t&@CRLF) For $j=1 to $n step 1 ;Xử lí CPU if( not($cpu_rah) and (DllStructGetData($process[$cpu],"TDiem")=0) ) Then if int(DllStructGetData($process[$cpu],"end"))=1 Then FileWriteLine($file,"Tiến trình "&DllStructGetData($process[$cpu],"Ten")&" kết thúc phiên làm việc. Trả CPU cho hệ thống"&@CRLF) $cpu_rah=true; Else FileWriteLine($file,"Tiến trình "&DllStructGetData($process[$cpu],"Ten")&" kết thúc trên CPU, Trả CPU hệ thống, Yêu Cầu I/0"&@CRLF) $cpu_rah=true;Trả CPU cho hệ thống _ArrayAdd($Waiting_list,$cpu); FileWriteLine($file,"Đưa tiến trình"&DllStructGetData($process[$cpu],"Ten")&" vào hàng đợi Waiting_List Chờ I/O"&@CRLF) $Waiting_list[0]=$Waiting_list[0]+1; ArrayPrint($label11,$t,$Waiting_list,$Waiting_list[0]) Endif EndIf if ($cpu_rah=true) Then if ($Ready_list[0]>0) Then $cpu=$Ready_list[1]; VeDoThi(1,$cpu,$t,DllStructGetData($process[$cpu],"TDiem")) FileWriteLine($file,"Đưa tiến trình "&DllStructGetData($process[$cpu],"Ten")&" vào CPU "&@CRLF) _ArrayDelete($Ready_list,1); $Ready_list[0]=$Ready_list[0]-1 $cpu_rah=false; ArrayPrint($label9,$t,$Ready_list,$Ready_list[0]) EndIf EndIf ;Xử lí I/O if( not ($i_o_rah) and DllStructGetData($process[$i_o],"Tgian")=0) Then FileWriteLine($file,"Tiến trình "&DllStructGetData($process[$i_o],"Ten")&" kết thúc làm việc trêm tài nguyên, trả lại cho hệ thống"&@CRLF) $i_o_rah=true;Trả tài nguyên cho hệ thống _Arrayadd($Ready_list,$i_o) FileWriteLine($file,"Đưa tiến trình "&DllStructGetData($process[$i_o],"Ten") &" vào hàng đợi Ready_list chờ để kết thúc"&@CRLF) $Ready_list[0]=$Ready_list[0]+1; DllStructSetData($process[$i_o],"TDiem",DllStructGetData($process[$i_o],"Conlai")); FileWriteLine($file,"Tiến trình "&DllStructGetData($process[$i_o],"Ten")&" còn mất "&DllStructGetData($process[$i_o],"Conlai")&" (đvtg) để kết thúc"&@CRLF) DllStructSetData($process[$i_o],"end",1); ArrayPrint($label9,$t,$Ready_list,$Ready_list[0]) Endif if ($i_o_rah) Then if($Waiting_list[0]>0) then $i_o=$Waiting_list[1]; VeDoThi(2,$i_o,$t,DllStructGetData($process[$i_o],"Tgian")) FileWriteLine($file,"Đưa tiến trình "&DllStructGetData($process[$i_o],"Ten")&" vào I/O"&@CRLF) _ArrayDelete($Waiting_list,1) $Waiting_list[0]=$Waiting_list[0]-1; $i_o_rah=false; ArrayPrint($label11,$t,$Waiting_list,$Waiting_list[0]) EndIf EndIf Next if($t=$t_check) Then if (not ($i_o_rah)) then GUICtrlSetData($Label17,GUICtrlRead($Label17)&@CRLF&"Tiến trình "&DllStructGetData($process[$i_o],"Ten")&" làm việc trên I/O") if (not ($cpu_rah)) then GUICtrlSetData($Label17,GUICtrlRead($Label17)&@CRLF&"Tiến trình "&DllStructGetData($process[$cpu],"Ten")&" làm việc trên CPU") for $r=1 to $Ready_list[0] step 1 GUICtrlSetData($Label17,GUICtrlRead($Label17)&@CRLF&"Tiến trình "&DllStructGetData($process[$Ready_list[$r]],"Ten")&" đang chờ CPU") Next for $w=1 to $Waiting_list[0] step 1 GUICtrlSetData($Label17,GUICtrlRead($Label17)&@CRLF&"Tiến trình "&DllStructGetData($process[$Waiting_list[$w]],"Ten")&" đang chờ I/O") Next for $p=1 to $n step 1 if (DllStructGetData($process[$p],"TDiem")=0) and (DllStructGetData($process[$p],"end")=1) then GUICtrlSetData($Label17,GUICtrlRead($Label17)&@CRLF&"Tiến trình "&DllStructGetData($process[$p],"Ten")&" đã kết thúc") EndIf Next EndIf if ($t<$t_check) and (($t+1)>$t_check) Then if (not ($i_o_rah)) then GUICtrlSetData($Label17,GUICtrlRead($Label17)&@CRLF&"Tiến trình "&DllStructGetData($process[$i_o],"Ten")&" làm việc trên I/O") for $w=1 to $Waiting_list[0] step 1 GUICtrlSetData($Label17,GUICtrlRead($Label17)&@CRLF&"Tiến trình "&DllStructGetData($process[$Waiting_list[$w]],"Ten")&" đang chờ I/O") Next Else if $Waiting_list[0]>0 then GUICtrlSetData($Label17,GUICtrlRead($Label17)&@CRLF&"Tiến trình "&DllStructGetData($process[$Waiting_list[1]],"Ten")&" làm việc trên I/O") for $w=2 to $Waiting_list[0] step 1 GUICtrlSetData($Label17,GUICtrlRead($Label17)&@CRLF&"Tiến trình "&DllStructGetData($process[$Waiting_list[$w]],"Ten")&" đang chờ I/O") Next EndIf EndIf if (not ($cpu_rah)) then GUICtrlSetData($Label17,GUICtrlRead($Label17)&@CRLF&"Tiến trình "&DllStructGetData($process[$cpu],"Ten")&" làm việc trên CPU") for $r=1 to $Ready_list[0] step 1 GUICtrlSetData($Label17,GUICtrlRead($Label17)&@CRLF&"Tiến trình "&DllStructGetData($process[$Ready_list[$r]],"Ten")&" đang chờ CPU") Next Else if $Ready_list[0]>0 then GUICtrlSetData($Label17,GUICtrlRead($Label17)&@CRLF&"Tiến trình "&DllStructGetData($process[$Ready_list[1]],"Ten")&" làm việc trên CPU") for $r=2 to $Ready_list[0] step 1 GUICtrlSetData($Label17,GUICtrlRead($Label17)&@CRLF&"Tiến trình "&DllStructGetData($process[$Ready_list[$r]],"Ten")&" đang chờ CPU") Next EndIf EndIf for $p=1 to $n step 1 if (DllStructGetData($process[$p],"TDiem")=0) and (DllStructGetData($process[$p],"end")=1) then GUICtrlSetData($Label17,GUICtrlRead($Label17)&@CRLF&"Tiến trình "&DllStructGetData($process[$p],"Ten")&" đã kết thúc") EndIf Next EndIf if (not ($cpu_rah)) then DllStructSetData($process[$cpu],"TDiem",int(DllStructGetData($process[$cpu],"TDiem"))-1) FileWriteLine($file,"Tiến trình "&DllStructGetData($process[$cpu],"Ten")&" đang xử lí trên CPU"&@CRLF) EndIf if (not ($i_o_rah)) then DllStructSetData($process[$i_o],"Tgian",int(DllStructGetData($process[$i_o],"Tgian"))-1) FileWriteLine($file,"Tiến trình "&DllStructGetData($process[$i_o],"Ten")&" đang xử lí trên tài nguyên"&@CRLF) EndIf $t=$t+1; $end=true for $c=1 to $n step 1 if (DllStructGetData($process[$c],"TDiem")<>0) or (DllStructGetData($process[$c],"end")<>1) then $end=False Next if $end then MsgBox(0,"Kết Thúc","Tổng thời gian thực hiện T="&($t)) FileWriteLine($file,"Kết thúc quá trình tại thời điểm t="&($t)) EndIf WEnd FileClose($file) EndSwitch WEnd Func Center_txt($txt,$len) if $len>StringLen($txt) then For $i=1 to int(($len-StringLen($txt))/2) step 1 $txt=" "&$txt&" " Next endif Return $txt EndFunc Func ArrayPrint($label,$t,$Array,$n) GUICtrlSetData($Label,GUICtrlRead($Label)&@CRLF&Center_txt($t,7)&"|") ;_ArrayDisplay($array) For $j=1 to $n step 1 GUICtrlSetData($Label,GUICtrlRead($Label)&Center_txt(DllStructGetData($process[$array[$j]],"Ten"),10)&"|") Next EndFunc Func VeDoThi($type,$process,$time_vao,$time_thuchien) Local $x,$y,$w,$h $x=60+int($time_vao)*20 $w=int($time_thuchien)*20 $h=10 if($type=1) then;ve tien trinh cpu $y=588 GUICtrlCreateLabel($time_vao,$x-2,578,15,10) GUICtrlSetColor(-1, 0xff) GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT) GUICtrlSetFont(-1, 7, 800, 0, "Tahoma") if $Ready_list[0]=1 Then GUICtrlCreateLabel(($time_vao+$time_thuchien),(60+int($time_vao+$time_thuchien)*20)-2,578,15,10) GUICtrlSetColor(-1, 0xff) GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT) GUICtrlSetFont(-1, 7, 800, 0, "Tahoma") EndIf EndIf if($type=2) then $y=548 GUICtrlCreateLabel($time_vao,$x-2,558,15,10) GUICtrlSetColor(-1, 0xff) GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT) GUICtrlSetFont(-1, 7, 800, 0, "Tahoma") if $Waiting_list[0]=1 Then GUICtrlCreateLabel(($time_vao+$time_thuchien),(60+int($time_vao+$time_thuchien)*20)-2,558,15,10) GUICtrlSetColor(-1, 0xff) GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT) GUICtrlSetFont(-1, 7, 800, 0, "Tahoma") EndIf EndIf GUICtrlCreateGraphic($x, $y, $w, $h) GUICtrlSetBkColor(-1, $color[$process]) EndFunc
Bài viết mới cùng chuyên mục
Bài viết liên quan
Share code điều phối tiến trình theo FIFO Algorithm