473,386 Members | 1,647 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

using filedescriptors in SIGINT signal handler

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
Sep 13 '05 #1
2 3069
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)

iD8DBQFDJs39Jd01MZaTXX0RApghAJ9SRtNridVQTj2qku5aVi uW+eM5jQCgq80q
c45b9r6vdrNHcbWtCGz+C3M=
=XpWr
-----END PGP SIGNATURE-----

Sep 13 '05 #2
je****@unpythonic.net wrote:
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

Jeff

Sep 13 '05 #3

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

Similar topics

2
by: Gary Robinson | last post by:
In some code we're writing we're making an assumption, and I'd like to confirm that the assumption is valid. Suppose a signal arrives while a file is being written, and the signal handler...
0
by: Keith | last post by:
I got this to work, but was hoping there was a better way. I was hoping that I wouldn't have to use globals. I wouldn't doubt that I could be totally off in what I am doing. Here is some...
0
by: rh0dium | last post by:
Hi all, I don't understand the signal module. I guess I understand what it does in principle but I can't figure out how to use it to timeout an external rsh command after a 5 seconds. Does...
3
by: rh0dium | last post by:
Hi all, Another newbie question. So you can't use signals on threads but you can use select. The reason I want to do this in the first place it I need a timeout. Fundamentally I want to run a...
4
by: Eric Boutin | last post by:
Hi ! currently reading C docs, I think I'm reading docs about the stdc lib, but I'm not shure.. it talks about signals, does *all* OS implement the signal function, and use it well ? I mean.. I...
20
by: bubunia2000 | last post by:
Hi all, I heard that strtok is not thread safe. So I want to write a sample program which will tokenize string without using strtok. Can I get a sample source code for the same. For exp:...
5
by: david | last post by:
I'm developing a program that runs using an asyncore loop. Right now I can adequately terminate it using Control-C, but as things get refined I need a better way to stop it. I've developed...
1
by: =?ISO-8859-2?Q?Grzegorz_Kokosi=F1ski?= | last post by:
Hi! I waste a lot of my time figuring why this snip of code doesn`t work. And still without any solution ;/ Does anybody know why this handler cannot handle SIGINT signal? What did I wrong? ...
3
by: pradeep | last post by:
Hello again friends: I'm now trying to get a signal handler to work. I have made the example code below to check I can make it work, but there is a compiler error, argument of type ‘void...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.