⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 随机.txt

📁 CPU调度源码分类介绍。我做课程设计是用到的。拿出分享。
💻 TXT
📖 第 1 页 / 共 2 页
字号:
// CPU Schedule1Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "CPU Schedule1.h"
#include "CPU Schedule1Dlg.h"
#include "Process.h"
#include "time.h"
#include "stdlib.h"

static int I=0; //记录进程的序号及个数
static CProcess process[50];//用于存放进程的
static int StringCount=1;//用于记录添加的行数
static int m_AddProcess=0;//用于标志是否加入了进程,0表无,非0表示已加入 
static float StaAverageWaitTime[4];//用于综合评价算法
static float StaAverageRoundTime[4];//用于综合评价算法
static int WT=0;
static int RT=0;

#ifdef _DEBUG
#define new DEBUG_NEW
#define Littletime 2 //设置时间片为2
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCPUSchedule1Dlg dialog

CCPUSchedule1Dlg::CCPUSchedule1Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCPUSchedule1Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCPUSchedule1Dlg)
	m_pname = _T("");
	m_servicetime =0;
	m_priority =0;
	m_littletime =0 ;
	m_arriveorder = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CCPUSchedule1Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCPUSchedule1Dlg)
	DDX_Control(pDX, IDC_LIST3, m_request);
	DDX_Control(pDX, IDC_LIST2, m_cresultshow);
	DDX_Control(pDX, IDC_LIST1, m_showprocess);
	DDX_Control(pDX, IDC_EDIT5, m_clittletime);
	DDX_Control(pDX, IDC_EDIT4, m_cpriority);
	DDX_Control(pDX, IDC_EDIT3, m_cservicetime);
	DDX_Control(pDX, IDC_EDIT2, m_carriveorder);
	DDX_Control(pDX, IDC_EDIT1, m_cpname);
	DDX_Text(pDX, IDC_EDIT1, m_pname);
	DDX_Text(pDX, IDC_EDIT3, m_servicetime);
	DDV_MinMaxInt(pDX, m_servicetime, 1, 100);
	DDX_Text(pDX, IDC_EDIT4, m_priority);
	DDV_MinMaxInt(pDX, m_priority, 1, 100);
	DDX_Text(pDX, IDC_EDIT5, m_littletime);
	DDV_MinMaxInt(pDX, m_littletime, 1, 100);
	DDX_Text(pDX, IDC_EDIT2, m_arriveorder);
	DDV_MinMaxInt(pDX, m_arriveorder, 1, 100);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCPUSchedule1Dlg, CDialog)
	//{{AFX_MSG_MAP(CCPUSchedule1Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_AddProcess, OnAddProcess)
	ON_LBN_DBLCLK(IDC_LIST1, OnDblclkList1)
	ON_BN_CLICKED(IDC_BUTTON12, OnBtnRadom)
	ON_BN_CLICKED(IDC_BUTTON11, OnBtnClear)
	ON_BN_CLICKED(IDC_BUTTON10, OnBtnDelete)
	ON_BN_CLICKED(IDC_BUTTON1, OnBtnFCFS)
	ON_BN_CLICKED(IDC_BUTTON2, OnBtnPriority)
	ON_BN_CLICKED(IDC_BUTTON3, OnBtnMultQue)
	ON_BN_CLICKED(IDC_BUTTON8, OnBtnComEvaluate)
	ON_BN_CLICKED(IDC_BUTTON6, OnBtnSJF)
	ON_BN_CLICKED(IDC_BUTTON7, OnBtnRR)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCPUSchedule1Dlg message handlers

BOOL CCPUSchedule1Dlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CCPUSchedule1Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CCPUSchedule1Dlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CCPUSchedule1Dlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CCPUSchedule1Dlg::OnAddProcess() 
{
	// TODO: Add your control notification handler code here
	int IArriveOrder,IServiceTime,IPriority,ILittleTime;
	char CProcessName[20],CArriveOrder[10],CServiceTime[10],CPriority[10],CLittleTime[10];
	CString cstr_process;
	
	//通过控制变量取文本框内容
	if(UpdateData())
	{
		//UpdateData()为bool型变量,通过该函数限制输入数值的地方不能输入非数值类型的值
		m_cpname.GetWindowText(CProcessName,20);
	    m_carriveorder.GetWindowText(CArriveOrder,10);
	    m_cservicetime.GetWindowText(CServiceTime,10);
	    m_cpriority.GetWindowText(CPriority,10);
	    m_clittletime.GetWindowText(CLittleTime,10);
	
	    IArriveOrder=atoi(CArriveOrder);
        IServiceTime=atoi(CServiceTime);
        IPriority=atoi(CPriority);
	    ILittleTime=atoi(CLittleTime);
         
	    process[I].SetProcessName(CProcessName);
        process[I].SetArriveOrder(IArriveOrder); 
        process[I].SetServiceTime(IServiceTime);
        process[I].SetPriority(IPriority);
		process[I].SetLittleTime(ILittleTime);


		
		//将文本框中内容打包
		cstr_process.Format(" %2s %4d  %3d  %4d  %3d",CProcessName,
			IArriveOrder,IServiceTime,IPriority,ILittleTime);
		if(m_cpname.GetWindowText(CProcessName,20)==NULL)
		{
			m_AddProcess=0;
		}//因为到达次序,服务时间,优先级等数值数据由成员变量通过UpdateData()控制,
		//所以只需判断进程名不为空时可加入进程,否则不加入

		else
		{
			I++; //当有进程时加1
			m_AddProcess++;//m_AddProcess判断是否有进程,非0则有,否则没有进程
		}
	}//if 输入进程
	else
	{
		m_AddProcess=0;//没有输入进程
	}//else

	if(m_AddProcess==0)
	{
		MessageBox("警告! 您还没输入任何进程,请输入!");
	}//if
	else
	{
		m_showprocess.AddString(cstr_process);//将进程的内容显示在list中
		++StringCount;
	}//else
}




