Tab1.cpp
上传用户:tianjwyx
上传日期:2007-01-13
资源大小:813k
文件大小:14k
源码类别:

操作系统开发

开发平台:

Visual C++

  1. // Tab1.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "OSDemo.h"
  5. #include "Tab1.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CTab1 dialog
  13. CTab1::CTab1(CWnd* pParent /*=NULL*/)
  14. : CDialog(CTab1::IDD, pParent)
  15. {
  16. //{{AFX_DATA_INIT(CTab1)
  17. m_NeedMem = _T("");
  18. m_NeedTime = _T("");
  19. m_ReqMem = 0;
  20. m_ReqTime = 0;
  21. m_StartMsg = _T("");
  22. //}}AFX_DATA_INIT
  23. }
  24. void CTab1::DoDataExchange(CDataExchange* pDX)
  25. {
  26. CDialog::DoDataExchange(pDX);
  27. //{{AFX_DATA_MAP(CTab1)
  28. DDX_Control(pDX, IDC_TIME_SLIDER, m_TimeSlider);
  29. DDX_Control(pDX, IDC_TAB_VIEW, m_TabView);
  30. DDX_Control(pDX, IDC_COMBO_TAPE, m_Combo2);
  31. DDX_Control(pDX, IDC_COMBO_PRINTER, m_Combo1);
  32. DDX_Control(pDX, IDC_MEMLIST, m_MemList);
  33. DDX_Control(pDX, IDC_JOBLIST, m_JobList);
  34. DDX_Text(pDX, IDC_NEED_MEM, m_ReqMem);
  35. DDV_MinMaxUInt(pDX, m_ReqMem, 5, 100);
  36. DDX_Text(pDX, IDC_NEED_TIME, m_ReqTime);
  37. DDV_MinMaxUInt(pDX, m_ReqTime, 5, 30);
  38. DDX_Text(pDX, IDC_START, m_StartMsg);
  39. //}}AFX_DATA_MAP
  40. }
  41. BEGIN_MESSAGE_MAP(CTab1, CDialog)
  42. //{{AFX_MSG_MAP(CTab1)
  43. ON_NOTIFY(NM_RELEASEDCAPTURE, IDC_TIME_SLIDER, OnReleasedcaptureTimeSlider)
  44. ON_BN_CLICKED(IDC_TIME_ADD, OnTimeAdd)
  45. ON_BN_CLICKED(IDC_TIME_DEC, OnTimeDec)
  46. ON_BN_CLICKED(IDC_BTN_ADD, OnBtnAdd)
  47. ON_BN_CLICKED(IDC_BTN_RUN, OnBtnRun)
  48.     ON_BN_CLICKED(IDC_BTN_DEL, OnBtnDel)
  49. ON_WM_CONTEXTMENU()
  50. ON_COMMAND(IDR_MNU_REDO, OnMnuRedo)
  51. //}}AFX_MSG_MAP
  52. END_MESSAGE_MAP()
  53. /////////////////////////////////////////////////////////////////////////////
  54. // CTab1 message handlers
  55. BOOL CTab1::OnInitDialog() 
  56. {
  57. CDialog::OnInitDialog();
  58. CurJobNum=0;
  59.     DWORD dwStyle1 = m_JobList.GetExtendedStyle();
  60. ListView_SetExtendedListViewStyle(m_JobList.m_hWnd, LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
  61. DWORD dwStyle2 = m_MemList.GetExtendedStyle();
  62. ListView_SetExtendedListViewStyle(m_MemList.m_hWnd, LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
  63. m_JobList.InsertColumn(0,"作业号",LVCFMT_LEFT,50);
  64. m_JobList.InsertColumn(1,"输入时间",LVCFMT_CENTER,80);
  65. m_JobList.InsertColumn(2,"要求计算时间",LVCFMT_CENTER,80);
  66. m_JobList.InsertColumn(3,"占用内存",LVCFMT_CENTER,80);
  67. m_JobList.InsertColumn(4,"磁带机数量",LVCFMT_CENTER,80);
  68. m_JobList.InsertColumn(5,"打印机数量",LVCFMT_CENTER,80);
  69. m_MemList.InsertColumn(0,"时间",LVCFMT_LEFT,35);
  70. m_MemList.InsertColumn(1,"块大小",LVCFMT_LEFT,50);
  71. m_MemList.InsertColumn(2,"首址",LVCFMT_LEFT,50);
  72. m_TimeSlider.SetRange(0,60);
  73. m_TimeSlider.SetTicFreq(5);
  74. m_StartMsg="作业输入时刻: 8:00";
  75. ((CSpinButtonCtrl*)GetDlgItem(IDC_SPIN1))->SetRange(5,30);
  76. ((CSpinButtonCtrl*)GetDlgItem(IDC_SPIN2))->SetRange(5,100);
  77. m_Combo1.SetCurSel(0);
  78. m_Combo2.SetCurSel(0);
  79.     m_ReqMem=5;
  80. m_ReqTime=5;
  81. ItemCount=0;
  82. m_TabView.AddPage("作业运行图", &m_TabJob, IDD_TAB_JOB);
  83. m_TabView.AddPage("内存分配图", &m_TabMem, IDD_TAB_MEM);
  84. m_TabView.Show();
  85. FreeAllMem();
  86. UpdateData(false);
  87. return TRUE;  // return TRUE unless you set the focus to a control
  88.               // EXCEPTION: OCX Property Pages should return FALSE
  89. }
  90. void CTab1::OnReleasedcaptureTimeSlider(NMHDR* pNMHDR, LRESULT* pResult) 
  91. {
  92.     ShowStartTime();
  93. *pResult = 0;
  94. }
  95. void CTab1::OnTimeAdd() 
  96. {
  97. int nPos=m_TimeSlider.GetPos();
  98.     m_TimeSlider.SetPos(nPos+5);
  99.     ShowStartTime();
  100. }
  101. void CTab1::ShowStartTime()
  102. {
  103.     int nPos=m_TimeSlider.GetPos();
  104. if (nPos<10)
  105.      m_StartMsg.Format("作业输入时刻:8:0%d",nPos);
  106.     else
  107.          m_StartMsg.Format("作业输入时刻:8:%d",nPos);
  108. if (nPos==60) m_StartMsg="作业输入时刻:9:00";
  109. UpdateData(false);
  110. }
  111. void CTab1::OnTimeDec() 
  112. {
  113. // TODO: Add your control notification handler code here
  114. int nPos=m_TimeSlider.GetPos();
  115.     m_TimeSlider.SetPos(nPos-5);
  116.     ShowStartTime();
  117. }
  118. void CTab1::OnBtnAdd() 
  119. {
  120.     UpdateData(true);
  121. if (JCBList.GetSize()>=5)
  122. {
  123. MessageBox("为了便于演示,本程序最多只能支持5个作业!","提示:",MB_ICONINFORMATION);
  124. return;
  125. }
  126. JCB NewJob;
  127. //JCBList NewJob;
  128.     CString PrintNum;
  129. CString TapeNum;
  130.     NewJob.ID=JCBList.GetSize()+1;
  131. m_Combo1.GetWindowText(PrintNum);
  132. NewJob.PrintNum=atoi(PrintNum);
  133. m_Combo2.GetWindowText(TapeNum);
  134. NewJob.TapeNum=atoi(TapeNum);
  135. NewJob.TakeMem=m_ReqMem;
  136. NewJob.TakeTime=(float)m_ReqTime;
  137. NewJob.RunTime=m_TimeSlider.GetPos();
  138. JCBList.Add(NewJob);
  139. ShowJobList();
  140. }
  141. void CTab1::ShowJobList()
  142. {
  143.     CString id;
  144. CString RunTime;
  145. CString TakeMem;
  146. CString TakeTime;
  147. CString PrintNum;
  148. CString TapeNum;
  149. m_JobList.DeleteAllItems(); 
  150. for(int i=0;i<JCBList.GetSize();i++)
  151. {
  152.        id.Format("%d",i+1);
  153.    RunTime.Format("%d",JCBList[i].RunTime );
  154.    TakeMem.Format("%d",JCBList[i].TakeMem);
  155.    TakeTime.Format("%d",(int)JCBList[i].TakeTime);
  156.    PrintNum.Format("%d",JCBList[i].PrintNum);
  157.        TapeNum.Format("%d",JCBList[i].TapeNum);
  158.    AddJob(id,RunTime,TakeTime,TakeMem,PrintNum,TapeNum);
  159. }
  160. }
  161. void CTab1::AddJob(CString id, CString inputtime, CString usetime, CString mem,CString printer, CString tape)
  162. {
  163.     LVITEM lvitem;
  164. int iActualItem;
  165. CString StartTime;
  166. for(int iSubItem=0;iSubItem<6;iSubItem++)
  167. {
  168. lvitem.mask = LVIF_TEXT|(iSubItem == 0? LVIF_IMAGE : 0);
  169. lvitem.iItem = (iSubItem == 0)? ++ItemCount : iActualItem;
  170. lvitem.iSubItem = iSubItem;
  171. switch(iSubItem)
  172. {
  173. case 0:
  174. lvitem.pszText =(LPTSTR)(LPCTSTR)id;
  175. break;
  176. case 1:
  177. if (atoi(inputtime)%60<10)
  178.     StartTime.Format("%d:0%d",8+atoi(inputtime)/60,atoi(inputtime)%60);
  179.         else
  180. StartTime.Format("%d:%d",8+atoi(inputtime)/60,atoi(inputtime)%60);
  181. lvitem.pszText = (LPTSTR)(LPCTSTR)StartTime;
  182.       break;
  183.         case 2:
  184. usetime+="min";
  185. lvitem.pszText = (LPTSTR)(LPCTSTR)usetime;
  186. break;
  187. case 3:
  188. mem+="KB";
  189. lvitem.pszText = (LPTSTR)(LPCTSTR)mem;
  190. break;
  191. case 4:
  192. tape+="台";
  193. lvitem.pszText = (LPTSTR)(LPCTSTR)tape;
  194. break;
  195. case 5:
  196. printer+="台";
  197. lvitem.pszText = (LPTSTR)(LPCTSTR)printer;
  198. break;
  199. if (iSubItem == 0)
  200. iActualItem = m_JobList.InsertItem(&lvitem);
  201. else
  202. m_JobList.SetItem(&lvitem);
  203. }
  204. }
  205. void CTab1::OnBtnRun() 
  206. {
  207.     if(!JCBList.GetSize())
  208. {
  209. MessageBox("尚未加入任何要运行的作业!","错误:",MB_ICONEXCLAMATION);
  210. return;
  211. }
  212. CountTime=0;
  213. int i;
  214.     m_TabMem.MyRect.RemoveAll();
  215. m_TabJob.MyRect.RemoveAll();
  216. JCBWait.RemoveAll();
  217. JCBRun.RemoveAll();
  218. while((JCBList.GetSize())>0 || (JCBWait.GetSize())>0 || (JCBRun.GetSize())>0)
  219. {
  220. i=0;
  221. while (i<JCBRun.GetSize())
  222. {
  223. JCBRun[i].TakeTime-=1/(float)JCBRun.GetSize();
  224. if (JCBRun[i].TakeTime<=0)
  225. {
  226.     CString crText;
  227. crText.Format("(%d)",JCBRun[i].ID);
  228. if (JCBRun[i].RunTime>JCBRun[i].SetTime)
  229.  m_TabJob.DrawDotRect(60+JCBRun[i].SetTime*5,25*JCBRun[i].ID,(JCBRun[i].RunTime-JCBRun[i].SetTime)*5,JobColor[JCBRun[i].ID-1]);
  230. m_TabJob.DrawRect(60+JCBRun[i].RunTime*5,25*JCBRun[i].ID,(CountTime-JCBRun[i].RunTime)*5,JobColor[JCBRun[i].ID-1],"");
  231. m_TabMem.DrawRect(60+JCBRun[i].RunTime*5,25+1.8*(TOTAL_MEM-(JCBRun[i].TakeMem+JCBRun[i].MemSize-1)),(CountTime-JCBRun[i].RunTime)*5,JCBRun[i].MemSize*1.8,JobColor[JCBRun[i].ID-1],crText);
  232. m_TabMem.AddPiece(JobColor[JCBRun[i].ID-1],(float)JCBRun[i].MemSize/100*360,crText);
  233.                 m_TabJob.SumTime+=CountTime-JCBRun[i].RunTime;
  234. m_TabJob.AveTime++;
  235. if (JCBRun[i].PrintNum>0 || JCBRun[i].TapeNum>0)
  236. {
  237. for (int j=0;j<3;j++)
  238. {
  239. if (RCList[j].User==(int)JCBRun[i].ID)
  240. {
  241.                             
  242. crText.Format("%d",RCList[j].User);
  243. m_TabJob.DrawRect(60+JCBRun[i].RunTime*5,RCList[j].yPos,(CountTime-JCBRun[i].RunTime)*5,RCList[j].tagColor,crText);
  244. RCList[j].User=-1;
  245. }
  246. }
  247. }
  248. free_block(JCBRun[i].TakeMem,JCBRun[i].MemSize);
  249. nTaper+=JCBRun[i].TapeNum;
  250. nPrinter+=JCBRun[i].PrintNum;
  251. ShowMemList();
  252. JCBRun.RemoveAt(i);
  253. i--;
  254. }
  255. i++;
  256. }
  257.     i=0;
  258. while (i<JCBList.GetSize())
  259. {
  260. if (JCBList[i].RunTime==CountTime)
  261.             {
  262. JCBWait.Add(JCBList[i]);
  263. JCBList.RemoveAt(i);
  264. i--;
  265. }
  266. i++;
  267. }
  268. i=0;
  269. while (i<JCBWait.GetSize())
  270. {
  271. if (IsMemEnough(JCBWait[i].TakeMem) && JCBWait[i].PrintNum<=nPrinter && JCBWait[i].TapeNum<=nTaper)
  272.             {
  273. if (JCBWait[i].PrintNum==1)
  274. Apply_HardWare(PRINTER,JCBWait[i].ID);
  275. for (UINT x=0;x<JCBWait[i].TapeNum;x++)
  276.                     Apply_HardWare(TAPER,JCBWait[i].ID);
  277. nPrinter-=JCBWait[i].PrintNum;
  278. nTaper-=JCBWait[i].TapeNum;
  279. JCBWait[i].MemSize=JCBWait[i].TakeMem;
  280. //Sleep(1000);
  281. JCBWait[i].TakeMem=get_block(JCBWait[i].MemSize);
  282.                 JCBWait[i].SetTime=JCBWait[i].RunTime;
  283. JCBWait[i].RunTime=CountTime;
  284. JCBRun.Add(JCBWait[i]);
  285. JCBWait.RemoveAt(i);
  286. i--;
  287. }
  288. i++;
  289. }
  290.      CountTime++;
  291. }
  292. m_TabJob.bPaint=true;
  293. m_TabMem.bPaint=true;
  294. m_TabJob.AveTime=(float)m_TabJob.SumTime/m_TabJob.AveTime;
  295. m_TabJob.CalcRunTime();
  296. }
  297. void CTab1::OnBtnDel() 
  298. {
  299. if(m_JobList.GetFirstSelectedItemPosition())
  300. {
  301. JCBList.RemoveAt((int)(m_JobList.GetFirstSelectedItemPosition()-1));
  302. ShowJobList();
  303. }
  304. else
  305. {
  306. MessageBox("请先选中要删除的作业","提示:",MB_ICONINFORMATION);
  307. }
  308. }
  309. void CTab1::ShowMemList()
  310. {
  311.     CString strSize;
  312. CString strAddr;
  313. //m_MemList.DeleteAllItems();
  314. for (int i=0;i<FBTList.GetSize();i++)
  315. {
  316. strSize.Format("%3d",FBTList[i].size);
  317. strAddr.Format("%3d",FBTList[i].addr-1);
  318. CString CurTime;
  319. CurTime.Format("%d",CountTime);
  320. AddMemItem(CurTime,strSize,strAddr);
  321. }
  322. }
  323. void CTab1::AddMemItem(CString time,CString size, CString addr)
  324. {
  325.     LVITEM lvitem;
  326. int iActualItem;
  327. for(int iSubItem=0;iSubItem<6;iSubItem++)
  328. {
  329. lvitem.mask = LVIF_TEXT|(iSubItem == 0? LVIF_IMAGE : 0);
  330. lvitem.iItem = (iSubItem == 0)? ++MemCount : iActualItem;
  331. lvitem.iSubItem = iSubItem;
  332. CString CurTime;
  333. switch(iSubItem)
  334. {
  335. case 0:
  336.             if (atoi(time)%60<10)
  337.     CurTime.Format("%d:0%d",8+atoi(time)/60,atoi(time)%60);
  338. else
  339. CurTime.Format("%d:%d",8+atoi(time)/60,atoi(time)%60);
  340. lvitem.pszText =(LPTSTR)(LPCTSTR)CurTime;
  341. break;
  342. case 1:
  343. size+="KB";
  344. lvitem.pszText =(LPTSTR)(LPCTSTR)size;
  345. break;
  346. case 2:
  347. addr+="KB";
  348.     lvitem.pszText =(LPTSTR)(LPCTSTR)addr;
  349. break;
  350. if (iSubItem == 0)
  351. iActualItem = m_MemList.InsertItem(&lvitem);
  352. else
  353. m_MemList.SetItem(&lvitem);
  354. }
  355. }
  356. void CTab1::FreeAllMem()
  357. {
  358.     FBTList.RemoveAll();
  359. m_MemList.DeleteAllItems();
  360. m_TabJob.AveTime=0;
  361. m_TabJob.SumTime=0;
  362. m_TabJob.bPaint=false;
  363. m_TabJob.MyRect.RemoveAll();
  364. m_TabMem.bPaint=false;
  365. m_TabMem.MyRect.RemoveAll();
  366. m_JobList.DeleteAllItems();
  367. m_TabMem.m_wndChart.Reset();
  368. FBT MemItem;
  369. MemItem.addr=1;
  370. MemItem.size=100;
  371.     FBTList.Add(MemItem);
  372. ShowMemList();
  373. nTaper=2;
  374. nPrinter=1;
  375. RCList[0].Type=TAPER;
  376. RCList[0].yPos=150;
  377.     RCList[0].User=-1;
  378. RCList[0].tagColor=RGB(206,48,0);
  379. RCList[1].Type=TAPER;
  380. RCList[1].yPos=175;
  381. RCList[1].tagColor=RGB(132,0,0);
  382. RCList[1].User=-1;
  383. RCList[2].Type=PRINTER;
  384. RCList[2].yPos=198;
  385. RCList[2].tagColor=RGB(0,130,132);
  386.     RCList[2].User=-1;
  387. JobColor[0]=RGB(200,0,0);
  388. JobColor[1]=RGB(200,0,200);
  389. JobColor[2]=RGB(100,0,200);
  390. JobColor[3]=RGB(240,220,70);
  391. JobColor[4]=RGB(100,135,100);
  392. JCBWait.RemoveAll();
  393. JCBList.RemoveAll();
  394. JCBRun.RemoveAll();
  395.    
  396. }
  397. int CTab1::get_block(UINT NeedSize)
  398. {
  399.     int i=0;
  400. int p;
  401.     while (FBTList[i].size>0 && FBTList[i].size<NeedSize && i<FBTList.GetSize())
  402.     i++;
  403. if(i>=FBTList.GetSize()) return 0;
  404. p=FBTList[i].addr+FBTList[i].size-NeedSize;
  405. FBTList[i].size-=NeedSize;
  406. //MessageBox("get_block1");
  407. ShowMemList();
  408. return p;  //返回分得的内存块首址
  409. }
  410. bool CTab1::free_block(int addr,int size)   //回收已分配的内存并修改自由区表
  411. {
  412.     bool flag=false;
  413. int i=0;
  414. int nIndex;
  415. while (i<FBTList.GetSize())     //第一遍循环判断回收的块是否有下接区,有就合并,并向下执行。
  416. {
  417. if ((int)(FBTList[i].addr+FBTList[i].size)==addr)
  418. {
  419. if ((FBTList[i].addr+FBTList[i].size+size)>TOTAL_MEM+1)
  420. return false;
  421. else
  422. {
  423. FBTList[i].size+=size;
  424.                 addr=FBTList[i].addr;   //修改回收首址为后并后的首址以防块上下都有接区。
  425. size=FBTList[i].size;
  426. flag=true;
  427. nIndex=i;
  428. }
  429. }
  430. i++;
  431. }
  432. i=0;
  433.     while (i<FBTList.GetSize())     //第二遍循环判断回收的块是否有上接区,有合并后退出
  434. {
  435. if (addr+size==(int)FBTList[i].addr)
  436. {
  437. FBTList[i].size+=size;
  438. FBTList[i].addr=addr;
  439. if (flag) FBTList.RemoveAt(nIndex);
  440. return true;
  441. }
  442. i++;
  443. }
  444. if (flag) return true;  //如果只有上接区到这里退出
  445. //下面是即没有上接区,也没有下接区的回收处理
  446. if ((addr+size)>TOTAL_MEM+1) return false;   //要回收的首址加长度超过内存总长,错误返回。
  447.     FBT MemBlock;
  448. MemBlock.addr=addr;
  449. MemBlock.size=size;
  450. FBTList.Add(MemBlock);   //在自由区表添加新块
  451. return true;
  452. }
  453. bool CTab1::IsMemEnough(int size)    //判断内存自由区是否足够
  454. {
  455.   for (int i=0;i<FBTList.GetSize();i++)
  456.   {
  457.   if (FBTList[i].size>=(UINT)size)
  458.   return true;
  459.   }
  460.   return false;
  461. }
  462. bool CTab1::Apply_HardWare(UINT type,UINT user)  //申请打印机或磁带机资源
  463. {
  464.     for(int i=0;i<3;i++)
  465. {
  466. if ((RCList[i].Type==type) && (RCList[i].User==-1))
  467. {
  468.            RCList[i].User=user;
  469.    return true;
  470. }
  471. }
  472.     return false;
  473. }
  474. void CTab1::OnContextMenu(CWnd* pWnd, CPoint point) 
  475. {
  476. if (point.x == -1 && point.y == -1){
  477. //keystroke invocation
  478. CRect rect;
  479. GetClientRect(rect);
  480. ClientToScreen(rect);
  481. point = rect.TopLeft();
  482. point.Offset(5, 5);
  483. }
  484. CMenu menu;
  485. VERIFY(menu.LoadMenu(IDR_POP_MNU1));
  486. CMenu* pPopup = menu.GetSubMenu(0);
  487. ASSERT(pPopup != NULL);
  488. CWnd* pWndPopupOwner = this;
  489. //while (pWndPopupOwner->GetStyle() & WS_CHILD)
  490. // pWndPopupOwner = pWndPopupOwner->GetParent();
  491. TrackSkinPopupMenu( pPopup->m_hMenu, TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y,
  492. pWndPopupOwner->m_hWnd );
  493. }
  494. void CTab1::OnMnuRedo() 
  495. {
  496.     FreeAllMem();
  497.     if (m_TabView.GetCurSel()==0)
  498. {
  499. m_TabJob.ShowWindow(SW_HIDE);
  500. m_TabJob.ShowWindow(SW_SHOW);
  501. }
  502. else
  503. {
  504. m_TabMem.ShowWindow(SW_HIDE);
  505. m_TabMem.ShowWindow(SW_SHOW);
  506. }
  507. // 
  508. //
  509. }