How to scan user's input and output some stuff to the console at the same time?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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
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
}
}
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.