Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 06-03-2009, 10:33 PM   #1
Registered: Dec 2001
Location: thunder bay, on, Canada
Distribution: Windows vista & Ubuntu
Posts: 113

Rep: Reputation: 15
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.

Last edited by whepin; 06-03-2009 at 10:44 PM.
Old 06-04-2009, 04:32 AM   #2
Registered: Dec 2005
Location: Sheffield, UK
Distribution: Gentoo
Posts: 142

Rep: Reputation: 32
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
Old 06-04-2009, 10:23 PM   #3
Registered: Dec 2001
Location: thunder bay, on, Canada
Distribution: Windows vista & Ubuntu
Posts: 113

Original Poster
Rep: Reputation: 15
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
while (1)
////serial operation and output to the console
Old 06-05-2009, 12:11 AM   #4
Registered: Sep 2007
Posts: 42

Rep: Reputation: 15
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.
Old 06-05-2009, 04:18 AM   #5
Senior Member
Registered: Mar 2004
Location: england
Distribution: Debian, Mint, Puppy, Raspbian
Posts: 3,421

Rep: Reputation: 200Reputation: 200Reputation: 200
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.
maybe curses isn't suitable for this.
Are you using it just for the wgetch?
try select or termios


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
INIT: open(/dev/console): Input/output error pazkaw Slackware 13 03-25-2008 06:15 PM
how do you make a poll so user's can vote on stuff samuelmp Linux - General 2 02-07-2007 03:28 PM
INIT: open(/dev/console): Input/output error nykey Slackware 12 09-21-2006 04:30 PM
open(/dev/console): Input/Output error fintanmcevoy General 2 11-18-2005 07:44 AM
Sending message to a user's console GUIPenguin Linux - General 1 10-27-2005 12:38 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:39 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration