Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
thangtq139 committed Dec 10, 2017
2 parents 434990b + 722dd27 commit 2290192
Show file tree
Hide file tree
Showing 17 changed files with 430 additions and 28 deletions.
2 changes: 2 additions & 0 deletions src/nachos-3.4/code/Hello.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Hello, is it me you looking for?
I can see it in your eyes...
2 changes: 1 addition & 1 deletion src/nachos-3.4/code/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ all:
# don't delete executables in "test" in case there is no cross-compiler
clean:
/bin/csh -c "rm -f *~ */{core,nachos,DISK,*.o,swtch.s,*~} test/{*.coff} bin/{coff2flat,coff2noff,disassemble,out}"
/bin/csh -c "rm test/halt test/matmult test/printchar test/printstring test/readchar test/readstring test/shell test/sort test/ascii test/help"
/bin/csh -c "rm test/halt test/matmult test/printchar test/printstring test/readchar test/readstring test/shell test/sort test/ascii test/help test/cat test/copy test/echo test/createfile test/reverse"

print:
/bin/csh -c "$(LPR) Makefile* */Makefile"
Expand Down
9 changes: 9 additions & 0 deletions src/nachos-3.4/code/filesys/filesys.cc
Original file line number Diff line number Diff line change
Expand Up @@ -339,3 +339,12 @@ FileSystem::Print()
delete freeMap;
delete directory;
}

int
FileSystem::fopen(char *file_name, int type) {}

int
FileSystem::fclose(int fid) {}

OpenFile*
FileSystem::getOpenFile(int fid) {}
57 changes: 56 additions & 1 deletion src/nachos-3.4/code/filesys/filesys.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,64 @@

#include "copyright.h"
#include "openfile.h"
#include "table.h"

#ifdef FILESYS_STUB // Temporarily implement file system calls as
// calls to UNIX, until the real file system
// implementation is available
class FileSystem {
private:
Table *gFileTable;
public:
FileSystem(bool format) {}
FileSystem(bool format) {
gFileTable = new Table(10);
gFileTable->Set((void *)this, 0);
gFileTable->Set((void *)this, 1);
}

~FileSystem() {
delete gFileTable;
}

int fopen(char *name, int type) {
if (type != 0 && type != 1)
return -1;

int fileDescriptor = OpenForReadWrite(name, FALSE);
if (fileDescriptor == -1)
return -1;

OpenFile *f = new OpenFile(fileDescriptor, type);
if (f == NULL)
return -1;

int fid = gFileTable->Alloc(f);
if (fid == -1) {
delete f;
return -1;
}

return fid;
}

int fclose(int fid) {
if (fid < 2)
return -1;
OpenFile *f = (OpenFile *)gFileTable->Get(fid);
if (f == NULL)
return -1;

delete f;
gFileTable->Release(fid);
return 0;
}

OpenFile *getOpenFile(int fid) {
if (fid < 2)
return NULL;
OpenFile *f = (OpenFile *)gFileTable->Get(fid);
return f;
}

bool Create(char *name, int initialSize) {
int fileDescriptor = OpenForWrite(name);
Expand Down Expand Up @@ -85,11 +136,15 @@ class FileSystem {

void Print(); // List all the files and their contents

int fopen(char *name, int type);
int fclose(int fid);
OpenFile *getOpenFile(int fid);
private:
OpenFile* freeMapFile; // Bit map of free disk blocks,
// represented as a file
OpenFile* directoryFile; // "Root" directory -- list of
// file names, represented as a file
Table *gFileTable;
};

#endif // FILESYS
Expand Down
2 changes: 1 addition & 1 deletion src/nachos-3.4/code/filesys/openfile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ OpenFile::~OpenFile()
// "position" -- the location within the file for the next Read/Write
//----------------------------------------------------------------------

void
int
OpenFile::Seek(int position)
{
seekPosition = position;
Expand Down
67 changes: 50 additions & 17 deletions src/nachos-3.4/code/filesys/openfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,34 +28,67 @@
// See definitions listed under #else
class OpenFile {
public:
OpenFile(int f) { file = f; currentOffset = 0; } // open the file
OpenFile(int f, int t) {
file = f;
currentOffset = 0;
type = t;
} // open the file

OpenFile(int f) {
file = f;
currentOffset = 0;
}
~OpenFile() { Close(file); } // close the file

int ReadAt(char *into, int numBytes, int position) {
Lseek(file, position, 0);
return ReadPartial(file, into, numBytes);
}
Lseek(file, position, 0);
return ReadPartial(file, into, numBytes);
}
int WriteAt(char *from, int numBytes, int position) {
Lseek(file, position, 0);
WriteFile(file, from, numBytes);
return numBytes;
}
if (type == 1)
return -1;
Lseek(file, position, 0);
WriteFile(file, from, numBytes);
return numBytes;
}
int Read(char *into, int numBytes) {
int numRead = ReadAt(into, numBytes, currentOffset);
currentOffset += numRead;
return numRead;
}
int numRead = ReadAt(into, numBytes, currentOffset);
currentOffset += numRead;
return numRead;
}
int Write(char *from, int numBytes) {
int numWritten = WriteAt(from, numBytes, currentOffset);
currentOffset += numWritten;
return numWritten;
}
if (type == 1)
return -1;
int numWritten = WriteAt(from, numBytes, currentOffset);
currentOffset += numWritten;
return numWritten;
}

