473,804 Members | 3,675 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Interactive AND Portable?

Is there any way, in C, of interacting with a running program, but still
using code that is portable, at least between linux and Windows?

By "interactin g" it could be something as simple as detecting a key
board hit so that it could invoke scanf(). So a portable key board data
available routine would satisfy my minimum requirements. But more
generally I want to know if one can write portable C code that allows a
user to change the course of execution of a running program.

Thanks,

Mitchell Timin

--
"Many are stubborn in pursuit of the path they have chosen, few in
pursuit of the goal." - Friedrich Nietzsche

http://annevolve.sourceforge.net is what I'm into nowadays.
Humans may write to me at this address: zenguy at telus dot net
Nov 13 '05 #1
22 2293
Se******@seebel ow.nut scribbled the following:
Is there any way, in C, of interacting with a running program, but still
using code that is portable, at least between linux and Windows? By "interactin g" it could be something as simple as detecting a key
board hit so that it could invoke scanf(). So a portable key board data
available routine would satisfy my minimum requirements. But more
generally I want to know if one can write portable C code that allows a
user to change the course of execution of a running program.


No. The moment you do anything at all directly with the keyboard, you're
off to OS-specific land, and you have no hope of the same code working
on Linux and Windows.
What's wrong with using the standard input stream? In most cases that is
coming from the keyboard. It's just a matter of having to press Enter.

--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"I am looking for myself. Have you seen me somewhere?"
- Anon
Nov 13 '05 #2
On Thu, 30 Oct 2003 20:22:35 GMT, Se******@SeeBel ow.Nut wrote:
Is there any way, in C, of interacting with a running program, but still
using code that is portable, at least between linux and Windows?
Yes, if you stick with the stdio I/O functions (i.e. scanf(), printf(), and the
like), you can easily write a portable interactive program.
By "interactin g" it could be something as simple as detecting a key
board hit so that it could invoke scanf().
Now you've taken yourself out of the world of portability.
1) C doesn't have any portible way of "detecting a keyboard hit"
(the C language doesn't even recognize such a thing), and
2) In Linux (at least), an "interactiv e" program may be run /without/ a
recognizable 'keyboard' at all (thinks "expect" scripts, or just plain
scripting). To require the code to "detect a keyboard hit" is to make
it unportable.
So a portable key board data
available routine would satisfy my minimum requirements.
Such exists, in the form of the standard I/O routines like scanf() and getc()
But more
generally I want to know if one can write portable C code that allows a
user to change the course of execution of a running program.


Yes, of course. Use the stdio functions.

--
Lew Pitcher
IT Consultant, Enterprise Technology Solutions
Toronto Dominion Bank Financial Group

(Opinions expressed are my own, not my employers')
Nov 13 '05 #3
Joona I Palaste wrote:

Se******@seebel ow.nut scribbled the following:
Is there any way, in C, of interacting with a running program, but still
using code that is portable, at least between linux and Windows?

By "interactin g" it could be something as simple as detecting a key
board hit so that it could invoke scanf(). So a portable key board data
available routine would satisfy my minimum requirements. But more
generally I want to know if one can write portable C code that allows a
user to change the course of execution of a running program.


No. The moment you do anything at all directly with the keyboard, you're
off to OS-specific land, and you have no hope of the same code working
on Linux and Windows.
What's wrong with using the standard input stream? In most cases that is
coming from the keyboard. It's just a matter of having to press Enter.


I guess I wasn't clear; I don't want the program to be doing nothing
while waiting for an "enter" from the KB. My program may be running for
hours, doing simulated evolution. I need to occaisonally give it some
data, or make a decision for it. I don't mind if it pauses while
interacting with me, but after that it must continue processing.

Is there a way of doing that with the standard input stream?

m

--
"Many are stubborn in pursuit of the path they have chosen, few in
pursuit of the goal." - Friedrich Nietzsche

http://annevolve.sourceforge.net is what I'm into nowadays.
Humans may write to me at this address: zenguy at telus dot net
Nov 13 '05 #4
Lew Pitcher wrote:

On Thu, 30 Oct 2003 20:22:35 GMT, Se******@SeeBel ow.Nut wrote:
Is there any way, in C, of interacting with a running program, but still
using code that is portable, at least between linux and Windows?


Yes, if you stick with the stdio I/O functions (i.e. scanf(), printf(), and the
like), you can easily write a portable interactive program.
By "interactin g" it could be something as simple as detecting a key
board hit so that it could invoke scanf().


Now you've taken yourself out of the world of portability.
1) C doesn't have any portible way of "detecting a keyboard hit"
(the C language doesn't even recognize such a thing), and
2) In Linux (at least), an "interactiv e" program may be run /without/ a
recognizable 'keyboard' at all (thinks "expect" scripts, or just plain
scripting). To require the code to "detect a keyboard hit" is to make
it unportable.
So a portable key board data
available routine would satisfy my minimum requirements.


