Diễn đàn ITK36 - ĐH Sư phạm TPHCM
Bạn có muốn phản ứng với tin nhắn này? Vui lòng đăng ký diễn đàn trong một vài cú nhấp chuột hoặc đăng nhập để tiếp tục.
Diễn đàn ITK36 - ĐH Sư phạm TPHCM

Diễn đàn ITK36 - ĐH Sư phạm TPHCM
Diễn đàn đang nâng cấp ...
 
Trang ChínhTìm kiếmLatest imagesĐăng kýĐăng Nhập

Share | 
 

 Share code điều phối tiến trình theo FIFO Algorithm

Xem chủ đề cũ hơn Xem chủ đề mới hơn Go down 
Tác giảThông điệp
chienthan_16_12
Member cấp 3
chienthan_16_12


Nam
Tổng số bài gửi : 42
Điểm kinh nghiệm : 24505
Danh tiếng : 10
Ngày tham gia : 14/02/2011
Đến từ : Địa Ngục Trần Gian
Tài năng của chienthan_16_12 Danh vọng:42%/1000%
Tài năng:%/100%


Share code điều phối tiến trình theo FIFO Algorithm _
Bài gửiTiêu đề: Share code điều phối tiến trình theo FIFO Algorithm   Share code điều phối tiến trình theo FIFO Algorithm I_icon_minitimeThu Oct 13, 2011 10:13 pm

Chiều lên quê quá
Ko có máy để dev cái giao diện, share lên ai rảnh thì design cái giao diện kiếm vé miễn thi
Sad
Code trên autoit, nhanh gọn nhẹ, link down http://www.autoitscript.com/site/autoit/downloads/
Code:

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
;Thiết kế giao diện
Global $n=0;
Global $process[7];
Global $Ready_list[1]=[0];
Global $Waiting_list[1]=[0];
Global $color[7]=[0,0x306EFF,0x347C17,0x000000,0xF665AB,0x4F4F4F,0xADFF2F]
$Form1 = GUICreate("Mô Phỏng Điều Phối Tiến Trình- FIFO- Chienthan1612", 818, 700, 192, 0)
$Group2 = GUICtrlCreateGroup("Nhập tiến trình",60,230,590,70)
$Button1 = GUICtrlCreateButton("Thêm", 512, 272, 57, 25)
$Button2 = GUICtrlCreateButton("Bắt đầu", 575, 272, 60, 25)
$Label1 = GUICtrlCreateLabel("Tên tiến trình", 80, 248, 67, 17)
GUICtrlSetFont(-1, 8, 400, 0, "Tahoma")
$Input1 = GUICtrlCreateInput("A", 72, 264, 89, 21)
$Label2 = GUICtrlCreateLabel("Tổng thời gian", 176, 248, 72, 17)
GUICtrlSetFont(-1, 8, 400, 0, "Tahoma")
$Input2 = GUICtrlCreateInput("10", 168, 264, 97, 21)
$Label3 = GUICtrlCreateLabel("Thời điểm", 296, 248, 49, 17)
GUICtrlSetFont(-1, 8, 400, 0, "Tahoma")
$Input3 = GUICtrlCreateInput("3", 272, 264, 105, 21)
$Label4 = GUICtrlCreateLabel("Thời gian I/O", 400, 248, 66, 17)
GUICtrlSetFont(-1, 8, 400, 0, "tahoma")
$Input4 = GUICtrlCreateInput("5", 384, 264, 97, 21)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Label5 = GUICtrlCreateLabel("Mô Phỏng Điều Phối Tiến Trình Theo Thuật Toán FIFO", 112, 16, 617, 27)
GUICtrlSetFont(-1, 14, 800, 0, "Tahoma")
$Label6 = GUICtrlCreateLabel("+  Danh sách Tiến Trình", 64, 72, 150, 17)
$Label7 = GUICtrlCreateLabel("+-----------------+------------------------+-----------------------+-------------------+" & @CR &"|"& Center_txt("Tên Tiến Trình",17) &"|  Tồng thời gian        |  Thời Điểm              |  Thời Gian I/O  |"& @CR &"+-----------------+------------------------+-----------------------+-------------------+", 64, 96, 580, 129)
GUICtrlSetFont(-1, 8, 400, 0, "Tahoma")
$Label8 = GUICtrlCreateLabel("Ready List", 110, 312, 70, 17)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
$Label9 = GUICtrlCreateEdit(Center_txt("Time",7) & "|" & Center_txt("Mô phỏng",60), 64, 336, 220, 180)
$Label10 = GUICtrlCreateLabel("Waiting List", 524, 312, 70, 17)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
$Label11 = GUICtrlCreateEdit(Center_txt("Time",7) & "|" & Center_txt("Mô phỏng",60), 488, 328, 220, 180)
$Label12 = GUICtrlCreateLabel("Code by ChienThan1612 - Phạm Ngọc Quang Nam - K36.104.043", 16, 670, 473, 23)
GUICtrlSetFont(-1, 12, 400, 0, "Tahoma")
$Label13 = GUICtrlCreateLabel("Đồ thị mô phỏng:", 32, 520, 100, 17)
$Label14 = GUICtrlCreateLabel("I/O:", 32, 550, 23, 17)
$Label15 = GUICtrlCreateLabel("CPU:", 30, 580, 29, 17)
$Label16 = GUICtrlCreateLabel("Chú thích:", 30, 620, 100, 17)
$dothi=GUICtrlCreateGraphic(40,550,100,50)
;Line I/O y=8;x=20
GUICtrlSetGraphic(-1, $GUI_GR_MOVE, 20,8)
GUICtrlSetGraphic(-1, $GUI_GR_COLOR, 0x657383)
GUICtrlSetGraphic(-1, $GUI_GR_LINE, 700,8)
;Line CPU y=38;x=20
GUICtrlSetGraphic(-1, $GUI_GR_MOVE, 20,38)
GUICtrlSetGraphic(-1, $GUI_GR_COLOR, 0xff)
GUICtrlSetGraphic(-1, $GUI_GR_LINE, 700,38)
;Time I/O
GUICtrlCreateLabel("0",58,558,10,10)
GUICtrlSetColor(-1, 0xff)
GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
GUICtrlSetFont(-1, 7, 800, 0, "Tahoma")
;Time CPU
GUICtrlCreateLabel("0",58,578,10,10)
GUICtrlSetColor(-1, 0xff)
GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
GUICtrlSetFont(-1, 7, 800, 0, "Tahoma")
GUISetState(@SW_SHOW)
;Kết thúc code giao diện

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
Về Đầu Trang Go down
http://chienthan1612.com

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

Xem chủ đề cũ hơn Xem chủ đề mới hơn Về Đầu Trang 
Trang 1 trong tổng số 1 trang

Permissions in this forum:Bạn không có quyền trả lời bài viết
Diễn đàn ITK36 - ĐH Sư phạm TPHCM :: Góc học tập :: Tài liệu học tập-
Chuyển đến