I have this big operating system's project where I have to basically write my own mini operating system. I have everything broken into pieces and am taking it step by step. Starting with the loader. Prior to this class, I had no idea how intricate an OS system was and everything is fairly new to me. I've decided to do this in java for various reason mainly b/c it'll be a good refresher for me. Coding isn't my strong point...actually its probably a weak point and it's been 2 yrs since I last worked with java.

Can any1 with the patience and expertise help me through this ordeal? Ok so i understand the basic concept of the loader and i've started something here....the reading the input part. I have yet to get to implementing the part where it loads it into memory [I'll be using an array].

Can someone help me double check this?

import java.io.*;

public class Loader
{
	public static void main(String args[])

	String line = null;
  	int count = 0;

	try
	{
		FileReader input = new FileReader();//args[0]);
	   BufferedReader  buffRead = new BufferedReader(new FileReader(input));
	

		while((line=buffRead.readLine()) != null)
	 	{  
			System.out.println(count+": "+line);
			line = bufRead.readLine();
			count++;
     	        }

	
		
	}
	catch (IOException e)
            {
		// catch possible io errors from readLine()
		System.out.println("Got an IOException error!");
                        e.printStackTrace();
            }
	close buffRead();

}

Thank you! Whoever decides to help me will be getting a lot of this!

Recommended Answers

All 22 Replies

What problem are you having with it?

When i compile it i get these errors.....and its more so that I don't know how to implement the second part.....have it write what it reads into memory.

Errors:

Loader.java:39: ';' expected
	close buffRead();
	              ^
Loader.java:41: reached end of file while parsing
} 1A 00 00 00 00 00 00 00 00 00
  ^

Its pretty late.....I'm going to wake up early in the morning to start back on this...

On this line

close buffRead();

you have the method before the object. Is that right? No. How did you call readLine? Call close the same way.

As for "the second part" Java does not give you direct access to memory.

You should not be doing this stuff in main. You need to place it into a different method, and have that method return a String, or StringBuffer, or something to that effect. Then other parts of your program/os/whatever will simply call the method, using the filename as a parameter, and retreive the whatever as the result.

Was i just tired when that happened? Thanks for pointing those things out to me. Here is a basic sketch of what am trying to do:

Driver {
			loader();
			loop
				scheduler();
				dispatcher();
				CPU();
				waitforinterrupt();
			endloop;
			}

The driver is my main and its going to call the loader which in turn loads user programs, which will already be assembled by our professor (given as a stream of hex character) and stored in a ‘program-file.’

The basic steps of the loader are:

while (not end-of-program data-file) 
do {
	Read-File();
	Extract program attributes into the PCB
            Insert hex-code into simulated RAM
    }

Update:

import java.io.*;

public class Driver
{
 //--------------------------------------------------< main >--------//

     public static void main (String[] args) 
	  {
        Driver D = new Driver();
        D.loader();
     } 


     //--------------------------------------------< loader >--------//
	  
	  public StringBuffer loader() 
	  {
	  
		String line = null;
  		       int count = 0;

		try
		{
		       FileReader input = new FileReader(args[0]).input();
    BufferedReader  buffRead = new BufferedReader(new FileReader(input));
		
			line = new String();

			while((line=buffRead.readLine()) != null)
	 		{  
				System.out.println(count+": "+line);
				line = bufRead.readLine();
				count++;
     			}

		}
		catch (IOException e)
   		{
			// catch possible io errors from readLine()
			System.out.println("File input error!");
      		            e.printStackTrace();
   		}
		buffRead.close();

}

Am still getting the second error and I know you said I needed to do it in a method and I did. But rather than throw the exception, i still get the second error.

while((line=buffRead.readLine()) != null) {
    System.out.println(count+": "+line);
    line = bufRead.readLine();
    count++;
}

You are reading two lines every pass through the while loop. Once in the condition and again in the body. The error is probably coming because the while loop condition reads the last line in the file, then you try to read another line in the body of the while loop.

Delete the readLine in the body of the while loop.

Thanks I realized that earlier and i've already stopped getting that error. Am trying to figure out how to store an indefinite number of strings into an array. And am going through a trial and error process here. This is a sample data to be read in:

// JOB 1 17 2
0xC050005C
0x4B060000
0x4B010000
0x4B000000
0x4F0A005C
0x4F0D00DC

In the middle of things i get this error and i don't know why I do b/c there really is no file being read yet....

Loader.java:72: reached end of file while parsing

Update:

import java.io.*;


public class Driver
{
 //--------------------------------------------------< main >--------//

