Interactive AND Portable?

D

Dave Vandervies

... if you have a reliable way of generating SIGINT
(ctrl-C should do this on both Unix and Windows), you can fake
[interactivity].

but then:
oldsigfunc=signal(SIGTERM,sigint_handler); [and]
fputs("SIGTERM was ignored (perhaps we're running in the
background?), re-ignoring\n",stderr);
signal(SIGTERM,SIG_IGN);

Presumably these are all supposed to read SIGINT as well. :)

Yep. I blame it on having too much blood in my caffeine stream when
I wrote that. (You wouldn't want to run your "interactive attention"
handler when the system is requesting that your program terminate.)

Not quite on topic for the group, but perhaps significant, there
is a pitfall in using an interrupt key on Unix-like systems (whether
it applies to MS-Windows is less clear -- I try to avoid Windows
as much as possible and in any case Windows systems are not commonly
used with remote displays half a world away over long-response-time
TCP connections...). In particular, interrupting a program may
flush queued but as-yet-undisplayed output, at the OS level. There
is nothing a portable C program can do about this.

Would this cause problems for a program that only sets a flag in the
signal handler and does the actual input handling (suspending normal
execution of the program while doing so) elsewhere when that flag is set?


dave
 
C

Chris Torek

[Off-topic, but: ^C flushes output on some Unix-ish systems.] There
is nothing a portable C program can do about this.

Would this cause problems for a program that only sets a flag in the
signal handler and does the actual input handling (suspending normal
execution of the program while doing so) elsewhere when that flag is set?

No (and still unfortunately off-topic). The control for whether
interrupt flushes output is (optionally) in the POSIX "c_lflag"
field of a "termios" structure. From the BSD/OS manual page:

Local Modes
Values of the c_lflag field describe the control of various functions,
and are composed of the following masks.
...
If NOFLSH is set, the normal flush of the input and output queues associ-
ated with the INTR, QUIT, and SUSP characters are not be done.

(grammatical error and all).

(The presumption is that when you type ^C, you want any text spewing
out at you to cease right away. The discard happens at the OS
level, not the C stdio level.)
 
S

SeeBelow

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 "interacting" 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 for all the advice. I've decided to go with a file-based
system. That will give me both functionality and portability. My
running program will periodically see if it can open a file with a
specific name. If not, it just keeps running. If if can, it sets a
flag and closes the file, or it can read some data from the file before
closing it. It might also delete the file.
Both Windows and Linux provide multiple ways in which a file can be made
to appear in the directory. If complex interaction is needed, then a
separate program using a loop with scanf() or equivalent can create and
destroy the file.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,072
Latest member
trafficcone

Latest Threads

Top