Reputation: 33
So I am learning to create my own SerialConsole
in nachos (Java). I learned using Semaphore.P()
and Semaphore.V()
to wait for user input. Everything is going well until I tried to make a function like getch()
in C's conio.h
.
The problem is, whenever I called Semaphore.P(), even though the Semaphore.V() is called, it will always wait for Enter
key to be pressed before it resume the program. I wanted the program to resume whenever I press a key.
Below is some code I tried.
Console.java
public class Console {
private SerialConsole console;
private Semaphore sem = new Semaphore(0);
private Runnable send, recv;
private char tempChar;
public Console() {
console = Machine.console();
send = new Runnable() {
@Override
public void run() {
sem.V();
}
};
recv = new Runnable() {
@Override
public void run() {
tempChar = (char) console.readByte();
sem.V();
}
};
console.setInterruptHandlers(recv, send);
}
public String nextLine() {
String result = "";
do {
sem.P();
if (tempChar != '\n') result += tempChar;
} while(tempChar != '\n');
return result;
}
public char getch() {
sem.P();
return tempChar;
}
}
Main.java
public class Main {
private Console console;
public Main() {
console = new Console();
char c = console.getch();
System.out.println(c);
}
}
Is there anything I have missed or is there any way to programmatically press Enter
key or something?
PS: java.awt.Robot
can't be used inside a nachos project.
Any help would be appreciated.
Upvotes: 0
Views: 185