     public static void main (String[] args) 
	  {
        //array to hold data
		  String[] Data = new String[0];		  
		  Driver D = new Driver();
        D.loader();
     } 


     //--------------------------------------------< loader >--------//
	  
	  public StringBuffer loader() 
	  {
	  	
		String line = null;
  			int count = 0;

		try
		{
		FileReader input = new FileReader(args[0]).input();
 BufferedReader  buffRead = new BufferedReader(new FileReader(input));
		
		line = new String();

		while((line=buffRead.readLine()) != null)
	 	{  
	            System.out.println(count+": "+line);
		Data = add(line, Data); //add each line to the array as it reads
			count++;
					
     		}
		buffRead.close();
		}

		catch (IOException e)
   		{
			// catch possible io errors from readLine()
			System.out.println("File input error!");
      		            e.printStackTrace();
   		}

		print(Data);

}
private static String[] add(String s, String[] array)
{
        int len = array.length;
        String[] temp = new String[len+1];
        System.arraycopy(array, 0, temp, 0, len);
        temp[len] = s;
        return temp;
}

private static void print(String[] data) 
{
      	for(int i = 0; i < data.length; i++)
         System.out.println(data[i]);
}

I'm not sure what you are using to compile it, but that message may stem from the fact that you have no closing brace for the end of the class (assuming you posted the whole thing). There certainly isn't a line 72 in what you posted.

Thanks! Both of you! Ezzaral you were right about there being a missing closing brace. After I added the brace though I kept getting

missing Return type error.

I know one way to solve it would be changing the return type to void but masijade previously said that I needed to be doing this as a method and have a String or StringBuffer return type but when I tried to do that I got the error. When i added a return statement....it kept on saying

Cannot find symbol.

Maybe i was putting the return statement in the wrong place or i really have no clue of what am talking about.

I believe Masijade had something like this in mind, though I could be a bit off--

import java.io.*;
import java.util.ArrayList;

public class Driver
{
 //--------------------------------------------------< main >--------//

	public static void main (String[] args)
	{
		//array to hold data
		//String[] Data = new String[0];
		Driver D = new Driver();
		System.out.println(D.loader().toString()); // displays the information collected by the StringBuilder in loader
	}


	//--------------------------------------------< loader >--------//

	public StringBuffer loader()
	{

		String line = null;
		int count = 0;
		String example = ""; // dummy string, change this to the file that needs to be read
		ArrayList<String> variableSizeArray = new ArrayList<String>(0); // resizable array that is initially 0 lengthed
		StringBuffer sb = new StringBuffer();

		try
		{															// does FileReader.input() exist? And does it return a FileReader object?
			FileReader input = new FileReader(/*args[0]*/ example)/*.input()*/;
															// BufferedReaders wrap Reader objects, and input is already a Reader =P
			BufferedReader  buffRead = new BufferedReader(/*new FileReader(*/input/*)*/);

			line = new String();

			while((line=buffRead.readLine()) != null)
			{
				System.out.println(count+": "+line);
				variableSizeArray.add(line); //add each line to the array as it reads
				count++;
			}
			buffRead.close();
		}
		catch (IOException e)
		{
			// catch possible io errors from readLine()
			System.out.println("File input error!");
			e.printStackTrace();
		}

		String Data[] = variableSizeArray.toArray(new String[variableSizeArray.size()]);

		print(Data);

		for(int i = 0; i < Data.length; i++)
			sb.append(Data[i]);

		return sb;

	}
	/*
	private static String[] add(String s, String[] array)
	{
		int len = array.length;
		String[] temp = new String[len+1];
		System.arraycopy(array, 0, temp, 0, len);
		temp[len] = s;
		return temp;
	}
	*/

