Tab1.cpp
资源名称:OSDemo [点击查看]
上传用户:tianjwyx
上传日期:2007-01-13
资源大小:813k
文件大小:14k
源码类别:
操作系统开发
开发平台:
Visual C++
- // Tab1.cpp : implementation file
- //
- #include "stdafx.h"
- #include "OSDemo.h"
- #include "Tab1.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CTab1 dialog
- CTab1::CTab1(CWnd* pParent /*=NULL*/)
- : CDialog(CTab1::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CTab1)
- m_NeedMem = _T("");
- m_NeedTime = _T("");
- m_ReqMem = 0;
- m_ReqTime = 0;
- m_StartMsg = _T("");
- //}}AFX_DATA_INIT
- }
- void CTab1::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CTab1)
- DDX_Control(pDX, IDC_TIME_SLIDER, m_TimeSlider);
- DDX_Control(pDX, IDC_TAB_VIEW, m_TabView);
- DDX_Control(pDX, IDC_COMBO_TAPE, m_Combo2);
- DDX_Control(pDX, IDC_COMBO_PRINTER, m_Combo1);
- DDX_Control(pDX, IDC_MEMLIST, m_MemList);
- DDX_Control(pDX, IDC_JOBLIST, m_JobList);
- DDX_Text(pDX, IDC_NEED_MEM, m_ReqMem);
- DDV_MinMaxUInt(pDX, m_ReqMem, 5, 100);
- DDX_Text(pDX, IDC_NEED_TIME, m_ReqTime);
- DDV_MinMaxUInt(pDX, m_ReqTime, 5, 30);
- DDX_Text(pDX, IDC_START, m_StartMsg);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CTab1, CDialog)
- //{{AFX_MSG_MAP(CTab1)
- ON_NOTIFY(NM_RELEASEDCAPTURE, IDC_TIME_SLIDER, OnReleasedcaptureTimeSlider)
- ON_BN_CLICKED(IDC_TIME_ADD, OnTimeAdd)
- ON_BN_CLICKED(IDC_TIME_DEC, OnTimeDec)
- ON_BN_CLICKED(IDC_BTN_ADD, OnBtnAdd)
- ON_BN_CLICKED(IDC_BTN_RUN, OnBtnRun)
- ON_BN_CLICKED(IDC_BTN_DEL, OnBtnDel)
- ON_WM_CONTEXTMENU()
- ON_COMMAND(IDR_MNU_REDO, OnMnuRedo)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CTab1 message handlers
- BOOL CTab1::OnInitDialog()
- {
- CDialog::OnInitDialog();
- CurJobNum=0;
- DWORD dwStyle1 = m_JobList.GetExtendedStyle();
- ListView_SetExtendedListViewStyle(m_JobList.m_hWnd, LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
- DWORD dwStyle2 = m_MemList.GetExtendedStyle();
- ListView_SetExtendedListViewStyle(m_MemList.m_hWnd, LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
- m_JobList.InsertColumn(0,"作业号",LVCFMT_LEFT,50);
- m_JobList.InsertColumn(1,"输入时间",LVCFMT_CENTER,80);
- m_JobList.InsertColumn(2,"要求计算时间",LVCFMT_CENTER,80);
- m_JobList.InsertColumn(3,"占用内存",LVCFMT_CENTER,80);
- m_JobList.InsertColumn(4,"磁带机数量",LVCFMT_CENTER,80);
- m_JobList.InsertColumn(5,"打印机数量",LVCFMT_CENTER,80);
- m_MemList.InsertColumn(0,"时间",LVCFMT_LEFT,35);
- m_MemList.InsertColumn(1,"块大小",LVCFMT_LEFT,50);
- m_MemList.InsertColumn(2,"首址",LVCFMT_LEFT,50);
- m_TimeSlider.SetRange(0,60);
- m_TimeSlider.SetTicFreq(5);
- m_StartMsg="作业输入时刻: 8:00";
- ((CSpinButtonCtrl*)GetDlgItem(IDC_SPIN1))->SetRange(5,30);
- ((CSpinButtonCtrl*)GetDlgItem(IDC_SPIN2))->SetRange(5,100);
- m_Combo1.SetCurSel(0);
- m_Combo2.SetCurSel(0);
- m_ReqMem=5;
- m_ReqTime=5;
- ItemCount=0;
- m_TabView.AddPage("作业运行图", &m_TabJob, IDD_TAB_JOB);
- m_TabView.AddPage("内存分配图", &m_TabMem, IDD_TAB_MEM);
- m_TabView.Show();
- FreeAllMem();
- UpdateData(false);
- return TRUE; // return TRUE unless you set the focus to a control
- // EXCEPTION: OCX Property Pages should return FALSE
- }
- void CTab1::OnReleasedcaptureTimeSlider(NMHDR* pNMHDR, LRESULT* pResult)
- {
- ShowStartTime();
- *pResult = 0;
- }
- void CTab1::OnTimeAdd()
- {
- int nPos=m_TimeSlider.GetPos();
- m_TimeSlider.SetPos(nPos+5);
- ShowStartTime();
- }
- void CTab1::ShowStartTime()
- {
- int nPos=m_TimeSlider.GetPos();
- if (nPos<10)
- m_StartMsg.Format("作业输入时刻:8:0%d",nPos);
- else
- m_StartMsg.Format("作业输入时刻:8:%d",nPos);
- if (nPos==60) m_StartMsg="作业输入时刻:9:00";
- UpdateData(false);
- }
- void CTab1::OnTimeDec()
- {
- // TODO: Add your control notification handler code here
- int nPos=m_TimeSlider.GetPos();
- m_TimeSlider.SetPos(nPos-5);
- ShowStartTime();
- }
- void CTab1::OnBtnAdd()
- {
- UpdateData(true);
- if (JCBList.GetSize()>=5)
- {
- MessageBox("为了便于演示,本程序最多只能支持5个作业!","提示:",MB_ICONINFORMATION);
- return;
- }
- JCB NewJob;
- //JCBList NewJob;
- CString PrintNum;
- CString TapeNum;
- NewJob.ID=JCBList.GetSize()+1;
- m_Combo1.GetWindowText(PrintNum);
- NewJob.PrintNum=atoi(PrintNum);
- m_Combo2.GetWindowText(TapeNum);
- NewJob.TapeNum=atoi(TapeNum);
- NewJob.TakeMem=m_ReqMem;
- NewJob.TakeTime=(float)m_ReqTime;
- NewJob.RunTime=m_TimeSlider.GetPos();
- JCBList.Add(NewJob);
- ShowJobList();
- }
- void CTab1::ShowJobList()
- {
- CString id;
- CString RunTime;
- CString TakeMem;
- CString TakeTime;
- CString PrintNum;
- CString TapeNum;
- m_JobList.DeleteAllItems();
- for(int i=0;i<JCBList.GetSize();i++)
- {
- id.Format("%d",i+1);
- RunTime.Format("%d",JCBList[i].RunTime );
- TakeMem.Format("%d",JCBList[i].TakeMem);
- TakeTime.Format("%d",(int)JCBList[i].TakeTime);
- PrintNum.Format("%d",JCBList[i].PrintNum);
- TapeNum.Format("%d",JCBList[i].TapeNum);
- AddJob(id,RunTime,TakeTime,TakeMem,PrintNum,TapeNum);
- }
- }
- void CTab1::AddJob(CString id, CString inputtime, CString usetime, CString mem,CString printer, CString tape)
- {
- LVITEM lvitem;
- int iActualItem;
- CString StartTime;
- for(int iSubItem=0;iSubItem<6;iSubItem++)
- {
- lvitem.mask = LVIF_TEXT|(iSubItem == 0? LVIF_IMAGE : 0);
- lvitem.iItem = (iSubItem == 0)? ++ItemCount : iActualItem;
- lvitem.iSubItem = iSubItem;
- switch(iSubItem)
- {
- case 0:
- lvitem.pszText =(LPTSTR)(LPCTSTR)id;
- break;
- case 1:
- if (atoi(inputtime)%60<10)
- StartTime.Format("%d:0%d",8+atoi(inputtime)/60,atoi(inputtime)%60);
- else
- StartTime.Format("%d:%d",8+atoi(inputtime)/60,atoi(inputtime)%60);
- lvitem.pszText = (LPTSTR)(LPCTSTR)StartTime;
- break;
- case 2:
- usetime+="min";
- lvitem.pszText = (LPTSTR)(LPCTSTR)usetime;
- break;
- case 3:
- mem+="KB";
- lvitem.pszText = (LPTSTR)(LPCTSTR)mem;
- break;
- case 4:
- tape+="台";
- lvitem.pszText = (LPTSTR)(LPCTSTR)tape;
- break;
- case 5:
- printer+="台";
- lvitem.pszText = (LPTSTR)(LPCTSTR)printer;
- break;
- }
- if (iSubItem == 0)
- iActualItem = m_JobList.InsertItem(&lvitem);
- else
- m_JobList.SetItem(&lvitem);
- }
- }
- void CTab1::OnBtnRun()
- {
- if(!JCBList.GetSize())
- {
- MessageBox("尚未加入任何要运行的作业!","错误:",MB_ICONEXCLAMATION);
- return;
- }
- CountTime=0;
- int i;
- m_TabMem.MyRect.RemoveAll();
- m_TabJob.MyRect.RemoveAll();
- JCBWait.RemoveAll();
- JCBRun.RemoveAll();
- while((JCBList.GetSize())>0 || (JCBWait.GetSize())>0 || (JCBRun.GetSize())>0)
- {
- i=0;
- while (i<JCBRun.GetSize())
- {
- JCBRun[i].TakeTime-=1/(float)JCBRun.GetSize();
- if (JCBRun[i].TakeTime<=0)
- {
- CString crText;
- crText.Format("(%d)",JCBRun[i].ID);
- if (JCBRun[i].RunTime>JCBRun[i].SetTime)
- m_TabJob.DrawDotRect(60+JCBRun[i].SetTime*5,25*JCBRun[i].ID,(JCBRun[i].RunTime-JCBRun[i].SetTime)*5,JobColor[JCBRun[i].ID-1]);
- m_TabJob.DrawRect(60+JCBRun[i].RunTime*5,25*JCBRun[i].ID,(CountTime-JCBRun[i].RunTime)*5,JobColor[JCBRun[i].ID-1],"");
- 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);
- m_TabMem.AddPiece(JobColor[JCBRun[i].ID-1],(float)JCBRun[i].MemSize/100*360,crText);
- m_TabJob.SumTime+=CountTime-JCBRun[i].RunTime;
- m_TabJob.AveTime++;
- if (JCBRun[i].PrintNum>0 || JCBRun[i].TapeNum>0)
- {
- for (int j=0;j<3;j++)
- {
- if (RCList[j].User==(int)JCBRun[i].ID)
- {
- crText.Format("%d",RCList[j].User);
- m_TabJob.DrawRect(60+JCBRun[i].RunTime*5,RCList[j].yPos,(CountTime-JCBRun[i].RunTime)*5,RCList[j].tagColor,crText);
- RCList[j].User=-1;
- }
- }
- }
- free_block(JCBRun[i].TakeMem,JCBRun[i].MemSize);
- nTaper+=JCBRun[i].TapeNum;
- nPrinter+=JCBRun[i].PrintNum;
- ShowMemList();
- JCBRun.RemoveAt(i);
- i--;
- }
- i++;
- }
- i=0;
- while (i<JCBList.GetSize())
- {
- if (JCBList[i].RunTime==CountTime)
- {
- JCBWait.Add(JCBList[i]);
- JCBList.RemoveAt(i);
- i--;
- }
- i++;
- }
- i=0;
- while (i<JCBWait.GetSize())
- {
- if (IsMemEnough(JCBWait[i].TakeMem) && JCBWait[i].PrintNum<=nPrinter && JCBWait[i].TapeNum<=nTaper)
- {
- if (JCBWait[i].PrintNum==1)
- Apply_HardWare(PRINTER,JCBWait[i].ID);
- for (UINT x=0;x<JCBWait[i].TapeNum;x++)
- Apply_HardWare(TAPER,JCBWait[i].ID);
- nPrinter-=JCBWait[i].PrintNum;
- nTaper-=JCBWait[i].TapeNum;
- JCBWait[i].MemSize=JCBWait[i].TakeMem;
- //Sleep(1000);
- JCBWait[i].TakeMem=get_block(JCBWait[i].MemSize);
- JCBWait[i].SetTime=JCBWait[i].RunTime;
- JCBWait[i].RunTime=CountTime;
- JCBRun.Add(JCBWait[i]);
- JCBWait.RemoveAt(i);
- i--;
- }
- i++;
- }
- CountTime++;
- }
- m_TabJob.bPaint=true;
- m_TabMem.bPaint=true;
- m_TabJob.AveTime=(float)m_TabJob.SumTime/m_TabJob.AveTime;
- m_TabJob.CalcRunTime();
- }
- void CTab1::OnBtnDel()
- {
- if(m_JobList.GetFirstSelectedItemPosition())
- {
- JCBList.RemoveAt((int)(m_JobList.GetFirstSelectedItemPosition()-1));
- ShowJobList();
- }
- else
- {
- MessageBox("请先选中要删除的作业","提示:",MB_ICONINFORMATION);
- }
- }
- void CTab1::ShowMemList()
- {
- CString strSize;
- CString strAddr;
- //m_MemList.DeleteAllItems();
- for (int i=0;i<FBTList.GetSize();i++)
- {
- strSize.Format("%3d",FBTList[i].size);
- strAddr.Format("%3d",FBTList[i].addr-1);
- CString CurTime;
- CurTime.Format("%d",CountTime);
- AddMemItem(CurTime,strSize,strAddr);
- }
- }
- void CTab1::AddMemItem(CString time,CString size, CString addr)
- {
- LVITEM lvitem;
- int iActualItem;
- for(int iSubItem=0;iSubItem<6;iSubItem++)
- {
- lvitem.mask = LVIF_TEXT|(iSubItem == 0? LVIF_IMAGE : 0);
- lvitem.iItem = (iSubItem == 0)? ++MemCount : iActualItem;
- lvitem.iSubItem = iSubItem;
- CString CurTime;
- switch(iSubItem)
- {
- case 0:
- if (atoi(time)%60<10)
- CurTime.Format("%d:0%d",8+atoi(time)/60,atoi(time)%60);
- else
- CurTime.Format("%d:%d",8+atoi(time)/60,atoi(time)%60);
- lvitem.pszText =(LPTSTR)(LPCTSTR)CurTime;
- break;
- case 1:
- size+="KB";
- lvitem.pszText =(LPTSTR)(LPCTSTR)size;
- break;
- case 2:
- addr+="KB";
- lvitem.pszText =(LPTSTR)(LPCTSTR)addr;
- break;
- }
- if (iSubItem == 0)
- iActualItem = m_MemList.InsertItem(&lvitem);
- else
- m_MemList.SetItem(&lvitem);
- }
- }
- void CTab1::FreeAllMem()
- {
- FBTList.RemoveAll();
- m_MemList.DeleteAllItems();
- m_TabJob.AveTime=0;
- m_TabJob.SumTime=0;
- m_TabJob.bPaint=false;
- m_TabJob.MyRect.RemoveAll();
- m_TabMem.bPaint=false;
- m_TabMem.MyRect.RemoveAll();
- m_JobList.DeleteAllItems();
- m_TabMem.m_wndChart.Reset();
- FBT MemItem;
- MemItem.addr=1;
- MemItem.size=100;
- FBTList.Add(MemItem);
- ShowMemList();
- nTaper=2;
- nPrinter=1;
- RCList[0].Type=TAPER;
- RCList[0].yPos=150;
- RCList[0].User=-1;
- RCList[0].tagColor=RGB(206,48,0);
- RCList[1].Type=TAPER;
- RCList[1].yPos=175;
- RCList[1].tagColor=RGB(132,0,0);
- RCList[1].User=-1;
- RCList[2].Type=PRINTER;
- RCList[2].yPos=198;
- RCList[2].tagColor=RGB(0,130,132);
- RCList[2].User=-1;
- JobColor[0]=RGB(200,0,0);
- JobColor[1]=RGB(200,0,200);
- JobColor[2]=RGB(100,0,200);
- JobColor[3]=RGB(240,220,70);
- JobColor[4]=RGB(100,135,100);
- JCBWait.RemoveAll();
- JCBList.RemoveAll();
- JCBRun.RemoveAll();
- }
- int CTab1::get_block(UINT NeedSize)
- {
- int i=0;
- int p;
- while (FBTList[i].size>0 && FBTList[i].size<NeedSize && i<FBTList.GetSize())
- i++;
- if(i>=FBTList.GetSize()) return 0;
- p=FBTList[i].addr+FBTList[i].size-NeedSize;
- FBTList[i].size-=NeedSize;
- //MessageBox("get_block1");
- ShowMemList();
- return p; //返回分得的内存块首址
- }
- bool CTab1::free_block(int addr,int size) //回收已分配的内存并修改自由区表
- {
- bool flag=false;
- int i=0;
- int nIndex;
- while (i<FBTList.GetSize()) //第一遍循环判断回收的块是否有下接区,有就合并,并向下执行。
- {
- if ((int)(FBTList[i].addr+FBTList[i].size)==addr)
- {
- if ((FBTList[i].addr+FBTList[i].size+size)>TOTAL_MEM+1)
- return false;
- else
- {
- FBTList[i].size+=size;
- addr=FBTList[i].addr; //修改回收首址为后并后的首址以防块上下都有接区。
- size=FBTList[i].size;
- flag=true;
- nIndex=i;
- }
- }
- i++;
- }
- i=0;
- while (i<FBTList.GetSize()) //第二遍循环判断回收的块是否有上接区,有合并后退出
- {
- if (addr+size==(int)FBTList[i].addr)
- {
- FBTList[i].size+=size;
- FBTList[i].addr=addr;
- if (flag) FBTList.RemoveAt(nIndex);
- return true;
- }
- i++;
- }
- if (flag) return true; //如果只有上接区到这里退出
- //下面是即没有上接区,也没有下接区的回收处理
- if ((addr+size)>TOTAL_MEM+1) return false; //要回收的首址加长度超过内存总长,错误返回。
- FBT MemBlock;
- MemBlock.addr=addr;
- MemBlock.size=size;
- FBTList.Add(MemBlock); //在自由区表添加新块
- return true;
- }
- bool CTab1::IsMemEnough(int size) //判断内存自由区是否足够
- {
- for (int i=0;i<FBTList.GetSize();i++)
- {
- if (FBTList[i].size>=(UINT)size)
- return true;
- }
- return false;
- }
- bool CTab1::Apply_HardWare(UINT type,UINT user) //申请打印机或磁带机资源
- {
- for(int i=0;i<3;i++)
- {
- if ((RCList[i].Type==type) && (RCList[i].User==-1))
- {
- RCList[i].User=user;
- return true;
- }
- }
- return false;
- }
- void CTab1::OnContextMenu(CWnd* pWnd, CPoint point)
- {
- if (point.x == -1 && point.y == -1){
- //keystroke invocation
- CRect rect;
- GetClientRect(rect);
- ClientToScreen(rect);
- point = rect.TopLeft();
- point.Offset(5, 5);
- }
- CMenu menu;
- VERIFY(menu.LoadMenu(IDR_POP_MNU1));
- CMenu* pPopup = menu.GetSubMenu(0);
- ASSERT(pPopup != NULL);
- CWnd* pWndPopupOwner = this;
- //while (pWndPopupOwner->GetStyle() & WS_CHILD)
- // pWndPopupOwner = pWndPopupOwner->GetParent();
- TrackSkinPopupMenu( pPopup->m_hMenu, TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y,
- pWndPopupOwner->m_hWnd );
- }
- void CTab1::OnMnuRedo()
- {
- FreeAllMem();
- if (m_TabView.GetCurSel()==0)
- {
- m_TabJob.ShowWindow(SW_HIDE);
- m_TabJob.ShowWindow(SW_SHOW);
- }
- else
- {
- m_TabMem.ShowWindow(SW_HIDE);
- m_TabMem.ShowWindow(SW_SHOW);
- }
- //
- //
- }