Help with implementing a Loader[Operating Systems]

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2008
Posts: 79
Reputation: orangejuice2005 is an unknown quantity at this point 
Solved Threads: 0
orangejuice2005 orangejuice2005 is offline Offline
Junior Poster in Training

Help with implementing a Loader[Operating Systems]

 
0
  #1
Sep 18th, 2008
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?
  1. import java.io.*;
  2.  
  3. public class Loader
  4. {
  5. public static void main(String args[])
  6.  
  7. String line = null;
  8. int count = 0;
  9.  
  10. try
  11. {
  12. FileReader input = new FileReader();//args[0]);
  13. BufferedReader buffRead = new BufferedReader(new FileReader(input));
  14.  
  15.  
  16. while((line=buffRead.readLine()) != null)
  17. {
  18. System.out.println(count+": "+line);
  19. line = bufRead.readLine();
  20. count++;
  21. }
  22.  
  23.  
  24.  
  25. }
  26. catch (IOException e)
  27. {
  28. // catch possible io errors from readLine()
  29. System.out.println("Got an IOException error!");
  30. e.printStackTrace();
  31. }
  32. close buffRead();
  33.  
  34. }

Thank you! Whoever decides to help me will be getting a lot of this!
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,415
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 256
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Help with implementing a Loader[Operating Systems]

 
0
  #2
Sep 18th, 2008
What problem are you having with it?
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 79
Reputation: orangejuice2005 is an unknown quantity at this point 
Solved Threads: 0
orangejuice2005 orangejuice2005 is offline Offline
Junior Poster in Training

Re: Help with implementing a Loader[Operating Systems]

 
0
  #3
Sep 18th, 2008
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:
  1. Loader.java:39: ';' expected
  2. close buffRead();
  3. ^
  4. Loader.java:41: reached end of file while parsing
  5. } 1A 00 00 00 00 00 00 00 00 00
  6. ^

Its pretty late.....I'm going to wake up early in the morning to start back on this...
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,415
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 256
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Help with implementing a Loader[Operating Systems]

 
0
  #4
Sep 18th, 2008
On this line
  1. 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.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 79
Reputation: orangejuice2005 is an unknown quantity at this point 
Solved Threads: 0
orangejuice2005 orangejuice2005 is offline Offline
Junior Poster in Training

Re: Help with implementing a Loader[Operating Systems]

 
0
  #5
Sep 18th, 2008
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:
  1. Driver {
  2. loader();
  3. loop
  4. scheduler();
  5. dispatcher();
  6. CPU();
  7. waitforinterrupt();
  8. endloop;
  9. }
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:
  1.  
  2. while (not end-of-program data-file)
  3. do {
  4. Read-File();
  5. Extract program attributes into the PCB
  6. Insert hex-code into simulated RAM
  7. }