	private static void print(String[] data)
	{
		for(int i = 0; i < data.length; i++)
			System.out.println(data[i]);
	}
}

--notice the dummy String declared near the top of the method. You'll have to change it to the actual path for the file you ned to read information from.

As for commenting out your String-appending method, I instead used an ArrayList to append each String to the list. If your Instructor will not allow this, uncomment the method and use that instead of an ArrayList.

For more information about ArrayLists, refer to this link.

Edit: Also if this isn't a multi-threaded application and speed is an issue, StringBuilder may be preferable instead of using a StringBuffer.

Oh, word of caution when using StringBuilder and StringBuffer! I believe that if you append regular characters, they may be interpreted as numbers.

To prevent this (char c is an example--) , try append(c + "");

Thanks a lot Alex! The string appending method was a second choice and it actually isn't my code......I got the snipet from somewhere and it worked. I orginally was working with an arraylist but i was getting a lot of errors because i wasn't doing it right so thnx for simplifying it for me

And no....FileReader.input doesn't actually exist.....once again i was doing it wrong. Thanks for poiting out that mistake about BufferedReaders. I've never heard of StringBuilder untill now and thnx for the link. Speed issues are an afterthought for me b/c am more worried about actually having something that works. But thnx.....StringBuilder is the better choice.

Everything else looks right but I can't help but think i left out some intricacies to the program. Here's some detail to what am trying to accomplish and all i've done so far is read in the string and store it within an array.

The Loader
The loader module opens (once at the start) the program-file and does the loading of the user programs/jobs into the disk. Programs are loaded into disk according to the format of the batched user programs in the program-file. Ancillary programs would be needed to process (strip off) the special control “cards” – which start with, e.g., ‘// Job1 17 2’ before storing the (binary) hex-code. The data sections start with, e.g., ‘// Data B C C’. 

The ‘// Job1 17 2’ means user program/job 1 is of size 17 (hex) or (23 in decimal) words with the priority set to 2. Each data segment of a program starts with ‘// Data B C C’, which means the number of words in the job’s input buffer is 11 (B in hex), its output buffer size is 12 (C in hex) words and the size of its temporary buffer is 12 (C in hex) words. The control cards in the program-file contain directives – attribute data about each program, must be extracted and stored in the Process Control Block (PCB) (see below).

The basic steps of the loader are:
	
