From c1803336d1f1db722f76856d8395beef6b2688b0 Mon Sep 17 00:00:00 2001 From: Johannes Vollmer Date: Sat, 10 Oct 2015 14:02:49 +0200 Subject: [PATCH 01/10] edited AUTHORS --- AUTHORS | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/AUTHORS b/AUTHORS index f8c27e6..b024818 100644 --- a/AUTHORS +++ b/AUTHORS @@ -1,5 +1,2 @@ -Martin Aigner -Christian Barthel -Christoph Kirsch -Michael Lippautz -Simone Oblasser \ No newline at end of file +Tobias Seiler +Johannes Vollmer \ No newline at end of file From 626aa192e7a861b9a47e9d56f55435b7726275c5 Mon Sep 17 00:00:00 2001 From: Johannes Vollmer Date: Thu, 15 Oct 2015 19:31:49 +0200 Subject: [PATCH 02/10] fulfilled assignment0: added a linked list --- selfie.c | 201 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 201 insertions(+) diff --git a/selfie.c b/selfie.c index c2b5d87..e9e0d56 100755 --- a/selfie.c +++ b/selfie.c @@ -4104,6 +4104,207 @@ int* copyC2CStarArguments(int argc, int *argv) { return cstar_argv; } +///////////////////////////////////////////////////////////////////// +//////------Assignment 0: Implementation of a Linked List------////// +///////////////////////////////////////////////////////////////////// + +// Initialization of the linked list (Creating the header with data -1) +// The header cannot be deleted!! +int* initializeList(int *list) { + int *header; + int data = -1; // no data inserted + + header = malloc(4*2); + *header = 0; + *(header+1) = data; + return header; +} + +// Add a node to the linked list at the bottom +int* addToList(int *list, int data) { + + int *newNode; + newNode = malloc(4*2); + *newNode = list; + *(newNode+1) = data; + return newNode; +} + +// Iterate through the linked list and get the nth node of the list +int* getNodeFromList(int* list, int nthNode) { + + while(nthNode > 0) { + list = *list; + nthNode = nthNode - 1; + } + + return list; +} + +// Iterate through the linked list and get the data of the nth node of the list +int getDataFromList(int* list, int nthNode) { + + while(nthNode > 0) { + list = *list; + nthNode = nthNode - 1; + } + + return *(list+1); +} + +// Iterate through the linked list and set the data of the nth node of the list +int alterDataInList(int* list, int nthNode, int newValue) { + + while(nthNode > 0) { + list = *list; + nthNode = nthNode - 1; + } + + *(list+1) = newValue; + + return *(list+1); +} + + +// Get the size of the linked list +int sizeOfList(int* list) { + + int *count; + count = malloc(4); + *count = 0; + + while(*list != 0) { + list = *list; + *count = *count + 1; + } + return *count; +} + +// Delete a node from the linked list from the top +int* deleteFirstNodeFromList(int* list) { + + int *prev; + int *next; + prev = malloc(2*4); + next = malloc(2*4); + *prev = 0; + *next = 0; + int size = sizeOfList(list); + + prev = getNodeFromList(list, size-2); + next = getNodeFromList(list, size); + *prev = next; + + return list; +} + +// Sort the list with Bubble Sort +int* sortList(int* list) { + + int size = sizeOfList(list); + while(size > 1){ + int i = 0; + while(i= 0) { + int number = getDataFromList(list, *counter); + int *Buffer = (int*)malloc(4*10); + print(itoa( number, Buffer, 10, 0)); + putchar(CHAR_LF); + *counter = *counter - 1; + } + +} + +int testList() { + + int *list; + // Create new linked list (FIFO Linked List): + // top -> [9,7,8,2,4,1,5,3] -> bottom + printString('I','n','s','e','r','t',' ','i','n','t','o',' ','l','i','s','t',CHAR_LF,0,0,0); + list = initializeList(list); + list = addToList(list, 9); + list = addToList(list, 7); + list = addToList(list, 8); + list = addToList(list, 2); + list = addToList(list, 4); + list = addToList(list, 1); + list = addToList(list, 5); + list = addToList(list, 3); + printList(list); + + // Delete the first node (FIFO Linked List): + // top -> [2,4,1,5,3] -> bottom + printString('D','e','l','e','t','e',' ','f','i','r','s','t',' ','n','o','d','e',CHAR_LF,0,0); + list = deleteFirstNodeFromList(list); + list = deleteFirstNodeFromList(list); + list = deleteFirstNodeFromList(list); + printList(list); + + // Sorting the list + // top -> [5,4,3,2,1] -> bottom + printString('S','o','r','t','i','n','g',' ','t','h','e',' ','l','i','s','t',CHAR_LF,0,0,0); + list = sortList(list); + printList(list); + + exit(0); +} + +int main(int argc, int *argv) { + int *cstar_argv; + int *firstParameter; + + initLibrary(); + + initRegister(); + initDecoder(); + initSyscalls(); + + cstar_argv = copyC2CStarArguments(argc, argv); + + if (argc > 1) { + firstParameter = (int*) (*(cstar_argv+1)); + + if (*firstParameter == '-') { + if (*(firstParameter+1) == 'c') + main_compiler(); + else if (*(firstParameter+1) == 'm') { + if (argc > 3) + main_emulator(argc, argv, cstar_argv); + else + exit(-1); + } + else { + exit(-1); + } + } else { + exit(-1); + } + } else + // default: compiler + //main_compiler(); + testList(); +} + int main(int argc, int *argv) { int *cstar_argv; int *firstParameter; From d19bb1d8524620fd8fea960dc4add6301debd5b6 Mon Sep 17 00:00:00 2001 From: Johannes Vollmer Date: Mon, 19 Oct 2015 13:55:48 +0200 Subject: [PATCH 03/10] - added description for assignment01 --- selfie.c | 66 ++++++++++++++++++++++++++------------------------------ 1 file changed, 31 insertions(+), 35 deletions(-) diff --git a/selfie.c b/selfie.c index e9e0d56..f289b1e 100755 --- a/selfie.c +++ b/selfie.c @@ -4105,9 +4105,21 @@ int* copyC2CStarArguments(int argc, int *argv) { } ///////////////////////////////////////////////////////////////////// -//////------Assignment 0: Implementation of a Linked List------////// +//////-----------Assignment 0: Basic data structures-----------////// ///////////////////////////////////////////////////////////////////// +//Review linked lists and implement a simple program using a singly linked list in C*. The minimal requirements are as follows: +// +//must be implemented in C* +//must compile with selfie +//must run on selfie +//the list must be dynamically allocated +//every node must be dynamically allocated +//inserting nodes to the list and removing nodes from the list +//list iteration +//Bonus: sort the list. Any way you like +//Deadline: Oct 15, end of day + // Initialization of the linked list (Creating the header with data -1) // The header cannot be deleted!! int* initializeList(int *list) { @@ -4269,41 +4281,24 @@ int testList() { exit(0); } -int main(int argc, int *argv) { - int *cstar_argv; - int *firstParameter; - - initLibrary(); - - initRegister(); - initDecoder(); - initSyscalls(); +///////////////////////////////////////////////////////////////////// +//////-Assignment 1: Loading, scheduling, switching, execution-////// +///////////////////////////////////////////////////////////////////// - cstar_argv = copyC2CStarArguments(argc, argv); +//Implement basic concurrent execution of n processes in mipster. n >= 2 +// +//understand how mipster interprets and executes binary instructions. Tipp: add your own comments to the code +//mipster maintains a local state for a process (running executable), e.g., pc, registers, memory +//understand the purpose of each variable and data structure +//duplicate the process state n times +//running mipster like: ./selfie -m 32 yourbinary should generate n instances of yourbinary in a single instance of mipster +//implement preemptive multitasking, i.e., switching between the n instances of yourbinary is determined by mipster +//switch processes every m instructions. 1 <= m <= number of instructions in yourbinary +//implement round-robin scheduling +//add some output in yourbinary to demonstrate context switching +//Deadline: Oct 22, end of day - if (argc > 1) { - firstParameter = (int*) (*(cstar_argv+1)); - if (*firstParameter == '-') { - if (*(firstParameter+1) == 'c') - main_compiler(); - else if (*(firstParameter+1) == 'm') { - if (argc > 3) - main_emulator(argc, argv, cstar_argv); - else - exit(-1); - } - else { - exit(-1); - } - } else { - exit(-1); - } - } else - // default: compiler - //main_compiler(); - testList(); -} int main(int argc, int *argv) { int *cstar_argv; @@ -4337,5 +4332,6 @@ int main(int argc, int *argv) { } } else // default: compiler - main_compiler(); -} + //main_compiler(); + testList(); +} \ No newline at end of file From 267106c11187f023702ee5914b81eebcf2d6bf3b Mon Sep 17 00:00:00 2001 From: Johannes Vollmer Date: Mon, 19 Oct 2015 13:57:37 +0200 Subject: [PATCH 04/10] - fixed AUTHORS --- AUTHORS | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/AUTHORS b/AUTHORS index b024818..7d19ede 100644 --- a/AUTHORS +++ b/AUTHORS @@ -1,2 +1,7 @@ +Martin Aigner +Christian Barthel +Christoph Kirsch +Michael Lippautz +Simone Oblasser Tobias Seiler Johannes Vollmer \ No newline at end of file From 487fd6bc43e6f8962656cd256e5e3bd7430c665a Mon Sep 17 00:00:00 2001 From: Johannes Vollmer Date: Tue, 20 Oct 2015 13:46:55 +0200 Subject: [PATCH 05/10] - added flag to start linked list --- selfie.c | 41 ++++++----------------------------------- 1 file changed, 6 insertions(+), 35 deletions(-) diff --git a/selfie.c b/selfie.c index f289b1e..8c5fe2e 100755 --- a/selfie.c +++ b/selfie.c @@ -4105,21 +4105,9 @@ int* copyC2CStarArguments(int argc, int *argv) { } ///////////////////////////////////////////////////////////////////// -//////-----------Assignment 0: Basic data structures-----------////// +//////------Assignment 0: Implementation of a Linked List------////// ///////////////////////////////////////////////////////////////////// -//Review linked lists and implement a simple program using a singly linked list in C*. The minimal requirements are as follows: -// -//must be implemented in C* -//must compile with selfie -//must run on selfie -//the list must be dynamically allocated -//every node must be dynamically allocated -//inserting nodes to the list and removing nodes from the list -//list iteration -//Bonus: sort the list. Any way you like -//Deadline: Oct 15, end of day - // Initialization of the linked list (Creating the header with data -1) // The header cannot be deleted!! int* initializeList(int *list) { @@ -4281,25 +4269,6 @@ int testList() { exit(0); } -///////////////////////////////////////////////////////////////////// -//////-Assignment 1: Loading, scheduling, switching, execution-////// -///////////////////////////////////////////////////////////////////// - -//Implement basic concurrent execution of n processes in mipster. n >= 2 -// -//understand how mipster interprets and executes binary instructions. Tipp: add your own comments to the code -//mipster maintains a local state for a process (running executable), e.g., pc, registers, memory -//understand the purpose of each variable and data structure -//duplicate the process state n times -//running mipster like: ./selfie -m 32 yourbinary should generate n instances of yourbinary in a single instance of mipster -//implement preemptive multitasking, i.e., switching between the n instances of yourbinary is determined by mipster -//switch processes every m instructions. 1 <= m <= number of instructions in yourbinary -//implement round-robin scheduling -//add some output in yourbinary to demonstrate context switching -//Deadline: Oct 22, end of day - - - int main(int argc, int *argv) { int *cstar_argv; int *firstParameter; @@ -4324,6 +4293,9 @@ int main(int argc, int *argv) { else exit(-1); } + else if (*(firstParameter+1) == 'l') { + testList(); + } else { exit(-1); } @@ -4332,6 +4304,5 @@ int main(int argc, int *argv) { } } else // default: compiler - //main_compiler(); - testList(); -} \ No newline at end of file + main_compiler(); +} From 6915a691bb99fa068e62a1f72cb748c983eb13a7 Mon Sep 17 00:00:00 2001 From: Johannes Vollmer Date: Tue, 20 Oct 2015 13:56:30 +0200 Subject: [PATCH 06/10] - fixed allocation of "count" --- selfie.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/selfie.c b/selfie.c index 8c5fe2e..8691b2c 100755 --- a/selfie.c +++ b/selfie.c @@ -4169,15 +4169,14 @@ int alterDataInList(int* list, int nthNode, int newValue) { // Get the size of the linked list int sizeOfList(int* list) { - int *count; - count = malloc(4); - *count = 0; + int count; + count = 0; while(*list != 0) { list = *list; - *count = *count + 1; + count = count + 1; } - return *count; + return count; } // Delete a node from the linked list from the top @@ -4294,7 +4293,7 @@ int main(int argc, int *argv) { exit(-1); } else if (*(firstParameter+1) == 'l') { - testList(); + testList(); } else { exit(-1); From 8f1af44fde9495d4b84cd7abcd1cc43a69c1a426 Mon Sep 17 00:00:00 2001 From: Johannes Vollmer Date: Tue, 20 Oct 2015 14:22:43 +0200 Subject: [PATCH 07/10] - fixed duplicate of getNodeFromList and getDataFromList --- selfie.c | 37 +++++++++++++++---------------------- 1 file changed, 15 insertions(+), 22 deletions(-) diff --git a/selfie.c b/selfie.c index 8691b2c..1b4410b 100755 --- a/selfie.c +++ b/selfie.c @@ -4131,7 +4131,7 @@ int* addToList(int *list, int data) { } // Iterate through the linked list and get the nth node of the list -int* getNodeFromList(int* list, int nthNode) { +int getNodeFromList(int *list, int nthNode) { while(nthNode > 0) { list = *list; @@ -4141,19 +4141,8 @@ int* getNodeFromList(int* list, int nthNode) { return list; } -// Iterate through the linked list and get the data of the nth node of the list -int getDataFromList(int* list, int nthNode) { - - while(nthNode > 0) { - list = *list; - nthNode = nthNode - 1; - } - - return *(list+1); -} - // Iterate through the linked list and set the data of the nth node of the list -int alterDataInList(int* list, int nthNode, int newValue) { +int alterDataInList(int *list, int nthNode, int newValue) { while(nthNode > 0) { list = *list; @@ -4167,7 +4156,7 @@ int alterDataInList(int* list, int nthNode, int newValue) { // Get the size of the linked list -int sizeOfList(int* list) { +int sizeOfList(int *list) { int count; count = 0; @@ -4180,7 +4169,7 @@ int sizeOfList(int* list) { } // Delete a node from the linked list from the top -int* deleteFirstNodeFromList(int* list) { +int* deleteFirstNodeFromList(int *list) { int *prev; int *next; @@ -4198,15 +4187,18 @@ int* deleteFirstNodeFromList(int* list) { } // Sort the list with Bubble Sort -int* sortList(int* list) { +int* sortList(int *list) { int size = sizeOfList(list); while(size > 1){ int i = 0; - while(i= 0) { - int number = getDataFromList(list, *counter); + int *nodeNumber = getNodeFromList(list, *counter); + int number = *(nodeNumber+1); int *Buffer = (int*)malloc(4*10); print(itoa( number, Buffer, 10, 0)); putchar(CHAR_LF); From 2dce0f09b56f63ec3c8feab72f4c1c319ea96ba5 Mon Sep 17 00:00:00 2001 From: Johannes Vollmer Date: Tue, 20 Oct 2015 14:37:47 +0200 Subject: [PATCH 08/10] - added comments --- selfie.c | 46 +++++++++++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 19 deletions(-) diff --git a/selfie.c b/selfie.c index 1b4410b..76a0530 100755 --- a/selfie.c +++ b/selfie.c @@ -4104,9 +4104,18 @@ int* copyC2CStarArguments(int argc, int *argv) { return cstar_argv; } -///////////////////////////////////////////////////////////////////// -//////------Assignment 0: Implementation of a Linked List------////// -///////////////////////////////////////////////////////////////////// +//// Assignment 0: Basic data structures //// +//Review linked lists and implement a simple program using a singly linked list in C*. The minimal requirements are as follows: +// +//must be implemented in C* +//must compile with selfie +//must run on selfie +//the list must be dynamically allocated +//every node must be dynamically allocated +//inserting nodes to the list and removing nodes from the list +//list iteration +//Bonus: sort the list. Any way you like +//Deadline: Oct 15, end of day // Initialization of the linked list (Creating the header with data -1) // The header cannot be deleted!! @@ -4141,20 +4150,6 @@ int getNodeFromList(int *list, int nthNode) { return list; } -// Iterate through the linked list and set the data of the nth node of the list -int alterDataInList(int *list, int nthNode, int newValue) { - - while(nthNode > 0) { - list = *list; - nthNode = nthNode - 1; - } - - *(list+1) = newValue; - - return *(list+1); -} - - // Get the size of the linked list int sizeOfList(int *list) { @@ -4199,8 +4194,8 @@ int* sortList(int *list) { int y = *(listitemY+1); if(x < y) { - alterDataInList(list, i, y); - alterDataInList(list, i+1, x); + *(listitemX + 1) = y; + *(listitemY + 1) = x; } i = i + 1; } @@ -4261,6 +4256,19 @@ int testList() { exit(0); } +//// Assignment 1: Loading, scheduling, switching, execution //// +//Implement basic concurrent execution of n processes in mipster. n >= 2 +//understand how mipster interprets and executes binary instructions. Tipp: add your own comments to the code +//mipster maintains a local state for a process (running executable), e.g., pc, registers, memory +//understand the purpose of each variable and data structure +//duplicate the process state n times +//running mipster like: ./selfie -m 32 yourbinary should generate n instances of yourbinary in a single instance of mipster +//implement preemptive multitasking, i.e., switching between the n instances of yourbinary is determined by mipster +//switch processes every m instructions. 1 <= m <= number of instructions in yourbinary +//implement round-robin scheduling +//add some output in yourbinary to demonstrate context switching +//Deadline: Oct 22, end of day + int main(int argc, int *argv) { int *cstar_argv; int *firstParameter; From 959287a5cfa4f9c246dc29607bf3629ef357c866 Mon Sep 17 00:00:00 2001 From: Johannes Vollmer Date: Thu, 22 Oct 2015 20:23:02 +0200 Subject: [PATCH 09/10] - added assignment01 --- selfie.c | 444 +++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 351 insertions(+), 93 deletions(-) diff --git a/selfie.c b/selfie.c index 76a0530..3e1ff5b 100755 --- a/selfie.c +++ b/selfie.c @@ -3463,6 +3463,20 @@ void emitPutchar() { // ------------------------- INSTRUCTIONS -------------------------- // ----------------------------------------------------------------- +// ------------------------ GLOBAL VARIABLES ----------------------------------- +// ready queue +int* ready_queue; +// number of instructions for one process in one run +int numb_of_instr; +// getting maximal memory size +int MAX_MEM; +// Address of the actual running thread +int* running_process; + +void printList(int *list); +void printListPC(int *list); +void schedule_and_switch(); + void fct_syscall() { if (debug_disassemble) { print((int*)(*(fct_strings+function))); @@ -3931,6 +3945,12 @@ void execute() { } void run() { + int instr; + int* nextprocess; + int *Buffer; + Buffer = (int*)malloc(4*10); + + instr = 0; while (1) { fetch(); @@ -3938,6 +3958,16 @@ void run() { pre_debug(); execute(); post_debug(); + + instr = instr + 1; + *(running_process+1) = *(running_process+1) + 1; + + if(instr == numb_of_instr){ + //round robin scheduling and switching the running process + schedule_and_switch(); + instr = 0; + } + } } @@ -3955,6 +3985,8 @@ int* parse_args(int argc, int *argv, int *cstar_argv) { memorySize = atoi((int*)*(cstar_argv+2)) * 1024 * 1024 / 4; + MAX_MEM = memorySize; + allocateMachineMemory(memorySize*4); // initialize stack pointer @@ -4044,66 +4076,6 @@ void up_copyArguments(int argc, int *argv) { } } -int main_emulator(int argc, int *argv, int *cstar_argv) { - initInterpreter(); - - *(registers+REG_GP) = loadBinary(parse_args(argc, argv, cstar_argv)); - - *(registers+REG_K1) = *(registers+REG_GP); - - up_copyArguments(argc-3, argv+3); - - run(); - - exit(0); -} - -// ----------------------------------------------------------------- -// ----------------------------- MAIN ------------------------------ -// ----------------------------------------------------------------- - -int* copyC2CStarString(int* s) { - int l; - int *r; - int i; - - l = CStringLength(s); - - r = malloc((l + 1) * 4); - - i = 0; - - while (i <= l) { - *(r+i) = rightShift(leftShift(*s, 24 - (i % 4) * 8), 24); - - i = i + 1; - - if (i % 4 == 0) - s = s + 1; - } - - return r; -} - -int* copyC2CStarArguments(int argc, int *argv) { - int *cstar_argv; - int *cursor; - - cstar_argv = malloc(argc * 4); - - cursor = cstar_argv; - - while (argc > 0) { - *cursor = (int)copyC2CStarString((int*)*argv); - - argv = argv + 1; - cursor = cursor + 1; - argc = argc - 1; - } - - return cstar_argv; -} - //// Assignment 0: Basic data structures //// //Review linked lists and implement a simple program using a singly linked list in C*. The minimal requirements are as follows: // @@ -4121,11 +4093,10 @@ int* copyC2CStarArguments(int argc, int *argv) { // The header cannot be deleted!! int* initializeList(int *list) { int *header; - int data = -1; // no data inserted + int data; header = malloc(4*2); *header = 0; - *(header+1) = data; return header; } @@ -4134,7 +4105,7 @@ int* addToList(int *list, int data) { int *newNode; newNode = malloc(4*2); - *newNode = list; + *newNode = (int) list; *(newNode+1) = data; return newNode; } @@ -4143,11 +4114,11 @@ int* addToList(int *list, int data) { int getNodeFromList(int *list, int nthNode) { while(nthNode > 0) { - list = *list; + list = (int*) *list; nthNode = nthNode - 1; } - return list; + return (int) list; } // Get the size of the linked list @@ -4157,7 +4128,7 @@ int sizeOfList(int *list) { count = 0; while(*list != 0) { - list = *list; + list = (int*) *list; count = count + 1; } return count; @@ -4168,15 +4139,16 @@ int* deleteFirstNodeFromList(int *list) { int *prev; int *next; + int size; prev = malloc(2*4); next = malloc(2*4); *prev = 0; *next = 0; - int size = sizeOfList(list); + size = sizeOfList(list); - prev = getNodeFromList(list, size-2); - next = getNodeFromList(list, size); - *prev = next; + prev = (int*) getNodeFromList(list, size-2); + next = (int*) getNodeFromList(list, size); + *prev = (int) next; return list; } @@ -4184,18 +4156,26 @@ int* deleteFirstNodeFromList(int *list) { // Sort the list with Bubble Sort int* sortList(int *list) { - int size = sizeOfList(list); + int size; + int i; + int *listitemX; + int *listitemY; + int x; + int y; + + size = sizeOfList(list); + while(size > 1){ - int i = 0; + i = 0; while(i < size-1) { - int *listitemX = getNodeFromList(list, i); - int *listitemY = getNodeFromList(list, i+1); - int x = *(listitemX+1); - int y = *(listitemY+1); + listitemX = (int*) getNodeFromList(list, i); + listitemY = (int*) getNodeFromList(list, i+1); + x = *(listitemX+1); + y = *(listitemY+1); if(x < y) { - *(listitemX + 1) = y; - *(listitemY + 1) = x; + *(listitemX + 1) = y; + *(listitemY + 1) = x; } i = i + 1; } @@ -4207,22 +4187,25 @@ int* sortList(int *list) { // Print the linked list void printList(int *list) { - int *counter; - counter = malloc(4); - *counter = sizeOfList(list); - - while(*counter >= 0) { - int *nodeNumber = getNodeFromList(list, *counter); - int number = *(nodeNumber+1); - int *Buffer = (int*)malloc(4*10); - print(itoa( number, Buffer, 10, 0)); + int counter; + int *nodeNumber; + int number; + int *Buffer; + + counter = sizeOfList(list)-1; + + while(counter >= 0) { + nodeNumber = (int*) getNodeFromList(list, counter); + number = *(nodeNumber+1); + Buffer = (int*)malloc(4*10); + print(itoa(number, Buffer, 10, 0)); putchar(CHAR_LF); - *counter = *counter - 1; + counter = counter - 1; } - } -int testList() { +//Testing for Assignment 00 +int test00() { int *list; // Create new linked list (FIFO Linked List): @@ -4269,6 +4252,278 @@ int testList() { //add some output in yourbinary to demonstrate context switching //Deadline: Oct 22, end of day +// ------------------------ DATA STRUCTURE FOR A PROCESS ----------------------- +// Creating a new Process with Process ID (new_pid), Programm Counter (new_pc), +// Address to the Register (new_reg), Address to the Memory (new_mem), +// hi register for multiplication/division (new_reg_hi) and +// lo register for multiplication/division (new_reg_lo) and +int* create_process(int new_pid, int* new_reg, int* new_mem, int new_reg_hi, int new_reg_lo){ + //initialization of the process + int* process; + //memory allocation + process = malloc (6 * 4); + + //initalization of the argments of the process + *process = new_pid; + *(process+1) = 0; + *(process+2) = (int) new_reg; + *(process+3) = (int) new_mem; + *(process+4) = new_reg_hi; + *(process+5) = new_reg_lo; + + return process; +} + +// ------------------------ READY QUEUE ---------------------------------------- +// Creating a Ready queue with n processes and with m as number of instructions +void create_ready_queue(int n, int m){ + + int *new_process; + int pid; + int *new_reg; + int *new_mem; + + numb_of_instr = m; + pid = 0; + ready_queue = initializeList(ready_queue); + + while(pid < n){ + new_reg = malloc(4*32); + new_mem = malloc(4*MAX_MEM); + if(pid ==0){ + running_process = create_process(pid, new_reg, new_mem, reg_hi, reg_lo); + } + else{ + new_process = create_process(pid, new_reg, new_mem, reg_hi, reg_lo); + ready_queue = addToList(ready_queue, new_process); + } + pid = pid + 1; + } +} + +void printListPID(int *list) { + int counter; + int number; + int *Buffer; + int *node; + int *process; + + counter = sizeOfList(list)-1; + + while(counter >= 0) { + node = (int*) getNodeFromList(list, counter); + process = *(node+1); + number = *(process); + Buffer = (int*)malloc(4*10); + print(itoa(number, Buffer, 10, 0)); + putchar(CHAR_LF); + counter = counter - 1; + } + +} + +// Print the value of the Program counters +void printListPC(int *list) { + + int counter; + int number; + int *Buffer; + int *node; + int *process; + + counter = sizeOfList(list)-1; + + while(counter >= 0) { + node = (int*) getNodeFromList(list, counter); + process = *(node+1); + number = *(process+1); + Buffer = (int*)malloc(4*10); + print(itoa(number, Buffer, 10, 0)); + putchar(CHAR_LF); + counter = counter - 1; + } + +} + +void schedule_and_switch(){ + int size; + int *node; + size = sizeOfList(ready_queue); + //if the ready queue is empty, the running process can continue + if(size == 0){ + printString('e','m','p','t','y','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'); + return; + } + //else the processes have to be switched so that the next process from the ready queue is allowed to run + else{ + // 1. Saving the states of the running process in the ready queue + ready_queue = addToList(ready_queue, running_process); + // 2. Switching the running process and get the first of the ready queue + node = getNodeFromList(ready_queue, size-1); + running_process = *(node+1); + + // 3. Delete the first Process from ready queue because it is the running process + ready_queue = deleteFirstNodeFromList(ready_queue); + } +} + +void runtest() { + int* nextprocess; + int instr; + int *Buffer; + + instr = 0; + Buffer = (int*)malloc(4*10); + + while (1) { + instr = instr + 1; + *(running_process+1) = *(running_process+1) + 1; + + if(instr == numb_of_instr){ + //round robin scheduling and switching the running process + + //Printing the Ready queue and the running process + printString('-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'); + putchar(CHAR_LF); + printString('-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'); + putchar(CHAR_LF); + printString('R','e','a','d','y','Q','u','e','u','e',' ','a','f','t','e','r',' ','R','u','n'); + printString('n','i','n','g',' ', 'P','r','o','c','e','s','s',CHAR_LF,0,0,0,0,0,0,0); + printString('P','I','D','s',CHAR_LF,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0); + printListPID(ready_queue); + printString('P','C','s',CHAR_LF,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0); + printListPC(ready_queue); + printString('N','e','w',' ','R','u','n','n','i','n','g',' ','P','r','o','c','e','s','s',CHAR_LF); + printString('P','I','D',CHAR_LF,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0); + print(itoa(*(running_process), Buffer, 10, 0)); + putchar(CHAR_LF); + printString('P','C',CHAR_LF,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0); + print(itoa(*(running_process+1), Buffer, 10, 0)); + putchar(CHAR_LF); + schedule_and_switch(); + instr = 0; + + //Printing the Ready queue and the running process + printString('-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'); + putchar(CHAR_LF); + printString('-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'); + putchar(CHAR_LF); + printString('R','e','a','d','y','Q','u','e','u','e',' ','a','f','t','e','r',' ','S','w','i'); + printString('t','c','h','i','n','g',' ', 'P','r','o','c','e','s','s',CHAR_LF,0,0,0,0,0); + printString('P','I','D','s',CHAR_LF,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0); + printListPID(ready_queue); + printString('P','C','s',CHAR_LF,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0); + printListPC(ready_queue); + printString('N','e','w',' ','R','u','n','n','i','n','g',' ','P','r','o','c','e','s','s',CHAR_LF); + printString('P','I','D',CHAR_LF,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0); + print(itoa(*(running_process), Buffer, 10, 0)); + putchar(CHAR_LF); + printString('P','C',CHAR_LF,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0); + print(itoa(*(running_process+1), Buffer, 10, 0)); + putchar(CHAR_LF); + } + } +} + +//Testing for Assignment 01 +int test01() { + + int *Buffer; + + create_ready_queue(5,100000000); + + Buffer = (int*)malloc(4*10); + + printString('-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'); + putchar(CHAR_LF); + printString('-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'); + putchar(CHAR_LF); + //Printing the Ready Queue after the creation (PID's and PC's) + printString('R','e','a','d','y','Q','u','e','u','e',' ','a','f','t','e','r',' ','C','r','e'); + printString('a','t','i','o','n',CHAR_LF,0,0,0,0,0,0,0,0,0,0,0,0,0,0); + printString('P','I','D','s',CHAR_LF,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0); + printListPID(ready_queue); + printString('P','C','s',CHAR_LF,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0); + printListPC(ready_queue); + + //Printing the PID and the PC of the running Process + printString('R','u','n','n','i','n','g',' ','P','r','o','c','e','s','s',CHAR_LF,0,0,0,0); + printString('P','I','D',CHAR_LF,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0); + print(itoa(*(running_process), Buffer, 10, 0)); + putchar(CHAR_LF); + printString('P','C',CHAR_LF,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0); + print(itoa(*(running_process+1), Buffer, 10, 0)); + putchar(CHAR_LF); + + //Testversion of run of the Emulator + runtest(); + + exit(0); +} + +int main_emulator(int argc, int *argv, int *cstar_argv) { + initInterpreter(); + + *(registers+REG_GP) = loadBinary(parse_args(argc, argv, cstar_argv)); + + *(registers+REG_K1) = *(registers+REG_GP); + + up_copyArguments(argc-3, argv+3); + + //create a ready queue and 5 processes + create_ready_queue(5,10); + + run(); + + exit(0); +} + +// ----------------------------------------------------------------- +// ----------------------------- MAIN ------------------------------ +// ----------------------------------------------------------------- + +int* copyC2CStarString(int* s) { + int l; + int *r; + int i; + + l = CStringLength(s); + + r = malloc((l + 1) * 4); + + i = 0; + + while (i <= l) { + *(r+i) = rightShift(leftShift(*s, 24 - (i % 4) * 8), 24); + + i = i + 1; + + if (i % 4 == 0) + s = s + 1; + } + + return r; +} + +int* copyC2CStarArguments(int argc, int *argv) { + int *cstar_argv; + int *cursor; + + cstar_argv = malloc(argc * 4); + + cursor = cstar_argv; + + while (argc > 0) { + *cursor = (int)copyC2CStarString((int*)*argv); + + argv = argv + 1; + cursor = cursor + 1; + argc = argc - 1; + } + + return cstar_argv; +} + int main(int argc, int *argv) { int *cstar_argv; int *firstParameter; @@ -4293,8 +4548,11 @@ int main(int argc, int *argv) { else exit(-1); } - else if (*(firstParameter+1) == 'l') { - testList(); + else if (*(firstParameter+1) == '0') { + test00(); + } + else if (*(firstParameter+1) == '1') { + test01(); } else { exit(-1); @@ -4305,4 +4563,4 @@ int main(int argc, int *argv) { } else // default: compiler main_compiler(); -} +} \ No newline at end of file From 95419696f554aaa20bc700e1768a61db362df18b Mon Sep 17 00:00:00 2001 From: Johannes Vollmer Date: Tue, 27 Oct 2015 13:52:26 +0100 Subject: [PATCH 10/10] - fixed a problem which caused starting the second process in queue instead of the first one --- selfie.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/selfie.c b/selfie.c index 3e1ff5b..f251ee3 100755 --- a/selfie.c +++ b/selfie.c @@ -4295,7 +4295,7 @@ void create_ready_queue(int n, int m){ } else{ new_process = create_process(pid, new_reg, new_mem, reg_hi, reg_lo); - ready_queue = addToList(ready_queue, new_process); + ready_queue = addToList(ready_queue, (int) new_process); } pid = pid + 1; } @@ -4312,7 +4312,7 @@ void printListPID(int *list) { while(counter >= 0) { node = (int*) getNodeFromList(list, counter); - process = *(node+1); + process = (int*) *(node+1); number = *(process); Buffer = (int*)malloc(4*10); print(itoa(number, Buffer, 10, 0)); @@ -4335,7 +4335,7 @@ void printListPC(int *list) { while(counter >= 0) { node = (int*) getNodeFromList(list, counter); - process = *(node+1); + process = (int*) *(node+1); number = *(process+1); Buffer = (int*)malloc(4*10); print(itoa(number, Buffer, 10, 0)); @@ -4357,10 +4357,10 @@ void schedule_and_switch(){ //else the processes have to be switched so that the next process from the ready queue is allowed to run else{ // 1. Saving the states of the running process in the ready queue - ready_queue = addToList(ready_queue, running_process); + ready_queue = addToList(ready_queue, (int) running_process); // 2. Switching the running process and get the first of the ready queue - node = getNodeFromList(ready_queue, size-1); - running_process = *(node+1); + node = (int*) getNodeFromList(ready_queue, size); + running_process = (int*) *(node+1); // 3. Delete the first Process from ready queue because it is the running process ready_queue = deleteFirstNodeFromList(ready_queue); @@ -4563,4 +4563,4 @@ int main(int argc, int *argv) { } else // default: compiler main_compiler(); -} \ No newline at end of file +}