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

📄 maintest.cpp.bak

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

#define DEBUG 1

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


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);
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);
			
			//TextOut(hDC,50,50,"Hello World!",strlen("Hello World!"));
			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;

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

	printf("------------program init starts------------\n");
	fprintf(outputFile,"------------program init starts------------\r\n");
//	int mCount=0;
	char tmpChar[10];
	int x0,y0,height;

	x0=20;
	y0=60;
	x1=100
	height=40;
	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,"Process");
									tmpChar[7]=48+i;
									tmpChar[8]='\0';
									TextOut(hDC,x0,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,"Process");
									tmpChar[7]=48+i;
									tmpChar[8]='\0';
									TextOut(hDC,x0,y0+height*i,tmpChar,strlen(tmpChar));
									//mCount++;
									
			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)
			eDelay(1000);/*DELAY 1 SECS*/
		
		if(runProcess!=NULL)
			{
			count++;
	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);
		}
	}//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();
	return ;
}

⌨️ 快捷键说明

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