	while (not end-of-program data-file) do {
		Read-File();
		Extract program attributes into the PCB
Insert hex-code into simulated RAM
	}

I need to tokenize certain strings (strip off special control cards) before storing the hex code into array.

How do tell java to recognize a particular string and then tokenize it? The tokenized string needs to be stored in the PCB.


Heres what i have but this just tokenizes the whole file...

// Tokenizer to read from a file
StreamTokenizer t = new StreamTokenizer(input);
// Discard comments
t.slashSlashComments(true);
			
String[] ctrlcard = new String[t.countTokens()]; //Array to hold tokens
			
//Extract the Toekns
for (int i = 0; i<ctrlcard.length; i++)
{
	ctrlcard[i] = t.nextToken();
}
			
//Print Output
for (int i = 0; i<ctrlcard.length; i++)
{
	System.out.println(ctrlcard[i]);
}

Hmmm, are these variable strings?

You may need to specify a switch-case for them.

Then again, you'll need to provide some more information on the representation of these strings.

There's a number of ways it can be done, but before introducing any let's see if we can make the process simpler by having you supplying some more information =)

Yay Alex is back!...Heres a sample data of how the strings are going to look like and if you take a look at post #12...u'll see what they mean.

// JOB 1 17 2
0xC050005C
0x4B060000
0x4B010000
0x4B000000
0x4F0A005C
0x4F0D00DC
0x4C0A0004
0xC0BA0000
0x42BD0000
0x4C0D0004
0x4C060001
0x10658000
0x56810018
0x4B060000
0x4F0900DC
0x43970000
0x05070000
0x4C060001
0x4C090004
0x10658000
0x5681003C
0xC10000AC
0x92000000
// Data 14 C C
0x0000000A
0x00000006
0x0000002C
0x00000045
0x00000001
0x00000007
0x00000000
0x00000001

I'm not near a Java compiler at the moment, so I'll have to give you a pseudo-code suggestion.

(I'll make an attempt at an implementation, but don't expect too much).

Create a BufferedReader (import java.util.*) and read lines from the file using the method BufferedReader.readLine() method.

Create a HashMap<String, ArrayList<String> >

After each successful read, check to see if the first char starts with a backslash and if it does, use that as a key for a HashMap and store the following information in a new ArrayList for the cooresponding map then add the ArrayList to the map.

Hashmaps....scary. An array of arrays....hey thnx! I'll give that a go. I'll come back here and post what i was able to come up with....Thnx again Alex

I wish i could come up with something better...but its real late and i need to go to bed.

import java.io.*;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.util.*
 
public class Driver
{
 //--------------------------------------------------< main >--------//
 
public static void main (String[] args)
{
Driver D = new Driver();
System.out.println(D.loader().toString()); // displays the information collected by the StringBuilder in loader
}
 
 
//--------------------------------------------< loader >--------//
 
public StringBuilder loader()
{
 
String line = null;
int count = 0;
String example = ""; // dummy string, change this to the file that needs to be read
// Creating new HashMap objects
// keys are String(backslashes), values are corresponding string 
HashMap<String, ArrayList> hashMap = new HashMap<String, ArrayList>();
ArrayList<String> variableSizeArray = new ArrayList<String>(0); // resizable array that is initially 0 lengthed
StringBuilder sb = new StringBuilder();
 
try
{	
// FileReader.input() does not exist....where do i read from?
FileReader input = new FileReader(/*args[0]*/ example)/*.input()*/;
BufferedReader  buffRead = new BufferedReader(/*new FileReader(*/input/*)*/);
		
			
					
line = new String(); // string buffer for file reading  
 
while((line=buffRead.readLine()) != null)
{
System.out.println(count+": "+line);
for (int i = 0; i < line.length(); i++) 
{
// trigger condition if current character is a backslash
if ((line.slashSlashComments(true)) // is this even right? legal?
{
						   				
/* Tokenizer to read from a file
StreamTokenizer t = new StreamTokenizer(input);
hashMap.put(t.countTokens()); //Arraylist to hold tokens
			
//Extract the Tokens
for (int i = 0; i<hashMap.length; i++)
{
hashMap[i] = t.nextToken();
}
			
//Print Output
for (int i = 0; i<hashMap.length; i++)
{
System.out.println(hashMap[i]);
}*/
}
}
variableSizeArray.add(line); //add each line to the array as it reads
count++;
}
buffRead.close();
}
catch (IOException e)
{
	// catch possible io errors from readLine()
	System.out.println("File input error!");
	e.printStackTrace();
}
 
String Data[] = variableSizeArray.toArray(new String[variableSizeArray.size()]);
 
print(Data);
 
for(int i = 0; i < Data.length; i++)
sb.append(Data[i]);
 
return sb;
 
}
 
private static void print(String[] data)
{
	for(int i = 0; i < data.length; i++)
	           System.out.println(data[i]);
}
}

Yay Alex is back!...Heres a sample data of how the strings are going to look like and if you take a look at post #12...u'll see what they mean.

// JOB 1 17 2
0xC050005C
0x4B060000
0x4B010000
0x4B000000
0x4F0A005C
0x4F0D00DC
0x4C0A0004
0xC0BA0000
0x42BD0000
0x4C0D0004
0x4C060001
0x10658000
0x56810018
0x4B060000
0x4F0900DC
0x43970000
0x05070000
0x4C060001
0x4C090004
0x10658000
0x5681003C
0xC10000AC
0x92000000
// Data 14 C C
0x0000000A
0x00000006
0x0000002C
0x00000045
0x00000001
0x00000007
0x00000000
0x00000001

I used the above example in a .txt file called Loader_Information in this code snippet--

import java.util.*;
import java.io.*;

public class OS_Loader{

