First of all, where to add the code is important. Right now we are working with semaphores, schedules, locks, threads, etc. So in this case, the new class was added to the nachos/code/threads folder, in order to include the existing libraries of semaphores and threads.
Adding a new header file:
Knowing that, we created a new file: example.h. In that class we didn't do anything special, just declared some methods, and attributes for this example, like this:
Knowing that, we created a new file: example.h. In that class we didn't do anything special, just declared some methods, and attributes for this example, like this:
//Here is were you include libraries that you are going to use in your code like:
#include <iostream>
#include "thread.h"
#include "synch.h"
class Example{
public:
//Constructor
//Constructor
Example();
//Destructor
~Example();
void testSomething(int n);
private:
int n;
};
(Note: In C++ it is not neccesary to name the file after the class)
Adding a new cc file:
After that, we proceeded to write another code, this time named: example.cc.
Here is where we are going to actually put code, methods, etc, in order to add more functionality to NachOS.
Our example.cc code is as follows:
<pre class="brush: plain text">
//First, import the header file
#include "example.h"
using namespace std;
Example::Example(){
cout << "Constructor";
}
Example::~Example(){
cout << "Destructor";
}
void Example::testSomething(int n){
for(i = 0; i < n, i++){
cout << "Print #" << n<<endl;
}
}
</pre>
Modifying the Makefile.common:
With this, the only thing left is to add the example.h and example.cc files to the Makefile.common in the nachos/code/ folder in order to compile it with the rest of the NachOS code.
To do that, we just added some lines to the already existing code. We added code to the threads folder, so the new lines had to be added in the THREAD_H, and THREAD_C sections, but also in THREAD_O. Like this:
THREAD_H =../threads/copyright.h\ |
../threads/list.h\ |
../threads/scheduler.h\ |
../threads/synch.h \ |
../threads/synchlist.h\ |
../threads/system.h\ |
../threads/thread.h\ |
../threads/utility.h\ ../threads/example.h\ |
../machine/interrupt.h\ |
../machine/sysdep.h\ |
../machine/stats.h\ |
../machine/timer.h |
THREAD_C =../threads/main.cc\ |
../threads/list.cc\ |
../threads/scheduler.cc\ |
../threads/synch.cc \ |
../threads/synchlist.cc\ |
../threads/system.cc\ |
../threads/thread.cc\ |
../threads/utility.cc\ |
../threads/threadtest.cc\ ../threads/example.cc\ |
../machine/interrupt.cc\ |
../machine/sysdep.cc\ |
../machine/stats.cc\ |
../machine/timer.cc |
THREAD_O =main.o list.o scheduler.o synch.o synchlist.o system.o thread.o \ |
utility.o threadtest.o interrupt.o stats.o sysdep.o timer.o example.o |
And now we are ready to compile NachOS, to to this either compile everything running make from the nachos/code folder, or just compile the threads folder doing the same, in the threads folder.
To test the code, just run the nachos executable found in the threads folder, and it should print something like this:
(image goes here)
And thats it, thats how we added a new class to the existing nachos code, if you have any questions or suggestions, feel free to comment.
No hay comentarios:
Publicar un comentario