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

📄 maintest.cpp

📁 处理机调度
💻 CPP
字号:
#include "Ulti.h"
#include "PCB.h"
#include "EQueue.h"
#include "GraphAlgorithm.h"
#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <time.h>
#include <math.h>

#define DEBUG 1

char *inFileName="inputData.txt";
char *outFileName="outputData.txt";

int bRun;
long WINAPI WndProc(HWND hWnd,UINT iMessage,WPARAM wParam,LPARAM lParam);
BOOL InitWindowsClass(HINSTANCE hInstance);
BOOL InitWindows(HINSTANCE hInstance,int nCmdShow);
void  stiProcessMag(HDC  hdc);
void IntToChar(int intData,char* inChar);
HWND hWndMain;
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);



//program starting.
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInst,LPSTR lpszCmdLine,int nCmdShow)
{
	MSG   msg;
	if(!InitWindowsClass(hInstance))
		return FALSE;
	
	if(!InitWindows(hInstance,nCmdShow))
		return FALSE;
	
	//Core message looping
	while(GetMessage(&msg,NULL,0,0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	return msg.wParam;

}

//main wndProc function: message looping
long WINAPI WndProc(HWND hWnd,UINT iMessage,WPARAM wParam,LPARAM lParam)
{
	HDC hDC;
	HBRUSH hBrush;
	HPEN hPen;
	PAINTSTRUCT PtStr;

	switch(iMessage)
	{
		case WM_PAINT:
			//First draw,a black line
			hDC=BeginPaint(hWnd,&PtStr);
			hPen=(HPEN)GetStockObject(NULL_PEN);//get empty brush
			SelectObject(hDC,hPen);
			hBrush=(HBRUSH)GetStockObject(BLACK_BRUSH);
			SelectObject(hDC,hBrush);
			
			hPen=CreatePen(PS_SOLID,2,RGB(255,0,0));//create pen
			SelectObject(hDC,hPen);		
			


			stiProcessMag(hDC);
		
			DeleteObject(hPen);
			DeleteObject(hBrush);
			EndPaint(hWnd,&PtStr);
			
		
			//MessageBox(hWnd,"HelloWorld!","Msg Box",MB_OK);
			return 0;
		case WM_DESTROY:
			PostQuitMessage(0);
			return 0;
		default:
			return DefWindowProc(hWnd,iMessage,wParam,lParam);
				
	}
}

//Init the Window to show out.
BOOL InitWindows(HINSTANCE hInstance,int nCmdShow)
{
	HWND hWnd;
	hWnd=CreateWindow("WinFill",
				"进程调度摸拟程序图示",
					WS_OVERLAPPEDWINDOW,
				CW_USEDEFAULT,
				0,
				CW_USEDEFAULT,
				0,
				NULL,	
				NULL,
				hInstance,
				NULL	
			    );
	if(!hWnd)
		return FALSE;
	hWndMain=hWnd;
	ShowWindow(hWnd,nCmdShow);
	UpdateWindow(hWnd);
	return TRUE;
}

//Set wndClass Propertity
BOOL InitWindowsClass(HINSTANCE hInstance)
{
	WNDCLASS wndClass;

	wndClass.cbClsExtra=0;
	wndClass.cbWndExtra=0;
	wndClass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
	wndClass.hCursor=LoadCursor(NULL,IDC_ARROW);
	wndClass.hIcon=LoadIcon(NULL,"END");
	wndClass.hInstance=hInstance;
	wndClass.lpfnWndProc=WndProc;
	wndClass.lpszClassName="WinFill";
	wndClass.lpszMenuName=NULL;
	wndClass.style=CS_HREDRAW|CS_VREDRAW;
	
	return RegisterClass(&wndClass);
}

void stiProcessMag(HDC  hDC)
{
	/*--------Open file--------*/
	FILE* inputFile;
	FILE* outputFile;
	int bDelay;

	/*=--------creation part-------*/
	char mode;
	int numOfThread; 
	PCBList pcbList,p;
	ptrPCBNode tmpNode;
	/*--------main process-------*/
	pQueue readyQueue,endQueue;
	ptrProcess runProcess,tmpProcess;

	int count;
	int delayTime;
	DWORD result=0;


	if((inputFile=fopen(inFileName,"rb"))==NULL)
	{	
		printf("open inputFile error\n");
		return ;
	}
	
	if((outputFile=fopen(outFileName,"wb"))==NULL)
	{
		printf("open outputFile error\n");
		return ;
	}
			
		printf("oepn files success \n");
	
	/*Create the PCB Table*/

	
	/*create a pcbList with a null head.*/	
	pcbList=(ptrPCBNode)malloc(sizeof(struct PCBNode));
	p=pcbList;

	fscanf(inputFile,"mode=%c\r\n",&mode);
	fscanf(inputFile,"numOfThread=%d\r\n",&numOfThread);
	fscanf(inputFile,"delay=%d\r\n",&bDelay);
	fscanf(inputFile,"delayTime=%d\r\n",&delayTime);
	printf("------------program init starts------------\n");
	fprintf(outputFile,"------------program init starts------------\r\n");
//	int mCount=0;
	char tmpChar[10];
	int x0,y0,height;
	int x1,width;

	x0=10;
	y0=60;
	x1=100;
	height=40;
	width=10;
	int i;
	for(i=0;i<=30;i+=2)
	{
		IntToChar(i,tmpChar);
		TextOut(hDC,x1+i*width,y0-height-5,tmpChar,strlen(tmpChar));
	}
	

	DDALine(x1,y0-height+15,x1+700,y0-height+15,0,hDC);
	TextOut(hDC,x1+600,y0-20,"T",1);
	TextOut(hDC,x0+14,y0-height-5,"Pri SevTime",strlen("Pri SevTime"));

	if(mode=='0')/*Read process definition from the file*/
		{
 					/*Creation with input list*/
					int i;
					for(i=0;i<numOfThread;i++)
					{
							tmpNode=(ptrPCBNode)malloc(sizeof(struct PCBNode));
							fscanf(inputFile,"%d,%d,%s\r\n",&tmpNode->priorityNum,&tmpNode->remainSecs,tmpNode->processName);	
							tmpNode->processName[strlen(tmpNode->processName)]='\0';
							tmpNode->processID=i;
							tmpNode->staturs=STA_READY;
							
							if(tmpNode->remainSecs>0)
							{
									p->next=tmpNode;
									p=p->next;
									
									strcpy(tmpChar,"P");
									tmpChar[1]=48+i;
									tmpChar[2]='\0';
									TextOut(hDC,x0,y0+height*i,tmpChar,strlen(tmpChar));
									
									IntToChar(tmpNode->priorityNum,tmpChar);
									TextOut(hDC,x0+22,y0+height*i,tmpChar,strlen(tmpChar));
								
									IntToChar(tmpNode->remainSecs,tmpChar);
									TextOut(hDC,x0+40,y0+height*i,tmpChar,strlen(tmpChar));

			printf("ProcessID:%d,Name:%s,Priority:%d,RemainSecs:%d Staturs: Ready\n",tmpNode->processID,tmpNode->processName,tmpNode->priorityNum,tmpNode->remainSecs);
			fprintf(outputFile,"ProcessID:%d,Name:%s,Priority:%d,RemainSecs:%d Staturs: Ready\r\n",tmpNode->processID,tmpNode->processName,tmpNode->priorityNum,tmpNode->remainSecs);

							}	
					}
					p->next=NULL;//Correction: add this to has effects in sort algorithm
			
		}
	else if(mode=='1')
		{
					int i;
					srand((unsigned int)time (NULL));
					
					for(i=0;i<numOfThread;i++)
					{
							tmpNode=(ptrPCBNode)malloc(sizeof(struct PCBNode));
								
							tmpNode->priorityNum=(int)eRandom(10);
							
							tmpNode->remainSecs=(int)eRandom(10);
							
							tmpNode->processID=i;
							
							strcpy(tmpNode->processName,"Process");
							tmpNode->processName[7]=48+i;
							tmpNode->processName[8]='\0';

							tmpNode->staturs=STA_READY;
							
							
							if(tmpNode->remainSecs>0)
							{
									p->next=tmpNode;
									p=p->next;
									
									strcpy(tmpChar,"P");
									tmpChar[1]=48+i;
									tmpChar[2]='\0';
									TextOut(hDC,x0,y0+height*i,tmpChar,strlen(tmpChar));
									//mCount++;

									IntToChar(tmpNode->priorityNum,tmpChar);
									TextOut(hDC,x0+22,y0+height*i,tmpChar,strlen(tmpChar));
								
									IntToChar(tmpNode->remainSecs,tmpChar);
									TextOut(hDC,x0+40,y0+height*i,tmpChar,strlen(tmpChar));
									
			printf("ProcessID:%d,Name:%s,Priority:%d,RemainSecs:%d Staturs: Ready\n",tmpNode->processID,tmpNode->processName,tmpNode->priorityNum,tmpNode->remainSecs);
			fprintf(outputFile,"ProcessID:%d,Name:%s,Priority:%d,RemainSecs:%d Staturs: Ready\r\n",tmpNode->processID,tmpNode->processName,tmpNode->priorityNum,tmpNode->remainSecs);
							}	
						}
					p->next=NULL;//Correction: add this to has effects in sort algorithm
		}	
	else
		{
			printf("mode is illegal!\n");
			return ;
		}
	printf("------------program init ends------------\n");
	fprintf(outputFile,"------------program init ends------------\r\n");
	/*Sort the pcb table*/		
	sortPCB(pcbList);							//8:56 test ok
	if(DEBUG)printf("sortPCB successfully\n");	
	
	/*init queue*/
	endQueue=createNullQueueWithHead();
	readyQueue=createNullQueueWithHead();
		
	tmpNode=pcbList;
	tmpNode=tmpNode->next;
	
	while(tmpNode!=NULL)						//9:38 test ok
	{
		tmpProcess=(ptrProcess)malloc(sizeof(struct Process));
		tmpProcess->processID=tmpNode->processID;
		tmpProcess->staturs=tmpNode->staturs;
		tmpProcess->priorityNum=tmpNode->priorityNum;
		tmpProcess->remainSecs=tmpNode->remainSecs;
		strcpy(tmpProcess->processName,tmpNode->processName);
		enQueue(readyQueue,tmpProcess);
		tmpNode=tmpNode->next;
	}
	
	if(DEBUG)printf("create readyQueue successfully\n");
	runProcess=NULL;
	/*The core of process management*/
	printf("------------start of main simulation program------------\n");
	fprintf(outputFile,"------------start of main simulation program------------\r\n");
	count=0;
	while(!isEmpty(readyQueue)||runProcess!=NULL)
	{
		if(bDelay)
			Sleep(delayTime);
		//	eDelay(delayTime);/*DELAY 1 SECS*/
		
		if(runProcess!=NULL)
			{
			count++;
			
			MoveToEx(hDC,x1,y0+runProcess->processID*height,NULL);
			x1+=width;
			LineTo(hDC,x1,y0+runProcess->processID*height);
			
	printf("Line:%d ProcessID:%d,Name:%s,Priority:%d,RemainSecs:%d Staturs: Running\n",count,runProcess->processID,runProcess->processName,runProcess->priorityNum,runProcess->remainSecs);
	fprintf(outputFile,"Line:%d ProcessID:%d,Name:%s,Priority:%d,RemainSecs:%d Staturs: Running\r\n",count,runProcess->processID,runProcess->processName,runProcess->priorityNum,runProcess->remainSecs);
					runProcess->priorityNum++;
					runProcess->remainSecs--;
					
					if(runProcess->remainSecs==0)/*if time use out,end the process*/
						{
							runProcess->staturs=STA_END;
							enQueue(endQueue,runProcess);
							updatePCBList(pcbList,runProcess);
							count++;
	printf("Line:%d ProcessID:%d,Name:%s,Priority:%d,RemainSecs:%d Staturs: Ends\n",count,runProcess->processID,runProcess->processName,runProcess->priorityNum,runProcess->remainSecs);
	fprintf(outputFile,"Line:%d ProcessID:%d,Name:%s,Priority:%d,RemainSecs:%d Staturs: Ends\r\n",count,runProcess->processID,runProcess->processName,runProcess->priorityNum,runProcess->remainSecs);
						}
					else if(!isEmpty(readyQueue))
						{
							runProcess->staturs=STA_READY;
							insert(readyQueue,runProcess);
							updatePCBList(pcbList,runProcess);	
						}
			}
		
		/*apply new mem for the new node*/
		//Correction:runProcess!=NULL should be placed at front
		if(runProcess!=NULL&&runProcess->remainSecs==0)//for the last thread consult.&& runProcess!=firstNode(NULL)
			runProcess=NULL;
		
		if(!isEmpty(readyQueue))
		{
			runProcess=(ptrProcess)malloc(sizeof(struct Process));	
			/*wake up a new thread*/	
			runProcess=deQueue(readyQueue);
			runProcess->staturs=STA_RUN;
			updatePCBList(pcbList,runProcess);
		}

		if(count>90)
			break;
	}//10:03 test ok
	printf("------------end of main simulation program------------\n");
	fprintf(outputFile,"------------end of main simulation program------------\r\n");

	fclose(inputFile);
	fclose(outputFile);

	printf("program ends successfully\n");
	getchar();
	
	bRun=0;
	return;
	//return ;
}

//only convert plus data to minus data
void IntToChar(int intData,char* inChar)
{
		int tmpData;
		int i,len;
		/*
		if(inChar==NULL)
			MessageBox(hWnd,"in IntToChar,in Char is null\n!","Warning!",MB_OK);
		if(inData<0)
			MessageBox(hWnd,"in IntToChar,inData is minus\n!","Warning!",MB_OK);
		*/
		if(intData<10)
			len=1;
		else if(intData<100)
			len=2;
		else if(intData<1000)
			len=3;
		else if(intData<10000)
			len=4;
		else 
			len=5;
		
		i=len-1;
		while(i>=0)
		{
			tmpData=48+intData%10;
			inChar[i]=tmpData;
			i--;
			intData/=10;
		}

		inChar[len]='\0';
}

⌨️ 快捷键说明

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