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

📄 memsim.c

📁 solutions for some problems in Linux
💻 C
字号:
/****************************************************************************** * memsim.c * This file is part of "Assignment 2: Memory Management"  * Copyright (C) 2008 - c506001 (email: c506001@cse.hcmut.edu.vn ) * * Note: * ----- * These codes are used for reference, but not complete. Students can modified * them to satisfy the requirements. *  * Content: * --------  * The Main Function * * System and Networking Department * Faculty of Computer Science and Engineering * Ho Chi Minh City University of Technology ******************************************************************************/#include <stdlib.h>#include <stdio.h> #include <unistd.h>#include <string.h>#include "sys.h"#include "memsim.h"/** * parseArguments: analyse the parametters from user **/static int parseArguments(int argc, char **argv, struct Options *opts){	// TODO: Complete this	int opt;	int errFlag = 0;	extern char * optarg;          /* (global variable) command-line options */	// parse command line 	while ((opt = getopt(argc, argv, "o:t:s:h")) != EOF) {		switch (opt) {			case 'h':				printf("SYNOPSIS: memsim [-h] [-t taskfile] [-s sysfile] [-o sysfile]\n");				printf("	[-h]          : Show help information\n");				printf("	[-t taskfile] : Choose Task file name\n");				printf("	[-s sysfile]  : Choose System file name\n");
				printf("	[-o sysfile]  : Choose Output file name\n");				break;			case 's':				// TODO
				strcpy(opts->sysFile,optarg);				break;			case 't':				// TODO
				strcpy(opts->taskFile,optarg);				break;			case 'o':				// TODO
				strcpy(opts->outFile,optarg);				break;  			default:				errFlag = 1;				break;		}	}	if (errFlag) {		fprintf(stderr, "Error: parsing arg failed\n");		return -1;	}	return 0;}/**  * createMemRequestList: create the list of memory requests * @filename: task file * @mReqs: list of memory requests (output of this function) **/static int createMemRequestList(char *fileName, struct MemRequestList *mReqs){	int err;	char strCmd[MAX_CMD_NAME_LEN];	int cmd;	int pid;	int mem;	FILE *f = fopen(fileName, "r");	int count = 0;	while (count < MAX_MEM_REQ) {		err = fscanf(f, "%s %d %d", strCmd, &pid, &mem);		if (err == EOF) {			printf("Read task file complete\n");			break;		}		if (strcmp(strCmd,"START") == 0)			cmd = START;		else if (strcmp(strCmd,"ACCESS") == 0)			cmd = ACCESS;		else if (strcmp(strCmd,"FINISH") == 0)			cmd = FINISH;		else			continue;		mReqs->elems[count].cmd = cmd;		mReqs->elems[count].pid = pid;		mReqs->elems[count].mem = mem;		count++;	}	mReqs->count = count;	return 0;}/**  * configSystem: configure the system: ram, framesize * @filename: sys file * @system: system **/static int configSystem(char *fileName, struct System *system){	int err;	int count = 0;	int value;	int memSize;	int i;	char strCmd[20];	FILE *f = fopen(fileName, "r");	while (count < 1) {		err = fscanf(f, "%s = %d", strCmd, &value);		if (err == EOF) {			printf("Read script file complete\n");			break;		}		if (strcmp(strCmd, "RAM") == 0) {			printf("RAM = %dMB\n", value);			memSize = value * (1 << 20);		}		count++;	}	system->nFrames = memSize / (1 << N_OFFSET_BITS);	system->frameStatus = malloc(system->nFrames * sizeof(struct FrameStatus));	for (i = 0; i < system->nFrames; i++) {		system->frameStatus[i].state = FREE;
		system->frameStatus[i].lastAccessTime = 0;	}	system->taskList.nTasks = 0;	system->taskList.first = NULL;	return 0;}/** * start: Process a list of memory requests * @system: system * @mReqs: list of memory requests * @outFile: output file **/static int start(struct System *system, struct MemRequestList *mReqs,                     FILE *outFile){	int i = 0;	struct MemRequest* req;	printf("Starting simulation ... \n");	for (i = 0; i < mReqs->count; i++) {		req = &(mReqs->elems[i]);
		printf("request: %d\n", i);		switch (req->cmd) {			case START:				printf("Do allocate a space %d for pid %d\n", req->mem, req->pid);				allocFrames4Task(system, req->pid, req->mem, outFile);				break;			case ACCESS:				printf("Access mem %d of pid %d\n", req->mem, req->pid);				access2Mem(system, req->pid, req->mem, outFile);				break;			case FINISH:				printf("Pid %d is finishing \n", req->pid);				finishTask(system, req->pid, outFile);				break;			default:				break;		}	}	return 0;}/** * initOptions: initialize default input, output * @opts:options **/static int initOptions(struct Options *opts){	sprintf(opts->taskFile, "tasks.txt");	sprintf(opts->sysFile, "sys.conf");	sprintf(opts->outFile, "output.txt");	return 0;}/** * Main function **/int main(int argc, char **argv){	struct Options opts;	struct MemRequestList mReqs;	struct System system;	FILE *outFile;	/* Set default input, output */	initOptions(&opts);	/* Parse arguments */	parseArguments(argc, argv, &opts);	/* Create list of memory requests from file */	createMemRequestList(opts.taskFile, &mReqs);	/* Setup some hardware-related values from file */	configSystem(opts.sysFile, &system);	/* Simulate memory management - Virtual memory */	outFile = fopen(opts.outFile, "w");	start(&system, &mReqs, outFile);	fclose(outFile);	return 0;}

⌨️ 快捷键说明

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