Page 1 of 1

Keyboard Buffer Synchronozation

Posted: Wed Feb 02, 2005 12:00 am
by prajwal
I'v a problem in my OS code.

scenario :

1) There is a Keyboard Queue Buffer and a variable called 'sync' - Global
2) The Keyboard handler will put the read character(after scancode conversion) into this Queue Buffer
3) There is a blocking function getChar() which reads a character from Queue Buffer.
4) This function polls the Queue Buffer in a tight loop until it reads a character and then returns


Problem: The key pressed will echo on screen but with unpredictable output.... means junk chars, previous character
got from previous IRQ9 will also get displayed..... as a whole display is not proper...



a) Queue Read and Write are correct(tested)
b) Display function is Correct(tested)


pseuodo Code:

getChar()
{
while(readFromQueue() == success) ;
}

readFromQueue()
{
sync = 1

..... QUEUE read operation ......

sync = 0
}

keyboard_handler()
{
... All keyboard port related stuff

writeToBuffer() ;

..... IRET etc.....
}

writeToBuffer()
{
if(sync == 1)
return ;

..... QUEUE write operation ......

return ;
}

But if I put a waiting loop in getChar() like:
getchar()
{
while(readFromQueue() == success)
{
for(i=0;i<999;i++) ;

}
}

Then everything works fine.....

NOTE: I know that if sync = 1 and IRQ9 comes... the keyed in character is lost... but thats fine....

Please give a soluation or an alternative approach....

Re: Keyboard Buffer Synchronozation

Posted: Wed Feb 02, 2005 12:00 am
by proxy
better to use semaphores instead of polling/spinlocks. what i do is first i made my getch() function atomic so that no keyboard interrupt may occur while i'm getting a character (in non-smp this means interrupts off for this thread). Then i have a global semaphore initialized to 0 so first p() operation will block (and implicitly yield the thread which is important since we wont get a timer interrupt). Now whenever my kyeboard interrupt occurs i do a v() on the global lock after i add the data to the queue. Now this makes the thread that asked for the char runnable again so when it gets scheduled it will resume with interrupts still off and pop the value out of the queue....everyone is happy :)

proxy

Re: Keyboard Buffer Synchronozation

Posted: Sun Feb 06, 2005 12:00 am
by Da_Maestro
writeToBuffer()
{
if(sync == 1)
return ;

..... QUEUE write operation ......



Shouldn't that be a while loop not an if statement.

No wait, that will junk the whole thing.

What should happen in your code is the write method should set sync to 1 and then zero when finished, and the read method should spin ONLY IF SYNC IS ONE