Such exists, in the form of the standard I/O routines like scanf() and getc()
But more
generally I want to know if one can write portable C code that allows a
user to change the course of execution of a running program.


Yes, of course. Use the stdio functions.


Can that be done without the program pausing? I guess I wasn't clear; I
don't want the program to be doing nothing while waiting for an "enter"
from the KB. My program may be running for hours, doing simulated
evolution. I need to occaisonally give it some data, or make a decision
for it. I don't mind if it pauses while interacting with me, but after
that it must continue processing.

Is there a portable way of doing that?

Mitchell Timin

--
"Many are stubborn in pursuit of the path they have chosen, few in
pursuit of the goal." - Friedrich Nietzsche

http://annevolve.sourceforge.net is what I'm into nowadays.
Humans may write to me at this address: zenguy at telus dot net
Nov 13 '05 #5
Se******@seebel ow.nut scribbled the following:
Joona I Palaste wrote:
Se******@seebel ow.nut scribbled the following:
> Is there any way, in C, of interacting with a running program, but still
> using code that is portable, at least between linux and Windows?
> By "interactin g" it could be something as simple as detecting a key
> board hit so that it could invoke scanf(). So a portable key board data
> available routine would satisfy my minimum requirements. But more
> generally I want to know if one can write portable C code that allows a
> user to change the course of execution of a running program.


No. The moment you do anything at all directly with the keyboard, you're
off to OS-specific land, and you have no hope of the same code working
on Linux and Windows.
What's wrong with using the standard input stream? In most cases that is
coming from the keyboard. It's just a matter of having to press Enter.

I guess I wasn't clear; I don't want the program to be doing nothing
while waiting for an "enter" from the KB. My program may be running for
hours, doing simulated evolution. I need to occaisonally give it some
data, or make a decision for it. I don't mind if it pauses while
interacting with me, but after that it must continue processing. Is there a way of doing that with the standard input stream?


It took me some time to figure out what you want the program to do.
You say you want your program to be *both* processing its data *and*
looking for input from you at the same time? I don't think that it's
possible within the standard C I/O model.
Standard C input functions are blocking. If there is not enough data
available in the stream they are reading from, they block until there
is. If the stream is closed, they return immediately with an error
code.
Therefore your program would need to either wait every now and then
for you to give it input - or process a "batch" of input you've
given it in advance. All it wants is a stream of bytes. It doesn't
care where that stream is coming from.
If C had multi-threading support as standard, you could have one
thread processing the data and another doing blocking I/O, but as it
doesn't, you can't.
So, unfortunately, the answer is no: you can't do what you ask with
standard I/O. Your program must either block while accepting input
or process ready-written input without ex-tempore additions.

--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"There's no business like slow business."
- Tailgunner
Nov 13 '05 #6
Se******@SeeBel ow.Nut wrote in news:3F******** *******@telus.n et:
Yes, of course. Use the stdio functions.
Can that be done without the program pausing?


No. You probably want to use threads. Try an appropriate OS newsgroup that
discusses threads. It is quite easy with threads.
Is there a portable way of doing that?


No.

--
- Mark ->
--
Nov 13 '05 #7
In article <3F************ ***@telus.net>, <Se******@SeeBe low.Nut> wrote:
I guess I wasn't clear; I don't want the program to be doing nothing
while waiting for an "enter" from the KB. My program may be running for
hours, doing simulated evolution. I need to occaisonally give it some
data, or make a decision for it. I don't mind if it pauses while
interacting with me, but after that it must continue processing.

Is there a way of doing that with the standard input stream?
Not directly, but if you have a reliable way of generating SIGINT
(ctrl-C should do this on both Unix and Windows), you can fake it.

You'll need a flag that the signal handler will set to indicate that
the user wants to talk to the program:
--------
#include <signal.h>
volatile sig_atomic_t stop_and_get_in put;
--------

You'll need a signal handler that looks something like this:
--------
#include <signal.h>

