ClavinJune

ThreadedKernel.java

Jun 21st, 2017
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.61 KB | None | 0 0
  1. package nachos.threads;
  2.  
  3. import javax.crypto.Mac;
  4.  
  5. import nachos.machine.*;
  6. import nachos.myproj.MyConsole;
  7. import nachos.myproj.PrintRun;
  8.  
  9. /**
  10. * A multi-threaded OS kernel.
  11. */
  12. public class ThreadedKernel extends Kernel {
  13. /**
  14. * Allocate a new multi-threaded kernel.
  15. */
  16. public ThreadedKernel() {
  17. super();
  18. }
  19.  
  20. /**
  21. * Initialize this kernel. Creates a scheduler, the first thread, and an
  22. * alarm, and enables interrupts. Creates a file system if necessary.
  23. */
  24. public void initialize(String[] args) {
  25. // set scheduler
  26. String schedulerName = Config.getString("ThreadedKernel.scheduler");
  27. scheduler = (Scheduler) Lib.constructObject(schedulerName);
  28.  
  29. // set fileSystem
  30. String fileSystemName = Config.getString("ThreadedKernel.fileSystem");
  31. if (fileSystemName != null)
  32. fileSystem = (FileSystem) Lib.constructObject(fileSystemName);
  33. else if (Machine.stubFileSystem() != null)
  34. fileSystem = Machine.stubFileSystem();
  35. else
  36. fileSystem = null;
  37.  
  38. // start threading
  39. new KThread(null);
  40. //new PrintRun("D", 3).run();
  41. alarm = new Alarm();
  42.  
  43. Machine.interrupt().enable();
  44. }
  45.  
  46. /**
  47. * Test this kernel. Test the <tt>KThread</tt>, <tt>Semaphore</tt>,
  48. * <tt>SynchList</tt>, and <tt>ElevatorBank</tt> classes. Note that the
  49. * autograder never calls this method, so it is safe to put additional
  50. * tests here.
  51. */
  52. public void selfTest() {
  53. //KThread.selfTest();
  54. Semaphore.selfTest();
  55. SynchList.selfTest();
  56. if (Machine.bank() != null) {
  57. ElevatorBank.selfTest();
  58. }
  59. }
  60.  
  61. /**
  62. * A threaded kernel does not run user programs, so this method does
  63. * nothing.
  64. */
  65. Semaphore sem;
  66. public void run() {
  67. /**Timer Start form Here **/
  68. /*System.out.println("Timer ");
  69. Machine.interrupt().disable();
  70. Machine.timer().setInterruptHandler(new Runnable() {
  71. @Override
  72. public void run() {
  73. //System.out.println("Timer Jalan");
  74. }
  75. });
  76.  
  77. long time = Machine.timer().getTime();
  78. System.out.println("time: " + time); //returning tick
  79. */
  80. //new Semaphore(0).P();
  81.  
  82. /**Timer End Here **/
  83.  
  84. /**stubFileSystem start from here **/
  85.  
  86. //ubah nachos.conf stubfilesystem jadi true
  87. //dia ada di src/nachos/test/
  88. //ganti di nachos.conf directorynya
  89. // FileSystem.testDirectory = /home/prk/
  90.  
  91. //ini untuk create file
  92. //Machine.stubFileSystem().open("halo.txt", true);
  93.  
  94. //untuk write
  95. /*OpenFile of = Machine.stubFileSystem().open("halo.txt", true);
  96. if(of != null){
  97. String message = "test";
  98. byte[] buf = message.getBytes();
  99. of.write(buf, 0, buf.length);
  100. }
  101.  
  102. //read
  103. OpenFile of2 = Machine.stubFileSystem().open("halo.txt", false);
  104. if(of2 != null){
  105. byte[] buf = new byte[100];
  106. of2.read(buf, 0, buf.length);
  107. String message = new String(buf);
  108. System.out.println(message);
  109. }*/
  110. /**stubfilesystem End Here **/
  111.  
  112. /**network link start here **/
  113. //networklink ubah true
  114. //NetworkLink.reliability isi double antara 0-1
  115.  
  116. /*System.out.println("address :" + Machine.networkLink().getLinkAddress());
  117.  
  118.  
  119. //recv
  120. Machine.networkLink().setInterruptHandlers(new Runnable() {
  121.  
  122. @Override
  123. public void run() {
  124. System.out.println("Packet recv");
  125.  
  126. Packet pkt2 = Machine.networkLink().receive();
  127. String message = new String(pkt2.contents);
  128. System.out.println(message);
  129.  
  130. sem.V();
  131. }
  132. },new Runnable() {
  133.  
  134. @Override
  135. public void run() {
  136. System.out.println("Packet sent");
  137.  
  138. }
  139. });
  140.  
  141. //send packet
  142. byte[] packetBytes = new String("Hello World").getBytes();
  143. Packet pkt;
  144. try {
  145. pkt = new Packet(0, 0, packetBytes);
  146. Machine.networkLink().send(pkt);
  147. sem = new Semaphore(0);
  148. sem.P();
  149. } catch (MalformedPacketException e) {
  150. // TODO Auto-generated catch block
  151. e.printStackTrace();
  152. }
  153. try {
  154. byte[] packetBytez = new String("Hello World2").getBytes();
  155. pkt = new Packet(0, 0, packetBytez);
  156. Machine.networkLink().send(pkt);
  157. sem = new Semaphore(0);
  158. sem.P();
  159. } catch (MalformedPacketException e) {
  160. // TODO Auto-generated catch block
  161. e.printStackTrace();
  162. }*/
  163. /**network link end here **/
  164. /*console start here*/
  165. /*Machine.console().setInterruptHandlers(new Runnable() {
  166.  
  167. @Override
  168. public void run() {
  169. System.out.println("recv");
  170. System.out.println((char)Machine.console().readByte());
  171. sem.V();
  172. }
  173. }, new Runnable() {
  174.  
  175. @Override
  176. public void run() {
  177. System.out.println("sent");
  178. sem.V();
  179.  
  180. }
  181. });
  182. Machine.console().writeByte(65 ascii a);
  183. sem = new Semaphore(0);
  184. sem.P();
  185. Machine.console().writeByte(67 ascii a);
  186. sem = new Semaphore(0);
  187. sem.P();
  188. Machine.console().writeByte(9 ascii a);
  189. sem = new Semaphore(0);
  190. sem.P();
  191.  
  192. Machine.console().readByte();
  193. sem = new Semaphore(0);
  194. sem.P();*/
  195. /*console end here*/
  196.  
  197. /**bikinconsole**/
  198.  
  199.  
  200. MyConsole mc = new MyConsole();
  201. /*mc.writeLine("anjay");
  202. String text = mc.readLine();
  203. System.out.println(text);*/
  204. /**bikinconsole**/
  205. /** processor start here **/
  206. /*mc.writeLine("Proccessor!\n");
  207. mc.writeLine("Memory : " + Machine.processor().getMemory().length+'\n');
  208. */
  209. /** processor end here **/
  210.  
  211. /**Scheduler start here **/
  212. new KThread(new PrintRun("A", 3)).fork();
  213. new KThread(new PrintRun("B", 3)).fork();
  214. new KThread(new PrintRun("C", 3)).fork();
  215. /**Scheduler end here **/
  216.  
  217. new Semaphore(0).P();
  218. }
  219.  
  220. /**
  221. * Terminate this kernel. Never returns.
  222. */
  223. public void terminate() {
  224. Machine.halt();
  225. }
  226.  
  227. /** Globally accessible reference to the scheduler. */
  228. public static Scheduler scheduler = null;
  229. /** Globally accessible reference to the alarm. */
  230. public static Alarm alarm = null;
  231. /** Globally accessible reference to the file system. */
  232. public static FileSystem fileSystem = null;
  233.  
  234. // dummy variables to make javac smarter
  235. private static RoundRobinScheduler dummy1 = null;
  236. private static PriorityScheduler dummy2 = null;
  237. private static LotteryScheduler dummy3 = null;
  238. private static Condition2 dummy4 = null;
  239. private static Communicator dummy5 = null;
  240. private static Rider dummy6 = null;
  241. private static ElevatorController dummy7 = null;
  242. }
Add Comment
Please, Sign In to add comment