	private static HashMap<String, ArrayList<String> > hm
			= new HashMap<String, ArrayList<String> >();

	public static void main(String... args){

		OS_Loader.store("F:/123456789/Loader_Information.txt");
		for(String element : OS_Loader.getCodeValues("// JOB 1 17 2")){
			System.out.println(element);
		}

	}

	public static boolean store(String arg){
		BufferedReader br = null;
		FileReader fr = null;
		File f = null;

		try{
			f = new File(arg);
			fr = new FileReader(f);
			br = new BufferedReader(fr);
		}catch(FileNotFoundException fnfe){
			fnfe.printStackTrace();
			return false;
		}

		String s = "", currentKey = "";

		try{
			while((s = br.readLine()) != null){
				if(s.substring(0, 2).equalsIgnoreCase("//")){
					currentKey = s;
					hm.put(currentKey, new ArrayList<String>(0));
				}else{
					hm.get(currentKey).add(s);
				}
			}
		}catch(IOException ioe){
			ioe.printStackTrace();

		}finally{
			try{
				br.close();
			}catch(Throwable t){System.exit(1);}
		}

		return true;
	}

	public static ArrayList<String> getCodeValues(String key){
		return hm.get(key);
	}
}

--notice the advantage of doing this. In the event that you need a particular set of instructions you can obtain them via the associated control card.

I'm putting this on pause for now.....am missing a lot of information and things that i need before i work on the loader. And parsing would work better right? If i needed to get a particular set of instructions...i just parse the contral card info(process info) that the PCB reads, storing that somewhere...and store the process data into memory.


But thnx....like i said I'm missing a whole lot of info and the loader isn't coming out right. I'll get back to this wen i get the PCB and Memory level done.

Oh I'm sorry. I understand now.

I see that you want to tokenize the actual control cards to determine their size, etc and then collect additional information for the actual control-cards (such as their data sections and the attributes for the data sections).

I thought you needed an easier means of grouping the data, but I misunderstood at first.

It's still possible to use my first suggestion, however you will need to determine what defines a control card. For example, will all sizes of user programs be 16+ in hexadecimal? Is the data reliably grouped in the .txt file?

I can see of another way of this being done, but I need more information about how the information is organized in the .txt file (consistency, etc) and if there is a specific format for user programs (do they always have a number at the end of their name to identify them?), and do Data section always start with the word Data?

Yes thats what i've been trying to do. And now that i've gone deeper into the Operating system structure, i need to interconnect these 3 classes am working on. PCB, Hardware Layer and Loader. The PCB takes and stores process info(control card) and is stored in a data structure of choice. While the Hardware layer, defines the memory and disk block and the loader reads in the user program and strips of the control card then stores the process data in disk/memory.

I'll just attached everything that i've done so far and a sample program text file we'll be getting so u'll know the format.

I havn't done the short term and long term scheduler yet or the CPU...but i wanted to finish this before the weekened is over with and start on those as soon as possible. Thanks again for seeing through this with me.

Crap am not sure if am allowed to this do but my thread is getting lost amongst other (solved) threads.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.