void sigint_handler( int sig)
{
stop_and_get_in put=1;

/*Reinstate the signal handler. May or may not be required
(N869 7.14.1.1#3), but causes no problems if done when not
required (N869 7.14.1.1#5).
*/
#ifdef PARANOID
/*Set failed_to_reins tate_handler (which should be a volatile
sig_atomic_t) to indicate that something went wrong and we
won't be able to request an interactive session again
*/
if(signal(sig,s igint_handler) == SIG_ERR)
failed_to_reins tate_handler=1;
#else /*Not paranoid, so just assume reinstatement succeeded*/
signal(sig,sigi nt_handler);
#endif
}
--------

Before you enter your main loop, install the signal handler like this:
--------
/*Near the beginning of main is probably the best place to put this.
Don't forget to #include <signal.h>.
*/
void (*oldsigfunc)(i nt);
oldsigfunc=sign al(SIGTERM,sigi nt_handler);
if(oldsigfunc == SIG_ERR)
{
fputs("Can't install signal handler - user interaction requests are unavailable\n", stderr);
}
else if(oldsigfunc == SIG_IGN)
{
fputs("SIGTERM was ignored (perhaps we're running in the background?), re-ignoring\n",std err);
signal(SIGTERM, SIG_IGN);
}
--------

Now when your program fgets the SIGINT signal (usually as a result
of getting a ctrl-C at the keyboard), the signal handler will run and
set stop_and_get_in put to indicate that this happened, so now you can
put something like this inside your main loop:
--------
/*This should go somewhere deep enough in the main loop to run within
a reasonable amount of time after the signal handler runs, but not
deep enough to slow down the program by running millions of times
a second or to run in the middle of something that you don't want
to be changing parameters in the middle of.
*/
if(stop_and_get _input)
{
do_user_input() ;
stop_and_get_in put=0;
}
--------

The program won't run in the background while do_user_input() is running,
so you'll need to have a way for the user to tell it "OK, I'm done, go
back to crunching and I'll interrupt you again if I have more to say".
It would also be nice for it to have a way to exit the program without
running to completion, since the usual response to SIGINT is for the
program to do that (without stopping and bothering the user first).
One thing that may not work, and is a Bad Idea anyways, is to have the
signal handler call do_user_input directly. Since standard C doesn't
require that you be able to call library functions from a signal handler
(with a few exceptions, like reinstating the signal handler or installing
a new one for the same signal), you can't do this without invoking
undefined behavior; also, at least one rather common system (Windows)
will run the signal handler in its own thread while the main program
continues to run, forcing you to deal with concurrency issues if you
change anything that the program wants to be able to use.
dave

--
Dave Vandervies dj******@csclub .uwaterloo.caWrong.

May I be excused for showing incomplete/incorrect knowledge of C++ knowledge
in a C newsgroup? --Michael Rubenstein and Ian Woods in comp.lang.c
Nov 13 '05 #8
In article <Xn************ *************** *****@130.133.1 .4>,
no****@embedded fw.com says...
Se******@SeeBel ow.Nut wrote in news:3F******** *******@telus.n et:
Yes, of course. Use the stdio functions.
Can that be done without the program pausing?


No. You probably want to use threads. Try an appropriate OS newsgroup that
discusses threads. It is quite easy with threads.


Yes. comp.programmin g.threads is a fine place to start.
Is there a portable way of doing that?


No.


If you are willing to expand the concept of portability to POSIX
standards instead of ISO, for example wanting to run on both Linux
and Win32, then pthreads will do it for you. Just be aware that
moving to 64-bit from 32-bit on Windows may or may not be ready w/
respect to pthreads when your time comes to do the port.

--
Randy Howard _o
2reply remove FOOBAR \<,
_______________ _______()/ ()_____________ _______________ _______________ ___
SCO Spam-magnet: po********@sco. com
Nov 13 '05 #9
Joona I Palaste wrote:
<snip>
I guess I wasn't clear; I don't want the program to be doing nothing
while waiting for an "enter" from the KB. My program may be running for
hours, doing simulated evolution. I need to occaisonally give it some
data, or make a decision for it. I don't mind if it pauses while
interacting with me, but after that it must continue processing.

Is there a way of doing that with the standard input stream?


It took me some time to figure out what you want the program to do.
You say you want your program to be *both* processing its data *and*
looking for input from you at the same time? I don't think that it's
possible within the standard C I/O model.
Standard C input functions are blocking. If there is not enough data
available in the stream they are reading from, they block until there
is. If the stream is closed, they return immediately with an error
code.
Therefore your program would need to either wait every now and then
for you to give it input - or process a "batch" of input you've
given it in advance. All it wants is a stream of bytes. It doesn't
care where that stream is coming from.
If C had multi-threading support as standard, you could have one
thread processing the data and another doing blocking I/O, but as it
doesn't, you can't.
So, unfortunately, the answer is no: you can't do what you ask with
standard I/O. Your program must either block while accepting input
or process ready-written input without ex-tempore additions.


