using filedescriptors in SIGINT signal handler

B

Bram Stolk

Hello,

I catch SIGINT signals with a handler.
In the handler, I use fd's to write some data to a child process.

Is this valid?
Because the data never arrives, and I wonder what I'm doing wrong.

Can filedescriptors still be used when you're in the signal
handling of SIGINT?

Thanks,

Bram
 
J

jepler

If you're talking about a Python function registered as a handler by
signal.signal, then there should not be any restrictions on what you do
in that function.

Here's a small program I wrote:
#------------------------------------------------------------------------
import os, signal, time

def h(*args): os.write(fd, "data\n");

print "my pid is", os.getpid()
subproc = os.popen("cat -n", "w")
fd = subproc.fileno()

signal.signal(signal.SIGINT, h)

while 1:
time.sleep(1)
#------------------------------------------------------------------------

I ran this and in another terminal I repeatedly typed 'kill -INT nnnn',
where nnnn is the pid printed by my program. Each time, another line is
output by 'cat'.

When I try to deliver the signal by hitting ctrl-c in that terminal, the
first time nothing happens and the second time I get the message
OSError: [Errno 32] Broken pipe
in this case, I believe that the first signal was delivered to cat,
causing it to exit. The second signal was delivered to the python
program, which obviously couldn't write to the stdin of a process that
had exited.

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)

iD8DBQFDJs39Jd01MZaTXX0RApghAJ9SRtNridVQTj2qku5aViuW+eM5jQCgq80q
c45b9r6vdrNHcbWtCGz+C3M=
=XpWr
-----END PGP SIGNATURE-----
 
B

Bram Stolk

If you're talking about a Python function registered as a handler by
signal.signal, then there should not be any restrictions on what you do
in that function.

Here's a small program I wrote:
#------------------------------------------------------------------------
import os, signal, time

def h(*args): os.write(fd, "data\n");

print "my pid is", os.getpid()
subproc = os.popen("cat -n", "w")
fd = subproc.fileno()

signal.signal(signal.SIGINT, h)

while 1:
time.sleep(1)
#------------------------------------------------------------------------

I ran this and in another terminal I repeatedly typed 'kill -INT nnnn',
where nnnn is the pid printed by my program. Each time, another line is
output by 'cat'.

When I try to deliver the signal by hitting ctrl-c in that terminal, the
first time nothing happens and the second time I get the message
OSError: [Errno 32] Broken pipe
in this case, I believe that the first signal was delivered to cat,
causing it to exit. The second signal was delivered to the python
program, which obviously couldn't write to the stdin of a process that
had exited.

Ah!
ofcourse...

It makes sense now to me.
I, indeed, used Ctrl-C, and assumed it would go to python, not
the child process. Wrong assumption ofcourse.

Thanks.

Bram
 

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top