先进先出算法和时间片轮转算法

先进先出算法

// 先进先出Dlg.cpp : implementation file

//

#include "stdafx.h"

#include "先进先出.h"

#include "先进先出Dlg.h"

#include

#ifdef _DEBUG

#define new DEBUG_NEW

#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);

//}}AFX_VIRTUAL

// Implementation

protected:

//{{AFX_MSG(CAboutDlg)

//}}AFX_MSG

DECLARE_MESSAGE_MAP()

};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)

{ // DDX/DDV support

//{{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()

/////////////////////////////////////////////////////////////////////////////

// CMyDlg dialog

CMyDlg::CMyDlg(CWnd* pParent /*=NULL*/)

: CDialog(CMyDlg::IDD, pParent)

{

//{{AFX_DATA_INIT(CMyDlg)

m_1 = 0;

m_2 = 0;

m_3 = 0;

m_4 = 0;

m_5 = 0;

m_0 = _T("");

m_6 = 0;

//}}AFX_DATA_INIT

// Note that LoadIcon does not require a subsequent DestroyIcon in Win32

m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

}

void CMyDlg::DoDataExchange(CDataExchange* pDX)

{

CDialog::DoDataExchange(pDX);

//{{AFX_DATA_MAP(CMyDlg)

DDX_Text(pDX, IDC_EDIT1, m_1);

DDX_Text(pDX, IDC_EDIT2, m_2);

DDX_Text(pDX, IDC_EDIT3, m_3);

DDX_Text(pDX, IDC_EDIT4, m_4);

DDX_Text(pDX, IDC_EDIT5, m_5);

DDX_LBString(pDX, IDC_LIST1, m_0);

DDX_Text(pDX, IDC_EDIT6, m_6);

//}}AFX_DATA_MAP

}

BEGIN_MESSAGE_MAP(CMyDlg, CDialog)

//{{AFX_MSG_MAP(CMyDlg)

ON_WM_SYSCOMMAND()

ON_WM_PAINT()

ON_WM_QUERYDRAGICON()

ON_BN_CLICKED(IDC_BUTTON1, OnButton1)

ON_BN_CLICKED(IDC_BUTTON5, OnButton5)

ON_WM_TIMER()

ON_MESSAGE(MY_MSGID, DoFIFO)

ON_MESSAGE(MY_MSGID1, repaint)

ON_BN_CLICKED(IDC_BUTTON2, OnButton2)

ON_LBN_SELCHANGE(IDC_LIST1, OnSelchangeList1)

ON_BN_CLICKED(IDC_BUTTON6, OnButton6)

//}}AFX_MSG_MAP

END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////

// CMyDlg message handlers

BOOL CMyDlg::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

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 CMyDlg::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 CMyDlg::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 CMyDlg::OnQueryDragIcon()

{

return (HCURSOR) m_hIcon;

}

void CMyDlg::OnButton1() //从文档读数据

{

// TODO: Add your control notification handler code here

ifstream infile;

infile.open("fifo.txt");

CString str;

CListBox *pCtrl = (CListBox *)GetDlgItem( IDC_LIST1) ;

infile>>a[0];

for(int i=1;i

{

infile>>a[i];

str.Format("%d",a[i]);

pCtrl->AddString(str);

pCtrl->SetCurSel(pCtrl->GetCount()-1);

UpdateData(false);

}

infile.close();

j=1;

}

void CMyDlg::OnButton5()

{

// TODO: Add your control notification handler code here

::PostMessage(this->m_hWnd, MY_MSGID, NULL, NULL);

}

unsigned int __cdecl ThreadProc(LPVOID lParam)

{

((CMyDlg*)lParam)->FIFO();

return 0;

}

void CMyDlg::DoFIFO()

{

::AfxBeginThread(ThreadProc, this, NULL, NULL, NULL, NULL);

}

void CMyDlg::repaint()

{

UpdateData(false);

}

void CMyDlg::FIFO()

{

static int memory[3] = {0};

int time[3] = {0};

int i = 0, j = 0;

int m = -1, n = -1;

int max = -1,maxtime = 0;

int count = 0;

CString str1;

for(i = 1; i

{ m_1=a[i];

//找第一个空闲的物理块

for(j=0; j

{

if(memory[j] == 0)

{

m = j;

break;

}

}

//找与进程相同的标号

for(j = 0; j

{

if(memory[j] ==a[i])

{

n = j;

}

}

//找time 值最大的

for(j = 0; j

{

if(time[j]>maxtime)

{

maxtime = time[j];

max = j;

}

}

if(n == -1) //不存在相同进程

{

if(m != -1) //存在空闲物理块

{

memory[m] = a[i];

time[m] = 0;

for(j = 0;j

{

time[j]++;

}

m = -1;

}

else //不存在空闲物理块

{

memory[max] = a[i];

time[max] = 0;

for(j = 0;j

{

time[j]++;

}

max = -1;

maxtime = 0;

count++;

}

}

else //存在相同的进程

{

memory[n] = a[i];

for(j = 0;j

{

time[j]++;

}

n = -1;

}

for(j = 0 ;j

{

if(j==0)

{

m_2=memory[j];

}

if(j==1)

{

m_3=memory[j];

}

if(j==2)

{

m_4=memory[j];

}

::PostMessage(this->m_hWnd, MY_MSGID1, NULL, NULL);//发送消息

Sleep(500);

}

// printf("\n");

}

m_5=count;

::PostMessage(this->m_hWnd, MY_MSGID1, NULL, NULL);

}

void CMyDlg::OnTimer(UINT nIDEvent)

{

// TODO: Add your message handler code here and/or call default

CDialog::OnTimer(nIDEvent);

}

void CMyDlg::OnButton2()

{

// TODO: Add your control notification handler code here

m_1=0;

m_2=0;

m_3=0;

m_4=0;

m_5=0;

UpdateData(false);

}

void CMyDlg::OnSelchangeList1()

{

// TODO: Add your control notification handler code here

}

void CMyDlg::OnButton6() //手动

{

// TODO: Add your control notification handler code here int num=0;

int num1=0;

if(j>9)

AfxMessageBox("over");

else if(j

{

if(m_2==0)

{

m_1=a[j];

m_2=m_1;

j++;

}

else if(m_3==0)

{

m_1=a[j];

m_3=m_1;

j++;

}

else if(m_4==0)

{

m_1=a[j];

m_4=m_1;

j++;

}

else if(m_4!=0)

{

if(m_2==a[j]||m_3==a[j]||m_4==a[j]) {

m_1=a[j];

j++;

num1++;

}

else //无相同进程

{

if((j-m_6)%3==1)

{m_1=a[j];

m_2=a[j];j++;

num++;

}

else if((j-m_6)%3==2) {m_1=a[j];

m_3=a[j];j++;

num++;

}

else if((j-m_6)%3==0) {m_1=a[j];

m_4=a[j];j++;

num++;

}

}

}m_6+=num1;

m_5+=num;

}

UpdateData(false);

}

时间片轮转算法 //有相同进程

// 时间片轮转调度算法

#include

#include

#include

#include

using namespace std;

enum STATUS {RUN,READY,W AIT,FINISH};

struct PCBNode

{

int processID; //进程ID

STATUS status; //进程状态

int priorityNum; //优先数

int reqTime; //总的需要运行时间

int remainTime; //剩下需要运行时间

int arriveTime; //进入就绪队列时间

int startTime; //开始运行时间

int finishTime; //结束运行时间

int totalTime; //周转时间

float weightTotalTime; //带权周转时间

};

struct QueueNode

{

int ID; //进程ID

struct QueueNode * next; //队列中下一个进程指针

};

struct LinkQueue

{

QueueNode *head;//队首

};

void Fcfs(LinkQueue& Q, int& totalTimeSum, int& weightTotalTimeSum,PCBNo de * ProcessTable);

bool RR_Run(LinkQueue& Q,QueueNode* q, QueueNode* p, const int Roun d,int& currentTime,PCBNode * ProcessTable);

//分配时间片给q 所指进程,p 为刚退出的进程

void RoundRobin(LinkQueue& Q,const int Round, int& totalTimeSum, int& weightTotalTimeSum,PCBNode * ProcessTable);

//时间片轮转调度, 调用RR_Run(),时间片大小设为Round

void InitialQueue(LinkQueue& Q,PCBNode * ProcessTable,const int processnum); //初始化就绪队列

void Input(PCBNode * ProcessTable, const int processnum);

//从input.txt 文件输入数据

int main()

{

LinkQueue Q;//就绪队列

Q.head = NULL;

const int processnum = 16;//进程数

const int Round = 1; //时间片大小

int totalTimeSum = 0; //周转时间

int WeightTotalTimeSum = 0;//带权周转时间

PCBNode * ProcessTable=new PCBNode[processnum]; //进程表

Input(ProcessTable, processnum);

InitialQueue(Q, ProcessTable, processnum);

RoundRobin(Q, Round, totalTimeSum,WeightTotalTimeSum,ProcessTable); cout

Input(ProcessTable, processnum);

InitialQueue(Q, ProcessTable, processnum);

Fcfs(Q, totalTimeSum,WeightTotalTimeSum,ProcessTable);

cout

cout

delete [] ProcessTable;

return 0;

}

void RoundRobin(LinkQueue& Q,const int Round, int& totalTimeSum, int& weightTotalTimeSum,PCBNode * ProcessTable)

{

totalTimeSum = 0; //总的周转时间

weightTotalTimeSum = 0;//平均周转时间

int currentTime = 0; //当前时间

QueueNode* p;

QueueNode* q;

QueueNode* r;

bool finish = false;//调用RR_Run()后, 该进程是否已经做完退出

p = Q.head;

q = p-> next;

while (q != NULL)//从队首开始依次分配时间片

do

{

cout

cout ID

cout ID ID].remainTime

finish = RR_Run(Q, q, p, Round, currentTime, ProcessTab le);//分配时间片给q 进程

cout

if (!finish)//若是进程在本时间片内做完, 则跳出do…while循环 {

if (q-> next == NULL)

{

r = Q.head-> next;

}

else

{

r = q-> next;

}

}

else //否则计算周转时间和带权周转时间

{

totalTimeSum += ProcessTable[q-> ID].totalTime;

weightTotalTimeSum += ProcessTable[q-> ID].weightTotalTime;

delete q; //从队列中删除q 进程

q = p;

}

}while (!finish && (ProcessTable[r-> ID].arriveTime > currentTime + Round));

//下一个进程很晚才来, 则继续给当前进程分配时间片

p = q;

q = q-> next;

if (q == NULL && Q.head-> next!=NULL)

{

p = Q.head;

q = p-> next;

}

delete Q.head;

Q.head = NULL;

}

bool RR_Run(LinkQueue& Q,QueueNode* q, QueueNode* p, const int Roun d,int& currentTime,PCBNode * ProcessTable)

{

if (ProcessTable[q-> ID].remainTime

{

ProcessTable[q-> ID].finishTime = currentTime + ProcessTable[q-> ID].remainTime;

ProcessTable[q-> ID].totalTime += ProcessTable[q-> ID].remainTime;

ProcessTable[q-> ID].weightTotalTime = ProcessTable[q-> ID].totalTime/ProcessTable[q-> ID].reqTime;

currentTime = ProcessTable[q-> ID].finishTime;

p-> next = q-> next;

cout

cout ID

return true;

}

else//此时间片内做不完

{

ProcessTable[q-> ID].remainTime = ProcessTable[q-> ID].remainTime - Round;

ProcessTable[q-> ID].totalTime += Round;

currentTime += Round;

return false;

}

}

void Fcfs(LinkQueue& Q, int& totalTimeSum, int& weightTotalTimeSum,PCBNo de * ProcessTable)

{

totalTimeSum = 0;

weightTotalTimeSum = 0;//平均周转时间

QueueNode* p;

QueueNode* q;

p = Q.head-> next;

if (p !=NULL )

{

ProcessTable[p-> ID].startTime = ProcessTable[p-> ID].arriveTime;

ProcessTable[p-> ID].finishTime = ProcessTable[p-> ID].arriveTime + ProcessTable[p-> ID].reqTime;

}

for(q=p-> next; q!=NULL; q=q-> next)

{

if (ProcessTable[q-> ID].arriveTime ID].finishTime) {

ProcessTable[q-> ID].startTime = ProcessTable[p-> ID].finishTime; ProcessTable[q-> ID].finishTime = ProcessTable[p-> ID].finishTime + ProcessTable[q-> ID].reqTime;

}

else//下个进程到达时间较晚

{

ProcessTable[q-> ID].startTime = ProcessTable[q-> ID].arriveTime; ProcessTable[q-> ID].finishTime = ProcessTable[q-> ID].arriveTime + ProcessTable[q-> ID].reqTime;

}

p = q;

}

for(q=Q.head-> next; q!=NULL; q=q-> next)

{

ProcessTable[q-> ID].totalTime = ProcessTable[q-> ID].finishTime - ProcessTable[q-> ID].arriveTime;

ProcessTable[q-> ID].weightTotalTime = ProcessTable[q-> ID].totalTime/ProcessTable[q-> ID].reqTime;

totalTimeSum += ProcessTable[q-> ID].totalTime;

weightTotalTimeSum += ProcessTable[q-> ID].weightTotalTime; }

int t = 0;

for(q=Q.head-> next; q!=NULL; q=q-> next)

{

cout

while ( t ID].finishTime )

{

cout ID

}

if (q-> next != NULL)

{

cout ID

cout ID ID].totalTime

cout ID ID].weightTotalTime

}

else

{

cout ID

cout ID ID].totalTime

cout ID ID].weightTotalTime

}

}

cout

p = Q.head;

for(q=p-> next; q!=NULL; q=q-> next)

{

delete p;

p = q;

}

}

void InitialQueue(LinkQueue& Q, PCBNode * ProcessTable,const int processnu m)

{

//初始化

for (int i=0;i

{

ProcessTable[i].processID=i;

ProcessTable[i].reqTime=ProcessTable[i].remainTime;

ProcessTable[i].finishTime=0;

ProcessTable[i].startTime=0;

ProcessTable[i].status=WAIT;

ProcessTable[i].totalTime=0;

ProcessTable[i].weightTotalTime=0;

}

Q.head = new QueueNode;

Q.head-> next = NULL;

QueueNode * p;

QueueNode * q;

for (i=0;i

{

p = new QueueNode;

p-> ID = i;

p-> next = NULL;

if (i == 0)

{

Q.head-> next = p;

}

else

q-> next = p;

q = p;

}

}

void Input(PCBNode * ProcessTable, const int processnum)

{

FILE *fp; //读入线程的相关内容

if((fp=fopen( "input.txt ", "r "))==NULL)

{

cout

exit(0);

}

for(int i=0;i

{

fscanf(fp, "%d %d %d ",&ProcessTable[i].arriveTime,&ProcessTable[i].remainTime,&ProcessTable[i].priorityNum); }

fclose(fp);

}

先进先出算法

// 先进先出Dlg.cpp : implementation file

//

#include "stdafx.h"

#include "先进先出.h"

#include "先进先出Dlg.h"

#include

#ifdef _DEBUG

#define new DEBUG_NEW

#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);

//}}AFX_VIRTUAL

// Implementation

protected:

//{{AFX_MSG(CAboutDlg)

//}}AFX_MSG

DECLARE_MESSAGE_MAP()

};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)

{ // DDX/DDV support

//{{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()

/////////////////////////////////////////////////////////////////////////////

// CMyDlg dialog

CMyDlg::CMyDlg(CWnd* pParent /*=NULL*/)

: CDialog(CMyDlg::IDD, pParent)

{

//{{AFX_DATA_INIT(CMyDlg)

m_1 = 0;

m_2 = 0;

m_3 = 0;

m_4 = 0;

m_5 = 0;

m_0 = _T("");

m_6 = 0;

//}}AFX_DATA_INIT

// Note that LoadIcon does not require a subsequent DestroyIcon in Win32

m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

}

void CMyDlg::DoDataExchange(CDataExchange* pDX)

{

CDialog::DoDataExchange(pDX);

//{{AFX_DATA_MAP(CMyDlg)

DDX_Text(pDX, IDC_EDIT1, m_1);

DDX_Text(pDX, IDC_EDIT2, m_2);

DDX_Text(pDX, IDC_EDIT3, m_3);

DDX_Text(pDX, IDC_EDIT4, m_4);

DDX_Text(pDX, IDC_EDIT5, m_5);

DDX_LBString(pDX, IDC_LIST1, m_0);

DDX_Text(pDX, IDC_EDIT6, m_6);

//}}AFX_DATA_MAP

}

BEGIN_MESSAGE_MAP(CMyDlg, CDialog)

//{{AFX_MSG_MAP(CMyDlg)

ON_WM_SYSCOMMAND()

ON_WM_PAINT()

ON_WM_QUERYDRAGICON()

ON_BN_CLICKED(IDC_BUTTON1, OnButton1)

ON_BN_CLICKED(IDC_BUTTON5, OnButton5)

ON_WM_TIMER()

ON_MESSAGE(MY_MSGID, DoFIFO)

ON_MESSAGE(MY_MSGID1, repaint)

ON_BN_CLICKED(IDC_BUTTON2, OnButton2)

ON_LBN_SELCHANGE(IDC_LIST1, OnSelchangeList1)

ON_BN_CLICKED(IDC_BUTTON6, OnButton6)

//}}AFX_MSG_MAP

END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////

// CMyDlg message handlers

BOOL CMyDlg::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

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 CMyDlg::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 CMyDlg::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 CMyDlg::OnQueryDragIcon()

{

return (HCURSOR) m_hIcon;

}

void CMyDlg::OnButton1() //从文档读数据

{

// TODO: Add your control notification handler code here

ifstream infile;

infile.open("fifo.txt");

CString str;

CListBox *pCtrl = (CListBox *)GetDlgItem( IDC_LIST1) ;

infile>>a[0];

for(int i=1;i

{

infile>>a[i];

str.Format("%d",a[i]);

pCtrl->AddString(str);

pCtrl->SetCurSel(pCtrl->GetCount()-1);

UpdateData(false);

}

infile.close();

j=1;

}

void CMyDlg::OnButton5()

{

// TODO: Add your control notification handler code here

::PostMessage(this->m_hWnd, MY_MSGID, NULL, NULL);

}

unsigned int __cdecl ThreadProc(LPVOID lParam)

{

((CMyDlg*)lParam)->FIFO();

return 0;

}

void CMyDlg::DoFIFO()

{

::AfxBeginThread(ThreadProc, this, NULL, NULL, NULL, NULL);

}

void CMyDlg::repaint()

{

UpdateData(false);

}

void CMyDlg::FIFO()

{

static int memory[3] = {0};

int time[3] = {0};

int i = 0, j = 0;

int m = -1, n = -1;

int max = -1,maxtime = 0;

int count = 0;

CString str1;

for(i = 1; i

{ m_1=a[i];

//找第一个空闲的物理块

for(j=0; j

{

if(memory[j] == 0)

{

m = j;

break;

}

}

//找与进程相同的标号

for(j = 0; j

{

if(memory[j] ==a[i])

{

n = j;

}

}

//找time 值最大的

for(j = 0; j

{

if(time[j]>maxtime)

{

maxtime = time[j];

max = j;

}

}

if(n == -1) //不存在相同进程

{

if(m != -1) //存在空闲物理块

{

memory[m] = a[i];

time[m] = 0;

for(j = 0;j

{

time[j]++;

}

m = -1;

}

else //不存在空闲物理块

{

memory[max] = a[i];

time[max] = 0;

for(j = 0;j

{

time[j]++;

}

max = -1;

maxtime = 0;

count++;

}

}

else //存在相同的进程

{

memory[n] = a[i];

for(j = 0;j

{

time[j]++;

}

n = -1;

}

for(j = 0 ;j

{

if(j==0)

{

m_2=memory[j];

}

if(j==1)

{

m_3=memory[j];

}

if(j==2)

{

m_4=memory[j];

}

::PostMessage(this->m_hWnd, MY_MSGID1, NULL, NULL);//发送消息

Sleep(500);

}

// printf("\n");

}

m_5=count;

::PostMessage(this->m_hWnd, MY_MSGID1, NULL, NULL);

}

void CMyDlg::OnTimer(UINT nIDEvent)

{

// TODO: Add your message handler code here and/or call default

CDialog::OnTimer(nIDEvent);

}

void CMyDlg::OnButton2()

{

// TODO: Add your control notification handler code here

m_1=0;

m_2=0;

m_3=0;

m_4=0;

m_5=0;

UpdateData(false);

}

void CMyDlg::OnSelchangeList1()

{

// TODO: Add your control notification handler code here

}

void CMyDlg::OnButton6() //手动

{

// TODO: Add your control notification handler code here int num=0;

int num1=0;

if(j>9)

AfxMessageBox("over");

else if(j

{

if(m_2==0)

{

m_1=a[j];

m_2=m_1;

j++;

}

else if(m_3==0)

{

m_1=a[j];

m_3=m_1;

j++;

}

else if(m_4==0)

{

m_1=a[j];

m_4=m_1;

j++;

}

else if(m_4!=0)

{

if(m_2==a[j]||m_3==a[j]||m_4==a[j]) {

m_1=a[j];

j++;

num1++;

}

else //无相同进程

{

if((j-m_6)%3==1)

{m_1=a[j];

m_2=a[j];j++;

num++;

}

else if((j-m_6)%3==2) {m_1=a[j];

m_3=a[j];j++;

num++;

}

else if((j-m_6)%3==0) {m_1=a[j];

m_4=a[j];j++;

num++;

}

}

}m_6+=num1;

m_5+=num;

}

UpdateData(false);

}

时间片轮转算法 //有相同进程

// 时间片轮转调度算法

#include

#include

#include

#include

using namespace std;

enum STATUS {RUN,READY,W AIT,FINISH};

struct PCBNode

{

int processID; //进程ID

STATUS status; //进程状态

int priorityNum; //优先数

int reqTime; //总的需要运行时间

int remainTime; //剩下需要运行时间

int arriveTime; //进入就绪队列时间

int startTime; //开始运行时间

int finishTime; //结束运行时间

int totalTime; //周转时间

float weightTotalTime; //带权周转时间

};

struct QueueNode

{

int ID; //进程ID

struct QueueNode * next; //队列中下一个进程指针

};

struct LinkQueue

{

QueueNode *head;//队首

};

void Fcfs(LinkQueue& Q, int& totalTimeSum, int& weightTotalTimeSum,PCBNo de * ProcessTable);

bool RR_Run(LinkQueue& Q,QueueNode* q, QueueNode* p, const int Roun d,int& currentTime,PCBNode * ProcessTable);

//分配时间片给q 所指进程,p 为刚退出的进程

void RoundRobin(LinkQueue& Q,const int Round, int& totalTimeSum, int& weightTotalTimeSum,PCBNode * ProcessTable);

//时间片轮转调度, 调用RR_Run(),时间片大小设为Round

void InitialQueue(LinkQueue& Q,PCBNode * ProcessTable,const int processnum); //初始化就绪队列

void Input(PCBNode * ProcessTable, const int processnum);

//从input.txt 文件输入数据

int main()

{

LinkQueue Q;//就绪队列

Q.head = NULL;

const int processnum = 16;//进程数

const int Round = 1; //时间片大小

int totalTimeSum = 0; //周转时间

int WeightTotalTimeSum = 0;//带权周转时间

PCBNode * ProcessTable=new PCBNode[processnum]; //进程表

Input(ProcessTable, processnum);

InitialQueue(Q, ProcessTable, processnum);

RoundRobin(Q, Round, totalTimeSum,WeightTotalTimeSum,ProcessTable); cout

Input(ProcessTable, processnum);

InitialQueue(Q, ProcessTable, processnum);

Fcfs(Q, totalTimeSum,WeightTotalTimeSum,ProcessTable);

cout

cout

delete [] ProcessTable;

return 0;

}

void RoundRobin(LinkQueue& Q,const int Round, int& totalTimeSum, int& weightTotalTimeSum,PCBNode * ProcessTable)

{

totalTimeSum = 0; //总的周转时间

weightTotalTimeSum = 0;//平均周转时间

int currentTime = 0; //当前时间

QueueNode* p;

QueueNode* q;

QueueNode* r;

bool finish = false;//调用RR_Run()后, 该进程是否已经做完退出

p = Q.head;

q = p-> next;

while (q != NULL)//从队首开始依次分配时间片

do

{

cout

cout ID

cout ID ID].remainTime

finish = RR_Run(Q, q, p, Round, currentTime, ProcessTab le);//分配时间片给q 进程

cout

if (!finish)//若是进程在本时间片内做完, 则跳出do…while循环 {

if (q-> next == NULL)

{

r = Q.head-> next;

}

else

{

r = q-> next;

}

}

else //否则计算周转时间和带权周转时间

{

totalTimeSum += ProcessTable[q-> ID].totalTime;

weightTotalTimeSum += ProcessTable[q-> ID].weightTotalTime;

delete q; //从队列中删除q 进程

q = p;

}

}while (!finish && (ProcessTable[r-> ID].arriveTime > currentTime + Round));

//下一个进程很晚才来, 则继续给当前进程分配时间片

p = q;

q = q-> next;

if (q == NULL && Q.head-> next!=NULL)

{

p = Q.head;

q = p-> next;

}

delete Q.head;

Q.head = NULL;

}

bool RR_Run(LinkQueue& Q,QueueNode* q, QueueNode* p, const int Roun d,int& currentTime,PCBNode * ProcessTable)

{

if (ProcessTable[q-> ID].remainTime

{

ProcessTable[q-> ID].finishTime = currentTime + ProcessTable[q-> ID].remainTime;

ProcessTable[q-> ID].totalTime += ProcessTable[q-> ID].remainTime;

ProcessTable[q-> ID].weightTotalTime = ProcessTable[q-> ID].totalTime/ProcessTable[q-> ID].reqTime;

currentTime = ProcessTable[q-> ID].finishTime;

p-> next = q-> next;

cout

cout ID

return true;

}

else//此时间片内做不完

{

ProcessTable[q-> ID].remainTime = ProcessTable[q-> ID].remainTime - Round;

ProcessTable[q-> ID].totalTime += Round;

currentTime += Round;

return false;

}

}

void Fcfs(LinkQueue& Q, int& totalTimeSum, int& weightTotalTimeSum,PCBNo de * ProcessTable)

{

totalTimeSum = 0;

weightTotalTimeSum = 0;//平均周转时间

QueueNode* p;

QueueNode* q;

p = Q.head-> next;

if (p !=NULL )

{

ProcessTable[p-> ID].startTime = ProcessTable[p-> ID].arriveTime;

ProcessTable[p-> ID].finishTime = ProcessTable[p-> ID].arriveTime + ProcessTable[p-> ID].reqTime;

}

for(q=p-> next; q!=NULL; q=q-> next)

{

if (ProcessTable[q-> ID].arriveTime ID].finishTime) {

ProcessTable[q-> ID].startTime = ProcessTable[p-> ID].finishTime; ProcessTable[q-> ID].finishTime = ProcessTable[p-> ID].finishTime + ProcessTable[q-> ID].reqTime;

}

else//下个进程到达时间较晚

{

ProcessTable[q-> ID].startTime = ProcessTable[q-> ID].arriveTime; ProcessTable[q-> ID].finishTime = ProcessTable[q-> ID].arriveTime + ProcessTable[q-> ID].reqTime;

}

p = q;

}

for(q=Q.head-> next; q!=NULL; q=q-> next)

{

ProcessTable[q-> ID].totalTime = ProcessTable[q-> ID].finishTime - ProcessTable[q-> ID].arriveTime;

ProcessTable[q-> ID].weightTotalTime = ProcessTable[q-> ID].totalTime/ProcessTable[q-> ID].reqTime;

totalTimeSum += ProcessTable[q-> ID].totalTime;

weightTotalTimeSum += ProcessTable[q-> ID].weightTotalTime; }

int t = 0;

for(q=Q.head-> next; q!=NULL; q=q-> next)

{

cout

while ( t ID].finishTime )

{

cout ID

}

if (q-> next != NULL)

{

cout ID

cout ID ID].totalTime

cout ID ID].weightTotalTime

}

else

{

cout ID

cout ID ID].totalTime

cout ID ID].weightTotalTime

}

}

cout

p = Q.head;

for(q=p-> next; q!=NULL; q=q-> next)

{

delete p;

p = q;

}

}

void InitialQueue(LinkQueue& Q, PCBNode * ProcessTable,const int processnu m)

{

//初始化

for (int i=0;i

{

ProcessTable[i].processID=i;

ProcessTable[i].reqTime=ProcessTable[i].remainTime;

ProcessTable[i].finishTime=0;

ProcessTable[i].startTime=0;

ProcessTable[i].status=WAIT;

ProcessTable[i].totalTime=0;

ProcessTable[i].weightTotalTime=0;

}

Q.head = new QueueNode;

Q.head-> next = NULL;

QueueNode * p;

QueueNode * q;

for (i=0;i

{

p = new QueueNode;

p-> ID = i;

p-> next = NULL;

if (i == 0)

{

Q.head-> next = p;

}

else

q-> next = p;

q = p;

}

}

void Input(PCBNode * ProcessTable, const int processnum)

{

FILE *fp; //读入线程的相关内容

if((fp=fopen( "input.txt ", "r "))==NULL)

{

cout

exit(0);

}

for(int i=0;i

{

fscanf(fp, "%d %d %d ",&ProcessTable[i].arriveTime,&ProcessTable[i].remainTime,&ProcessTable[i].priorityNum); }

fclose(fp);

}


相关文章

  • 软件技术基础知识要点复习
  • 软件技术基础知识要点复习 1.软件的概念,软件的特性,软件的分类图1-5,软件的内容?图1-6 概念:软件是"与计算机系统操作有关的程序.过程.规则,以及任何有关的文档资料和数据". 或软件是程序.数据及相应文档所组成的 ...查看


  • 考点1:数据结构与算法
  • A )所谓算法就是计算方法 B )程序可以作为算法的一种描述方法 C )算法设计只需考虑得到计算结果 D )算法设计可以忽略算法的运算时间 题目解析:算法是一组有穷指令集,是解题方案的准确而完整的描述.通俗地说,算法就是计算机解题的过程, ...查看


  • 2014黑龙江省数据结构考试题库
  • 1.广义表A=(A,B,(C,D),(E,(F,G))),则head(tail(head(tail(tail(A)))))=( D ). A) (G) B) (D) C) C D) D 2.若采用邻接矩阵法存储一个n个顶点的无向图,则该邻接 ...查看


  • 处理机调度参考
  • 实验原理: 时间片轮转调度算法和优先权调度算法本质上是一致的,只是在调度时选择的策略不一样而已,其程序的流程图是一致的,所以在此仅给出了一个流程图.具体算法流程图如图1所示. 图1 处理机调度算法流程图 1. 时间片轮转调度算法 当系统按时 ...查看


  • 二级access公共基础历年真题解析
  • 全国计算机等级考试二级公共基础历年真题解析  2010年9月 选择题:(1)下列叙述中正确的是( ) A)线性表的链式存储结构与顺序存储结构所需要的存储空间是相同的 B)线性表的链式存储结构所需要的存储空间一般要多于顺序存储结构 C)线性 ...查看


  • 会计公式1
  • 资产=负债+所有者权益 固定资产原值-累计折旧=固定资产净值 一.利润计算公式 1.营业利润 =营业收入-营业成本-营业税金及附加-营业费用-管理费用 -财务费用-资产减值损失+公允价值变动收益(或减变动损失)+投资收益(或 减投资损失) ...查看


  • 实验二___虚拟存储器
  • 实验二 虚拟存储器 一.实验内容 模拟分页式虚拟存储管理中硬件的地址转换和缺页中断,以及选择页面调度算法处理缺 页中断. 二.实验目的 在计算机系统中,为了提高主存利用率,往往把辅助存储器(如磁盘)作为主存储器的 扩充,使多道运行的作业的全 ...查看


  • 作业调度算法
  • 2011年第17 期 ● ◇高教论述◇ 作业调度算法 崔帅1楚蓝天2高凯2 (1. 中国矿业大学环境与测绘学院江苏徐州221116: 2. 中国矿业大学化工学院江苏徐州221116) [摘要]在多道系统中,对批处理作业需要进行作业调度.作业 ...查看


  • 数据结构答案
  • 第一章 一.选择题 1 从逻辑上可以把数据结构分为( C)两大类. A .动态结构.静态结构 B.顺序结构.链式结构 C .线性结构.非线性结构 D.初等结构.构造型结构 2在下面的程序段中,对x 的赋值语句的频度为(C ) FOR i:= ...查看


热门内容