int Seek(int position) {
if (position == -1) {
currentOffset = Length();
return currentOffset;
}
if (position < 0) {
return -1;
}
if (type == 1) {
if (position > Length())
currentOffset = Length();
else
currentOffset = position;
} else {
currentOffset = position;
}
return currentOffset;
}

int Length() { Lseek(file, 0, 2); return Tell(file); }

private:
int file;
int currentOffset;
int type;
};

#else // FILESYS
Expand All @@ -67,7 +100,7 @@ class OpenFile {
// at "sector" on the disk
~OpenFile(); // Close the file

void Seek(int position); // Set the position from which to
int Seek(int position); // Set the position from which to
// start reading/writing -- UNIX lseek

int Read(char *into, int numBytes); // Read/write bytes from the file,
Expand Down
2 changes: 1 addition & 1 deletion src/nachos-3.4/code/machine/console.cc
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ Console::CheckCharAvail()

// otherwise, read character and tell user about it
Read(readFileNo, &c, sizeof(char));
DEBUG('u', "New character await\n");
// DEBUG('u', "New character await\n");
incoming = c ;
stats->numConsoleCharsRead++;
(*readHandler)(handlerArg);
Expand Down
40 changes: 39 additions & 1 deletion src/nachos-3.4/code/test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,21 @@ CPP = gcc -E
INCDIR =-I../userprog -I../threads
CFLAGS = -G 0 -c $(INCDIR)

all: halt shell matmult sort ascii printchar readchar printstring readstring help createfile
all: halt \
shell \
matmult \
sort \
ascii \
printchar \
readchar \
printstring \
readstring \
help \
createfile \
cat \
echo \
copy \
reverse

start.o: start.s ../userprog/syscall.h
$(CPP) $(CPPFLAGS) start.c > strt.s
Expand Down Expand Up @@ -109,3 +123,27 @@ createfile.o: createfile.c
createfile: createfile.o start.o
$(LD) $(LDFLAGS) start.o createfile.o -o createfile.coff
../bin/coff2noff createfile.coff createfile

cat.o: cat.c
$(CC) $(CFLAGS) -c cat.c
cat: cat.o start.o
$(LD) $(LDFLAGS) start.o cat.o -o cat.coff
../bin/coff2noff cat.coff cat

echo.o: echo.c
$(CC) $(CFLAGS) -c echo.c
echo: echo.o start.o
$(LD) $(LDFLAGS) start.o echo.o -o echo.coff
../bin/coff2noff echo.coff echo

copy.o: copy.c
$(CC) $(CFLAGS) -c copy.c
copy: copy.o start.o
$(LD) $(LDFLAGS) start.o copy.o -o copy.coff
../bin/coff2noff copy.coff copy

reverse.o: reverse.c
$(CC) $(CFLAGS) -c reverse.c
reverse: reverse.o start.o
$(LD) $(LDFLAGS) start.o reverse.o -o reverse.coff
../bin/coff2noff reverse.coff reverse
26 changes: 26 additions & 0 deletions src/nachos-3.4/code/test/cat.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include "syscall.h"

#define MAX_LEN 255
#define BUF_LEN 255

char file_name[MAX_LEN + 1];
char buf[BUF_LEN + 1];

int main()
{
int charRead, charWritten;
int fid;
PrintString("Nhap ten file: ");
ReadString(file_name, MAX_LEN);

fid = Open(file_name, 1);
while ((charRead = Read(buf, BUF_LEN, fid)) > 0) {
charWritten = Write(buf, charRead, 1);
if (charRead != charWritten) {
PrintString("Loi khi hien thi file.\n");
Halt();
}
}
Close(fid);
Halt();
}
47 changes: 47 additions & 0 deletions src/nachos-3.4/code/test/copy.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include "syscall.h"

#define MAX_LEN 256

char s_name[MAX_LEN + 1], d_name[MAX_LEN + 1];
char buf[MAX_LEN + 1];

int main()
{
int s_fid, d_fid;
int charCount, charWritten;
PrintString("Nhap file nguon: ");
ReadString(s_name, MAX_LEN);
PrintString("Nhap file dich: ");
ReadString(d_name, MAX_LEN);

s_fid = Open(s_name, 1);
if (s_fid == -1) {
PrintString("Loi khi mo file nguon.\n");
Halt();
}

if (CreateFile(d_name) == -1) {
PrintString("Loi khi tao file dich.\n");
Halt();
}

d_fid = Open(d_name, 0);
if (d_fid == -1) {
PrintString("Loi khi mo file dich.\n");
Halt();
}

while ((charCount = Read(buf, MAX_LEN, s_fid)) > 0) {
charWritten = Write(buf, charCount, d_fid);
if (charCount != charWritten) {
PrintString("Loi trong qua trinh copy file.\n");
Halt();
}
}

if (charCount == -1)
PrintString("Loi trong qua trinh doc file.\n");
else
PrintString("Copy file thanh cong.\n");
Halt();
}
19 changes: 19 additions & 0 deletions src/nachos-3.4/code/test/echo.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "syscall.h"

#define BUF_LEN 255

char buf[BUF_LEN + 1];

int main()
{
int charCount = 0, charWritten;
while ((charCount = Read(buf, BUF_LEN, 0)) > 0) {
buf[charCount++] = '\n';
charWritten = Write(buf, charCount, 1);
if (charWritten != charCount) {
PrintString("Error when printing to console.\n");
break;
}
}
Halt();
}
Loading

0 comments on commit 2290192

Please sign in to comment.