void CCPUSchedule1Dlg::OnDblclkList1() 
{
	// TODO: Add your control notification handler code here
	
}

void CCPUSchedule1Dlg::OnBtnRadom() 
{
	// TODO: Add your control notification handler code here
	int IArriveOrder,IServiceTime,IPriority;
	char CProcessName[20],CArriveOrder[10],CServiceTime[10],CPriority[10];
	CString cstr_pname;
    //,ILittleTime,CLittleTime[10]
	
	//随机命名
    cstr_pname.Format("P%d",I+1);
	m_cpname.SetWindowText(cstr_pname);      //随机的名字设置文本框内容
	m_cpname.GetWindowText(CProcessName,20);  //显示进程名称

	//随机生成其它数字
    srand((unsigned int)time(NULL));
	IArriveOrder=1+rand()%20;     //把到达次序设置在1到20之间
    IServiceTime=1+rand()%30;   //服务时间设置在1到30之间
	IPriority=1+rand()%20;   //优先级设置在1到20之间
	//ILittleTime=1+rand()%10;
			
	
     
	
	//itoa()函数是将一个整数值转换成一个字符串,
	//第一个参数是要转换的整数值,第二个参数是转换后存放的字符串,最后一个是转换的进制
	itoa(IArriveOrder,CArriveOrder,10); 
	itoa(IServiceTime,CServiceTime,10);
	itoa(IPriority,CPriority,10);
//	itoa(ILittleTime,CLittleTime,10);

	m_carriveorder.SetWindowText(CArriveOrder);
	m_cservicetime.SetWindowText(CServiceTime);
	m_cpriority.SetWindowText(CPriority);
	m_clittletime.SetWindowText("2");

		
	m_AddProcess++;//产生进程再添加进程时就不会出错
}


void CCPUSchedule1Dlg::OnBtnClear()
{
	// TODO: Add your control notification handler code here
	int num;
	I=0;    //当重新产生进程时,进程数又从1开始
	
	//删除文本框中的内容
	for(num=0;num<StringCount;num++)
	{
		m_showprocess.DeleteString(0);//删除第1行的内容,删除
		m_cresultshow.DeleteString(0);
		m_request.DeleteString(0);

	}
	
	m_cpname.SetWindowText("");
	m_carriveorder.SetWindowText("");
	m_cservicetime.SetWindowText("");
	m_cpriority.SetWindowText("");
	m_clittletime.SetWindowText("");
	
    m_AddProcess=0;//清空进程后变为0,当再添加进程时提示输入!
}



void CCPUSchedule1Dlg::OnBtnDelete() 
{
	// TODO: Add your control notification handler code here



	
}

void CCPUSchedule1Dlg::OnBtnFCFS() 
{
	// TODO: Add your control notification handler code here
	int p=I,m,n,q=2,c=1,IWaitTime=0,IRoundTime=0;
	float AverageWaitTime,AverageRoundTime;
	int WaitTime[50],RoundTime[50];   
	CString str_AverageWaitTime,str_AverageRoundTime;
	CProcess turn;  
    
	//当没有任何进程而单击该按钮时报错
	if(I<1)
		MessageBox("警告!没输入进程,请输入!");
	else
		{
	    //按照到达次序进行冒泡法排序
          for(m=0;m<p;m++)
		     for(n=0;n<p-m;n++)
			 {
			    if(process[n].GetArriveOrder()>process[n+1].GetArriveOrder())
				{
				   turn=process[n];
				   process[n]=process[n+1];
				   process[n+1]=turn; 
				}
			    else
				{
				}
			 }//for
			 /*用来验证修改后的数据的
			 str_AverageWaitTime.Format("%d 到达顺序%d,服务%d",1,process[1].GetArriveOrder(),process[1].GetServiceTime());
			 m_cresultshow.AddString(str_AverageWaitTime);
			 str_AverageWaitTime.Format("%d 到达顺序%d,服务%d",2,process[2].GetArriveOrder(),process[2].GetServiceTime());
		   	 m_cresultshow.AddString(str_AverageWaitTime);
			 */
					 
	//计算等待时间
	WaitTime[1]=0;
		while(q<=p)
		{
			WaitTime[q]=process[q-1].GetServiceTime()+WaitTime[q-1];
			//第q个进程的等待时间=第q-1个进程的等待时间+第q-1个进程的服务时间
			
			q++;
		}

		for(q=1;q<=p;q++)
			IWaitTime=IWaitTime+WaitTime[q];
		AverageWaitTime=(float)IWaitTime/p;
		
		//计算周转时间
		for(c=1;c<=p;c++)
		{
			RoundTime[c]=WaitTime[c]+process[c].GetServiceTime();
			IRoundTime+=RoundTime[c];
		}
		AverageRoundTime=(float)IRoundTime/p;

    	//输出
		
		m_cresultshow.AddString(str_AverageWaitTime);
		str_AverageWaitTime.Format("FCFS算法的平均等待时间是:%f",AverageWaitTime);
		m_cresultshow.AddString(str_AverageWaitTime);

		str_AverageRoundTime.Format("          平均周转时间为:%f",AverageRoundTime);
		m_cresultshow.AddString(str_AverageRoundTime);

		StringCount++;

		if(I<1)
		{}//无进程时不输出
		else
		{
			m_request.AddString("先来先服务(FCFS)调度算法是最简单的调度算法,该算法既可用于作业调度\n");
			m_request.AddString("也可用于进程调度。FCFS算法比较利于长作业(进程),而不利于短作业(进程)!\n");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -