LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to scan user's input and output some stuff to the console at the same time? (https://www.linuxquestions.org/questions/programming-9/how-to-scan-users-input-and-output-some-stuff-to-the-console-at-the-same-time-730546/)

whepin 06-03-2009 09:33 PM

How to scan user's input and output some stuff to the console at the same time?
 
Now I'm writing a console program. It needs to process user's input(such as some keystroke as commands) and at the same receives some data via serial ports and outputs them to the console.

I design the program using ncurses and thread library. I create two or three windows(using ncurses's newwin). One is for user's input, the other is for serial data output, the last one is for some status.

In addtion, I create two processes. One is for processing user's input, the other is for receiving data from serial ports.

But the question is , It doesnot runs well as I expected. When a process output some stuff to the console, the other process seems that it cannot grap the cpu and process my keystrokes?

I'm really confused. And I've tried a lot of methods and still can't get it working?

So anyone could help me? Any advice is welcome.

Thank you very much.

pgpython 06-04-2009 03:32 AM

The problem you have may well be expected. It highly depends on wether the first command is considered to be a atomic instruction if that is the case then there is nothing you can do to have the other process perform its command at the same time however if that is the case i expect your problem lies with the fact its being slowed down somewhere as if outputting typically takes a few nanoseconds as well catching the input would you notice the fact that they do perform simulteanously? Without seeing the code its difficult to say whats hogging the cpu. You could try some timing commands to see how long certain things are taking. It may be the case that you need to get one of the processes or both to sleep for a time if its in a infinite loop

whepin 06-04-2009 09:23 PM

Yeah, thank you very much. You are right. If I let them sleep for some seconds , then it's ok.
But, you know, every 20 or 30 ms, I'll get data from the serial ports. So it's not possible for both of the process to sleep in my opinion.

So, are there any ideas?
Thanks again.

The following is the skeleton of my code.

pid_t child_pid = fork ();
//parent process
if (child_pid != 0)
{
while (1)
wgetch (topwin); //capture user's keystroke

}
//child process
else
{
while (1)
{
////serial operation and output to the console
}
}

jdiggitydogg 06-04-2009 11:11 PM

why don't you use 1 process & then use select() on multiple file descriptors. see 'man 2 select' for more information and an example. there's also a great example at 'man 2 select_tut'. I don't know if select is exactly what you want, but its worth looking at if you aren't familiar with it.

bigearsbilly 06-05-2009 03:18 AM

well first off,
as is normally the case,
I'd close stdin on the writer and stdout on the reader.

I suspect there is a problem somewhere in your control logic.
or
maybe curses isn't suitable for this.
Are you using it just for the wgetch?
try select or termios


All times are GMT -5. The time now is 02:16 AM.