Skip to content

Commit

Permalink
Add file DA3
Browse files Browse the repository at this point in the history
  • Loading branch information
thangtq139 committed Dec 30, 2017
1 parent e0537ae commit 621332c
Show file tree
Hide file tree
Showing 8 changed files with 453 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/nachos-3.4/code/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
3
4 2 1
5 3 4 1
8 5 2
Empty file added src/nachos-3.4/code/output.txt
Empty file.
1 change: 1 addition & 0 deletions src/nachos-3.4/code/sinhvien_inp.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
8 5 2
103 changes: 103 additions & 0 deletions src/nachos-3.4/code/test/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#include "syscall.h"

int read_unsigned_integer(OpenFileId fid, int *n)
{
char c;
int ret;
*n = 0;
do {
ret = Read(&c, 1, fid);
} while (ret == 1 && (c < '0' || c > '9'));

while (ret == 1 && c >= '0' && c <= '9') {
*n = (*n) * 10 + (c - '0');
ret = Read(&c, 1, fid);
}

if (ret <= 0)
return -2;
if (c == '\n')
return -1;
return 0;
}

int main()
{
OpenFileId f_in, f_out, f_sv, f_tmp;
SpaceId job_sv, job_vn;
int n, ret;
char c;
if (CreateFile("output.txt") != 0) {
PrintString("Cannot create output file\n");
Exit(-1);
}
if (CreateSemaphore("lock_file", 1) == -1) {
PrintString("Cannot create semaphore lock_file\n");
Exit(-1);
}
f_in = Open("input.txt", 1);
if (f_in == -1) {
PrintString("Fail to open input\n");
Exit(-1);
}
f_out = Open("output.txt", 0);
if (f_out == -1) {
PrintString("Fail to open output\n");
Exit(-1);
}
read_unsigned_integer(f_in, &n);
while (n--) {
if (CreateFile("sinhvien_inp.txt") != 0) {
PrintString("Cannot create input for sinhvien\n");
Exit(-1);
}
f_sv = Open("sinhvien_inp.txt", 0);
if (f_sv == -1) {
PrintString("Fail to open input for sinhvien\n");
Exit(-1);
}
if (CreateFile("voinuoc_input.txt") != 0) {
PrintString("Cannout create input for voinuoc\n");
Exit(-1);
}
do {
ret = Read(&c, 1, f_in);
if (ret == 1)
Write(&c, 1, f_sv);
} while (ret == 1 && c != '\n');
Close(f_sv);

job_sv = Exec("test/sinhvien");
if (job_sv == -1) {
PrintString("Fail to exec sinhvien\n");
Exit(-1);
}
job_vn = Exec("test/voinuoc");
if (job_vn == -1) {
PrintString("Fail to exec voinuoc\n");
Exit(-1);
}
if (Join(job_sv) == -1) {
PrintString("sinhvien execute uncompletely\n");
Exit(-1);
}
if (Join(job_vn) == -1) {
PrintString("voinuoc execute uncompletely\n");
Exit(-1);
}
f_tmp = Open("tmp.txt", 1);
if (f_tmp == -1) {
PrintString("Fail to open tmp file to read.\n");
Exit(-1);
}
do {
ret = Read(&c, 1, f_tmp);
if (ret == 1)
Write(&c, 1, f_out);
} while (ret == 1);
Close(f_tmp);
}
Close(f_in);
Close(f_out);
Exit(0);
}
59 changes: 59 additions & 0 deletions src/nachos-3.4/code/test/sinhvien.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include "syscall.h"

int read_unsigned_integer(OpenFileId fid, int *n)
{
char c;
int ret;
*n = 0;
do {
ret = Read(&c, 1, fid);
} while (ret == 1 && (c < '0' || c > '9'));

while (ret == 1 && c >= '0' && c <= '9') {
*n = (*n) * 10 + (c - '0');
ret = Read(&c, 1, fid);
}

if (ret <= 0)
return -2;
if (c == '\n')
return -1;
return 0;
}


int main()
{
OpenFileId f_sv, f_vn;
char c;
int ret;
f_sv = Open("sinhvien_inp.txt", 1);
if (f_sv == -1)
Exit(-1);
f_vn = Open("voinuoc_input.txt", 0);
if (f_vn == -1)
Exit(-1);

do {
ret = Read(&c, 1, f_sv);
if (ret == 1 && c >= '0' && c <= '9') {
Wait("lock_file");
do {
Write(&c, 1, f_vn);
ret = Read(&c, 1, f_sv);
} while (ret == 1 && c >= '0' && c <= '9');
Write(" ", 1, f_vn);
Signal("lock_file");
}
} while (ret == 1);
Wait("lock_file");
// if (CreateFile("voinuoc_input.txt") != 0) {
// Signal("lock_file");
// Exit(-1);
// }
Write("-\n", 2, f_vn);
Signal("lock_file");

Close(f_vn);
Exit(0);
}
52 changes: 52 additions & 0 deletions src/nachos-3.4/code/test/voinuoc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include "syscall.h"

int read_unsigned_integer(OpenFileId fid)
{
char c;
int ret, res = 0;
do {
ret = Read(&c, 1, fid);
if (ret == 1 && c == '-')
return -1;
} while (ret == 1 && (c < '0' || c > '9'));

while (ret == 1 && c >= '0' && c <= '9') {
res = res * 10 + (c - '0');
ret = Read(&c, 1, fid);
}
return res;
}

int main()
{
OpenFileId f_vn, f_tmp;
int s_1, s_2, n;
f_vn = Open("voinuoc_input.txt", 0);
if (f_vn == -1)
Exit(-1);
if (CreateFile("tmp.txt") != 0)
Exit(-1);
f_tmp = Open("tmp.txt", 0);
if (f_tmp == -1)
Exit(-1);
s_1 = s_2 = 0;
while (1) {
Wait("lock_file");
n = read_unsigned_integer(f_vn);
if (n == -1) {
Signal("lock_file");
break;
}
Signal("lock_file");
if (s_1 <= s_2) {
s_1 += n;
Write("1 ", 2, f_tmp);
} else {
s_2 += n;
Write("2 ", 2, f_tmp);
}
}
Write("\n", 1, f_tmp);
Close(f_vn);
Close(f_tmp);
}
Loading

0 comments on commit 621332c

Please sign in to comment.