fstest.cc
来自「操作系统课程设计。在UNIX平台下实现Solary操作系统的一些功能」· CC 代码 · 共 341 行
CC
341 行
///////////////////////////////////////////////////////////////FileName : fstest.cc////Creator : Fang Wenbin(0410706)//CreateTime : 2006-12-26////File Desc:// 1.implement the file sysmtem's system calls// 2.implement some test functions/////////////////////////////////////////////////////////////#include "copyright.h"#include "utility.h"#include "filesys.h"#include "system.h"#include "thread.h"#include "disk.h"#include "stats.h"#include "filehdr.h"#define TransferSize 10 //////////////////////////////////////////////////////// Function name : RenameFile// Creator : Fang Wenbin(0410706)// CreateTime : 2007-1-9 21:21:35//// Function Desc : system call -- rename a file// The steps of RenameFile// 1. call FileSystem::s_RenameFile//// Fails if:// // Note :// Return type : void // Argument : char *src// Argument : char *des//////////////////////////////////////////////////////void RenameFile(char *src, char *des){ fileSystem->s_RenameFile(src, des);}//////////////////////////////////////////////////////// Function name : RemoveFile// Creator : Fang Wenbin(0410706)// CreateTime : 2007-1-9 21:22:07//// Function Desc : system call -- remove a file// The steps of RemoveFile// 1. call FileSystem::s_RemoveFile//// Fails if:// // Note :// Return type : void // Argument : char *src//////////////////////////////////////////////////////void RemoveFile(char *src){ fileSystem->s_RemoveFile(src);}//////////////////////////////////////////////////////// Function name : CopyFile// Creator : Fang Wenbin(0410706)// CreateTime : 2007-1-9 21:22:07//// Function Desc : system call -- copy a file// The steps of copyFile// 1. call FileSystem::s_CopyFile//// Fails if:// // Note :// Return type : void // Argument : char *src// Argument : char *des//////////////////////////////////////////////////////void CopyFile(char *src, char *des){ fileSystem->s_CopyFile(src, des);}void Copy(char *from, char *to){ FILE *fp; OpenFile* openFile; int amountRead, fileLength; char *buffer; if ((fp = fopen(from, "r")) == NULL) { printf("Copy: couldn't open input file %s\n", from); return; } fseek(fp, 0, 2); fileLength = ftell(fp); fseek(fp, 0, 0); fileSystem->s_CreateFile(to); buffer = new char[fileLength]; openFile = new OpenFile(to); while ((amountRead = fread(buffer, sizeof(char), fileLength, fp)) > 0) openFile->Write(buffer, fileLength); delete []buffer; fclose(fp);}//////////////////////////////////////////////////////// Function name : PrintFile// Creator : Fang Wenbin(0410706)// CreateTime : 2007-1-9 21:39:14//// Function Desc : // The steps of PrintFile// 1.read data from file// 2.print the data// // Fails if:// 1. file size equals zero.// 2. file type is dir//// Note :// Return type : void // Argument : char *name//////////////////////////////////////////////////////void PrintFile(char *name){ int len = fileSystem->s_GetFileLength(name); if (len == 0) { printf("File %s has no contents!\n", name); return; } if (fileSystem->s_GetFileType(name) & s_DIR_TYPE) { printf("Dir can't be printed!"); return; } char *data = new char[len]; fileSystem->s_ReadFile(name, len, data); data[len] = '\0'; printf("contains of file %s:\n%s\n", name, data); delete data;}#define s_TEST_FILE "/test.txt" //Test the synch file accessstaticvoid ReadThread(int which){ for (int i = 0; i < 5; i++) { int len = fileSystem->s_GetFileLength(s_TEST_FILE); char *data = new char[len]; fileSystem->s_ReadFile(s_TEST_FILE, len, data); data[len] = '\0'; printf("thread %d is reading file, which contains:\n%s\n", which, data); delete data; currentThread->Yield(); }}static void WriteThread(int which){ for (int i = 0; i < 5; i++) { printf("thread %d is writing file:\n", which); fileSystem->s_WriteFile(s_TEST_FILE, 1, "1"); currentThread->Yield(); }}//////////////////////////////////////////////////////// Function name : MultiThreadsTest// Creator : Fang Wenbin(0410706)// CreateTime : 2007-1-9 21:39:50//// Function Desc : Test of task 1// multithreads access disk// The steps of MultiThreadsTest// 1.write data into a tmp file// 2.create 3 threads to read the file//// Fails if://// // Note :// Return type : void //////////////////////////////////////////////////////
void MultiThreadsTest(){ fileSystem->s_CreateFile(s_TEST_FILE); fileSystem->s_WriteFile(s_TEST_FILE, 5, "hello"); Thread *tr1 = new Thread("Read thread1"); tr1->Fork(ReadThread, 1); Thread *tr2 = new Thread("Read thread2"); tr2->Fork(ReadThread, 2); Thread *tr3 = new Thread("Read thread3"); tr3->Fork(ReadThread, 3);}#define FileName "/TestFile"#define Contents '1'#define ContentSize strlen(Contents) #define FileSize ((int)(ContentSize * 5000))#define s_MAX_DIRECT_SIZE 3*128#define s_MAX_FIRST_SIZE (3+32)*128#define s_MAX_SECOND_SIZE (3+32+32*32)*128 #define s_MAX_THIRD_SIZE 30*30+30*30*30*64 static void FileDirectWrite(){ fileSystem->s_CreateFile(FileName); char data[s_MAX_DIRECT_SIZE]; for (int i = 0; i < s_MAX_DIRECT_SIZE; i++) data[i] = Contents; fileSystem->s_WriteFile(FileName, s_MAX_DIRECT_SIZE, data); }static void FileFirstWrite(){ fileSystem->s_CreateFile(FileName); char data[s_MAX_FIRST_SIZE]; for (int i = 0; i < s_MAX_FIRST_SIZE; i++) data[i] = Contents; fileSystem->s_WriteFile(FileName, s_MAX_FIRST_SIZE, data); }static void FileSecondWrite(){ fileSystem->s_CreateFile(FileName); char data[s_MAX_SECOND_SIZE]; for (int i = 0; i < s_MAX_SECOND_SIZE; i++) data[i] = Contents; fileSystem->s_WriteFile(FileName, s_MAX_SECOND_SIZE, data); }static void FileThirdWrite(){ fileSystem->s_CreateFile(FileName); char data[s_MAX_THIRD_SIZE]; for (int i = 0; i < s_MAX_THIRD_SIZE; i++) data[i] = Contents; fileSystem->s_WriteFile(FileName, s_MAX_THIRD_SIZE, data); }static void FileRead(){ int len = fileSystem->s_GetFileLength(FileName); char *data = new char[len]; fileSystem->s_ReadFile(FileName, len, data); data[len] = '\0'; printf(data); delete data;}//////////////////////////////////////////////////////// Function name : PerformanceTest// Creator : Fang Wenbin(0410706)// CreateTime : 2007-1-9 21:42:46//// Function Desc : large file write and read test// The steps of PerformanceTest// ////// Fails if:// // Note :// Return type : void // Argument : int layer -- // 0 -- denotes direct index// 1 -- denotes first indirect index// 2 -- denotes second indirect index// 3 -- denotes third indirect index//////////////////////////////////////////////////////void PerformanceTest(int layer){ switch (layer) { case 0: FileDirectWrite(); puts("Writing data in direct index..."); puts("Reading data in direct index..."); FileRead(); printf("\nFile Size: %d bytes", s_MAX_DIRECT_SIZE); break; case 1: FileFirstWrite(); puts("Writing data in first index..."); puts("Reading data in first index..."); FileRead(); printf("\nFile Size: %d bytes", s_MAX_FIRST_SIZE); break; case 2: FileSecondWrite(); puts("Writing data in second index..."); puts("Reading data in second index..."); FileRead(); printf("\nFile Size: %d bytes", s_MAX_SECOND_SIZE); break; case 3: FileThirdWrite(); puts("Writing data in third index..."); puts("Reading data in third index..."); FileRead(); printf("\nFile Size: %d bytes", s_MAX_THIRD_SIZE); break; } puts("\nFinished!");}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?