A C program can look for a file, and if it won't open, then it can
continue processing without blocking. Is there a way to do that using
the keyboard stream?

I could actually do this by using a file as a messenger. I would have
to have a seperate program running concurrently, which both Windows and
linux can do. This second program would use scanf() to get user input,
and then write a file. The primary program would be periodically trying
to open this file. When it succeeds it gets the data and then closes
the file. Otherwise it continues processing.

Is there some way to do the same thing by treating the keyboard stream
as a file?

Mitchell Timin

--
"Many are stubborn in pursuit of the path they have chosen, few in
pursuit of the goal." - Friedrich Nietzsche

http://annevolve.sourceforge.net is what I'm into nowadays.
Humans may write to me at this address: zenguy at telus dot net
Nov 13 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
2455
by: Joe | last post by:
When you run "python -i scriptname.py" after the script completes you left at the interactive command prompt. Is there a way to have this occur from a running program? In other words can I just run scriptname.py (NOT python -i scriptname.py) and inside of scriptname.py I decide that I want to fall back to the interactive prompt? I've searched and so far the only thing I've come up with is to use pdb, but
2
7814
by: Charles Krug | last post by:
List: I'm trying to us pylab to see what I'm doing with some DSP algorithms, in case my posts about convolution and ffts weren't giving it away. I've been using pylab's plot function, but I'm finding it a bit cumbersome. It works, but if I switch from the interactive window to the plot window and back, the plot window gets trashed.
2
1706
by: Allan Adler | last post by:
In C, I can execute system("gs gleep.ps"); and have C run a ghostscript program gleep.ps, which might ask for user input and, when it gets it, take some actions and report back to the C program. I think I can probably handle that last step by having ghostscript write to a file and have the C program read the file. That's ok for one interactive program. Suppose I want to have two or more
1
329
by: Nick Palmer | last post by:
As the title suggests, I've got a question about running an ASP.NET app on a server that has no interactive desktop login. In all our testing of our app here,the server always had an interactive desktop login since we were watching things on it, etc. Well I went to test it the way I would assume our customers are going to run it, that is with no interactive desktop login, and I get my initial page, but then I can't doing anything else. Our...
2
2014
by: WJ | last post by:
I have three ASPX pages: 1. "WebForm1.aspx" is interactive, responsible for calling a web site (https://www.payMe.com) with $$$. It is working fine. 2. "WebForm2.aspx" is non-interactive, a listener in my site to capture a "STATUS_CD" returned by www.payMe.com. It is working fine. 3. "WebForm3.aspx" is interactive page, responsibe to display the STATUS CODE "POSTED" by "WebForm2.aspx". 4. My goal is to use "WebForm2.aspx" to call...
131
6251
by: pemo | last post by:
Is C really portable? And, apologies, but this is possibly a little OT? In c.l.c we often see 'not portable' comments, but I wonder just how portable C apps really are. I don't write portable C code - *not only* because, in a 'C sense', I
3
4615
by: Lubomir | last post by:
Hi, I read that interactive windows services will not be allowed to run under Wista. I would like to ask what exactly is the interactive service. It is supposed to be a service that communicates with desktop. What about the following scenario: A service will create a thread. This thread will create and run an UI Windows application that manages task bar and so on on a desktop. This application will communicate with the service: a/ by...
13
3362
by: dmh2000 | last post by:
I am experimenting with the interactive interpreter environments of Python and Ruby and I ran into what seems to be a fundamental difference. However I may be doing something wrong in Python. Please comment and correct me if I am wrong In both languages, you can start up the interactive interpreter ('python' and 'irb'), load source files and do stuff, like create objects and call their methods. When you want to change something, you can...
8
5587
by: james.kirin39 | last post by:
Hi everyone, After having used Python on Linux for some time, I now have to do Python coding on Windows. I am big fan of the interactive Python shell to test, eg, regexps. Is there an interactive Python shell on Windows that supports: - easy copy-pasting to/from an editor? (as opposed to the cumbersome "mark", "copy" and then "paste" sequence that any terminal on Windows
0
9707
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9585
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10082
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9161
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7622
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6856
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4301
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2997
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.