viernes, 9 de septiembre de 2011

Adding our own class to NachOS

Hi there, in this post we are going to explain how we added a new class, in order to compile it and execute it using NachOS.(4.0 or 3.4 tested).

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:

//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{
void testSomething(int n);
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:
Here is where we are going to actually put code, methods, etc, in order to add more functionality to NachOS.
Our code is as follows:
<pre class="brush: plain text">
//First, import the header file
#include "example.h"
using namespace std;

cout << "Constructor";
cout << "Destructor";

void Example::testSomething(int n){
for(i = 0; i < n, i++){
cout << "Print #" << n<<endl;

Modifying the Makefile.common:
With this, the only thing left is to add the example.h and 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/synch.h \

THREAD_C =../threads/\
../threads/ \

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
Compiling and executing:
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 en la entrada