Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package nachos.threads;
- import javax.crypto.Mac;
- import nachos.machine.*;
- import nachos.myproj.MyConsole;
- import nachos.myproj.PrintRun;
- /**
- * A multi-threaded OS kernel.
- */
- public class ThreadedKernel extends Kernel {
- /**
- * Allocate a new multi-threaded kernel.
- */
- public ThreadedKernel() {
- super();
- }
- /**
- * Initialize this kernel. Creates a scheduler, the first thread, and an
- * alarm, and enables interrupts. Creates a file system if necessary.
- */
- public void initialize(String[] args) {
- // set scheduler
- String schedulerName = Config.getString("ThreadedKernel.scheduler");
- scheduler = (Scheduler) Lib.constructObject(schedulerName);
- // set fileSystem
- String fileSystemName = Config.getString("ThreadedKernel.fileSystem");
- if (fileSystemName != null)
- fileSystem = (FileSystem) Lib.constructObject(fileSystemName);
- else if (Machine.stubFileSystem() != null)
- fileSystem = Machine.stubFileSystem();
- else
- fileSystem = null;
- // start threading
- new KThread(null);
- //new PrintRun("D", 3).run();
- alarm = new Alarm();
- Machine.interrupt().enable();
- }
- /**
- * Test this kernel. Test the <tt>KThread</tt>, <tt>Semaphore</tt>,
- * <tt>SynchList</tt>, and <tt>ElevatorBank</tt> classes. Note that the
- * autograder never calls this method, so it is safe to put additional
- * tests here.
- */
- public void selfTest() {
- //KThread.selfTest();
- Semaphore.selfTest();
- SynchList.selfTest();
- if (Machine.bank() != null) {
- ElevatorBank.selfTest();
- }
- }
- /**
- * A threaded kernel does not run user programs, so this method does
- * nothing.
- */
- Semaphore sem;
- public void run() {
- /**Timer Start form Here **/
- /*System.out.println("Timer ");
- Machine.interrupt().disable();
- Machine.timer().setInterruptHandler(new Runnable() {
- @Override
- public void run() {
- //System.out.println("Timer Jalan");
- }
- });
- long time = Machine.timer().getTime();
- System.out.println("time: " + time); //returning tick
- */
- //new Semaphore(0).P();
- /**Timer End Here **/
- /**stubFileSystem start from here **/
- //ubah nachos.conf stubfilesystem jadi true
- //dia ada di src/nachos/test/
- //ganti di nachos.conf directorynya
- // FileSystem.testDirectory = /home/prk/
- //ini untuk create file
- //Machine.stubFileSystem().open("halo.txt", true);
- //untuk write
- /*OpenFile of = Machine.stubFileSystem().open("halo.txt", true);
- if(of != null){
- String message = "test";
- byte[] buf = message.getBytes();
- of.write(buf, 0, buf.length);
- }
- //read
- OpenFile of2 = Machine.stubFileSystem().open("halo.txt", false);
- if(of2 != null){
- byte[] buf = new byte[100];
- of2.read(buf, 0, buf.length);
- String message = new String(buf);
- System.out.println(message);
- }*/
- /**stubfilesystem End Here **/
- /**network link start here **/
- //networklink ubah true
- //NetworkLink.reliability isi double antara 0-1
- /*System.out.println("address :" + Machine.networkLink().getLinkAddress());
- //recv
- Machine.networkLink().setInterruptHandlers(new Runnable() {
- @Override
- public void run() {
- System.out.println("Packet recv");
- Packet pkt2 = Machine.networkLink().receive();
- String message = new String(pkt2.contents);
- System.out.println(message);
- sem.V();
- }
- },new Runnable() {
- @Override
- public void run() {
- System.out.println("Packet sent");
- }
- });
- //send packet
- byte[] packetBytes = new String("Hello World").getBytes();
- Packet pkt;
- try {
- pkt = new Packet(0, 0, packetBytes);
- Machine.networkLink().send(pkt);
- sem = new Semaphore(0);
- sem.P();
- } catch (MalformedPacketException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- try {
- byte[] packetBytez = new String("Hello World2").getBytes();
- pkt = new Packet(0, 0, packetBytez);
- Machine.networkLink().send(pkt);
- sem = new Semaphore(0);
- sem.P();
- } catch (MalformedPacketException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }*/
- /**network link end here **/
- /*console start here*/
- /*Machine.console().setInterruptHandlers(new Runnable() {
- @Override
- public void run() {
- System.out.println("recv");
- System.out.println((char)Machine.console().readByte());
- sem.V();
- }
- }, new Runnable() {
- @Override
- public void run() {
- System.out.println("sent");
- sem.V();
- }
- });
- Machine.console().writeByte(65 ascii a);
- sem = new Semaphore(0);
- sem.P();
- Machine.console().writeByte(67 ascii a);
- sem = new Semaphore(0);
- sem.P();
- Machine.console().writeByte(9 ascii a);
- sem = new Semaphore(0);
- sem.P();
- Machine.console().readByte();
- sem = new Semaphore(0);
- sem.P();*/
- /*console end here*/
- /**bikinconsole**/
- MyConsole mc = new MyConsole();
- /*mc.writeLine("anjay");
- String text = mc.readLine();
- System.out.println(text);*/
- /**bikinconsole**/
- /** processor start here **/
- /*mc.writeLine("Proccessor!\n");
- mc.writeLine("Memory : " + Machine.processor().getMemory().length+'\n');
- */
- /** processor end here **/
- /**Scheduler start here **/
- new KThread(new PrintRun("A", 3)).fork();
- new KThread(new PrintRun("B", 3)).fork();
- new KThread(new PrintRun("C", 3)).fork();
- /**Scheduler end here **/
- new Semaphore(0).P();
- }
- /**
- * Terminate this kernel. Never returns.
- */
- public void terminate() {
- Machine.halt();
- }
- /** Globally accessible reference to the scheduler. */
- public static Scheduler scheduler = null;
- /** Globally accessible reference to the alarm. */
- public static Alarm alarm = null;
- /** Globally accessible reference to the file system. */
- public static FileSystem fileSystem = null;
- // dummy variables to make javac smarter
- private static RoundRobinScheduler dummy1 = null;
- private static PriorityScheduler dummy2 = null;
- private static LotteryScheduler dummy3 = null;
- private static Condition2 dummy4 = null;
- private static Communicator dummy5 = null;
- private static Rider dummy6 = null;
- private static ElevatorController dummy7 = null;
- }
Add Comment
Please, Sign In to add comment