Update:
  1. import java.io.*;
  2.  
  3. public class Driver
  4. {
  5. //--------------------------------------------------< main >--------//
  6.  
  7. public static void main (String[] args)
  8. {
  9. Driver D = new Driver();
  10. D.loader();
  11. }
  12.  
  13.  
  14. //--------------------------------------------< loader >--------//
  15.  
  16. public StringBuffer loader()
  17. {
  18.  
  19. String line = null;
  20. int count = 0;
  21.  
  22. try
  23. {
  24. FileReader input = new FileReader(args[0]).input();
  25. BufferedReader buffRead = new BufferedReader(new FileReader(input));
  26.  
  27. line = new String();
  28.  
  29. while((line=buffRead.readLine()) != null)
  30. {
  31. System.out.println(count+": "+line);
  32. line = bufRead.readLine();
  33. count++;
  34. }
  35.  
  36. }
  37. catch (IOException e)
  38. {
  39. // catch possible io errors from readLine()
  40. System.out.println("File input error!");
  41. e.printStackTrace();
  42. }
  43. buffRead.close();
  44.  
  45. }

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.
Last edited by orangejuice2005; Sep 18th, 2008 at 12:57 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,415
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 256
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Help with implementing a Loader[Operating Systems]

 
0
  #6
Sep 18th, 2008
  1. while((line=buffRead.readLine()) != null) {
  2. System.out.println(count+": "+line);
  3. line = bufRead.readLine();
  4. count++;
  5. }
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.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 79
Reputation: orangejuice2005 is an unknown quantity at this point 
Solved Threads: 0
orangejuice2005 orangejuice2005 is offline Offline
Junior Poster in Training

Re: Help with implementing a Loader[Operating Systems]

 
0
  #7
Sep 18th, 2008
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:

  1. // JOB 1 17 2
  2. 0xC050005C
  3. 0x4B060000
  4. 0x4B010000
  5. 0x4B000000
  6. 0x4F0A005C
  7. 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....

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


Update:
  1. import java.io.*;
  2.  
  3.  
  4. public class Driver
  5. {
  6. //--------------------------------------------------< main >--------//
  7.  
  8. public static void main (String[] args)
  9. {
  10. //array to hold data
  11. String[] Data = new String[0];
  12. Driver D = new Driver();
  13. D.loader();
  14. }
  15.  
  16.  
  17. //--------------------------------------------< loader >--------//
  18.  
  19. public StringBuffer loader()
  20. {
  21.  
  22. String line = null;
  23. int count = 0;
  24.  
  25. try
  26. {
  27. FileReader input = new FileReader(args[0]).input();
  28. BufferedReader buffRead = new BufferedReader(new FileReader(input));
  29.  
  30. line = new String();
  31.  
  32. while((line=buffRead.readLine()) != null)
  33. {
  34. System.out.println(count+": "+line);
  35. Data = add(line, Data); //add each line to the array as it reads
  36. count++;
  37.  
  38. }
  39. buffRead.close();
  40. }
  41.  
  42. catch (IOException e)
  43. {
  44. // catch possible io errors from readLine()
  45. System.out.println("File input error!");
  46. e.printStackTrace();
  47. }
  48.  
  49. print(Data);
  50.  
  51. }
  52. private static String[] add(String s, String[] array)
  53. {
  54. int len = array.length;
  55. String[] temp = new String[len+1];
  56. System.arraycopy(array, 0, temp, 0, len);
  57. temp[len] = s;
  58. return temp;
  59. }
  60.  
  61. private static void print(String[] data)
  62. {
  63. for(int i = 0; i < data.length; i++)
  64. System.out.println(data[i]);
  65. }
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,483
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 515
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Help with implementing a Loader[Operating Systems]

 
0
  #8
Sep 18th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 79
Reputation: orangejuice2005 is an unknown quantity at this point 
Solved Threads: 0
orangejuice2005 orangejuice2005 is offline Offline
Junior Poster in Training

Re: Help with implementing a Loader[Operating Systems]

 
0
  #9
Sep 18th, 2008
Thanks! Both of you! Ezzaral you were right about there being a missing closing brace. After I added the brace though I kept getting
  1. 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
  1. 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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: Help with implementing a Loader[Operating Systems]

 
0
  #10
Sep 18th, 2008
I believe Masijade had something like this in mind, though I could be a bit off--

  1. import java.io.*;
  2. import java.util.ArrayList;
  3.  
  4. public class Driver
  5. {
  6. //--------------------------------------------------< main >--------//
  7.  
  8. public static void main (String[] args)
  9. {
  10. //array to hold data
  11. //String[] Data = new String[0];
  12. Driver D = new Driver();
  13. System.out.println(D.loader().toString()); // displays the information collected by the StringBuilder in loader
  14. }
  15.  
  16.  
  17. //--------------------------------------------< loader >--------//
  18.  
  19. public StringBuffer loader()
  20. {
  21.  
  22. String line = null;
  23. int count = 0;
  24. String example = ""; // dummy string, change this to the file that needs to be read
  25. ArrayList<String> variableSizeArray = new ArrayList<String>(0); // resizable array that is initially 0 lengthed
  26. StringBuffer sb = new StringBuffer();
  27.  
  28. try
  29. { // does FileReader.input() exist? And does it return a FileReader object?
  30. FileReader input = new FileReader(/*args[0]*/ example)/*.input()*/;
  31. // BufferedReaders wrap Reader objects, and input is already a Reader =P
  32. BufferedReader buffRead = new BufferedReader(/*new FileReader(*/input/*)*/);
  33.  
  34. line = new String();
  35.  
  36. while((line=buffRead.readLine()) != null)
  37. {
  38. System.out.println(count+": "+line);
  39. variableSizeArray.add(line); //add each line to the array as it reads
  40. count++;
  41. }
  42. buffRead.close();
  43. }
  44. catch (IOException e)
  45. {
  46. // catch possible io errors from readLine()
  47. System.out.println("File input error!");
  48. e.printStackTrace();
  49. }
  50.  
  51. String Data[] = variableSizeArray.toArray(new String[variableSizeArray.size()]);
  52.  
  53. print(Data);
  54.  
  55. for(int i = 0; i < Data.length; i++)
  56. sb.append(Data[i]);
  57.  
  58. return sb;
  59.  
  60. }
  61. /*
  62. private static String[] add(String s, String[] array)
  63. {
  64. int len = array.length;
  65. String[] temp = new String[len+1];
  66. System.arraycopy(array, 0, temp, 0, len);
  67. temp[len] = s;
  68. return temp;
  69. }
  70. */
  71.  
  72. private static void print(String[] data)
  73. {
  74. for(int i = 0; i < data.length; i++)
  75. System.out.println(data[i]);
  76. }
  77. }

--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.
Last edited by Alex Edwards; Sep 18th, 2008 at 6:21 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC