Dear friends,
I want to extend the interrupt handler of a program, my code is: -
...
-
if ($pid = fork) {
-
local $SIG{INT} = sub {
-
print "Please select:\n";
-
$choice = <STDIN>;
-
if ($choice eq "a") {
-
&sub_routine();
-
}
-
else {
-
kill INT => $pid; # send signal to child by parent selectively
-
}
-
};
-
}
-
else {
-
exec("origninal.pl"); # original program, have its own INT handler
-
...
-
}
-
But when I hit ctrl+c, both parent and child process receive INT, the
choice doesn't work.
How can I make child process only accept signal from parent?
Thanks a lot!
3 2283 miller 1,089
Recognized Expert Top Contributor
Greetings Joel,
I'm not a fork expert, nor do I have a lot of experience working with signals. However there are a couple obvious bugs in your problem.
First off, the INT handler that you created for the parent process will never actually be used. Use rightly use local in that assignment, but that is all you do in the parent if statement. This means $SIG{INT} handler is reverted back to it's original value immediately after the assignment because the if block is left. Most likely you need a wait command, but this is the point where my ipc knowledge becomes only perfunctory.
Also, your test for your choice will never match because you do not chomp $choice.
There are other improvements that I would advise, but I'll leave it there for now.
- Miller
Dear Miller and other friends, Thank you for the work that made my question clear, also I find that
you are very precise. One of the improvments you mentioned might be checking whether
fork is successful, to make this piece of code not pseudo-code like,
I will add it. Seems the original post can't be modified :-( -Joe
miller 1,089
Recognized Expert Top Contributor
Hello Joel,
Yes, fork and process programming is definitely one of those areas where it is in your best interest to never skimp on error checking. You should definitely add the check for if $pid is defined. As that is the first thing that I would do if I was to refactor your code.
Anyway, good luck with your project, and feel free to ask further questions if you hit a block again.
- Miller
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Rookie |
last post by:
Hi,
I have a program that fork()'s + execlp()'s two children. Later in the
program I want to send a signal to the parent(SIGINT) . The parent's signal
handler should then send signals to these two children. I am able to catch
the signal in the parent - on catching this signal I invoke a function that
sends kill(childPid,SIGINT) but the signal handler in the child doesn't
seem to be getting invoked. Can someone please tell me why this is...
|
by: Steve Jorgensen |
last post by:
Yup, Steve's full of tips, but hey, it makes him feel important, right?
Ok, here goes. I've been trying to improve encapsulation by putting code
in the same object as the stuff it affects, so I rarely refer to a subform
ir parent form's controls or records from the other form. Instead, I call
a procedure in that form that does the job.
That's all fine and good, and nothing at all revolutionary, but it leaves
one with an annoying...
|
by: gnutuxy |
last post by:
Hi,
I am newbie in the Unix system programming and in learning phase.
I usually read the libc manual and then try to implement small
programs to test/check the learnt thing.
I read the libc manual for signals and tried the program to check
whether child
inherits the signal handler intalled by parent befor fork.
|
by: foolmelon |
last post by:
If a childThread is in the middle of a catch block and handling an
exception caught, the main thread calls childThread.Abort(). At that
time a ThreadAbortException is thrown in the childThread. The question
is: after the ThreadAbortException is thrown, does the childThread
continue run the remaining code in the catch block?
|
by: Adrian Casey |
last post by:
I have a multi-threaded python application which uses pexpect to connect to
multiple systems concurrently. Each thread within my application is a
connection to a remote system. The problem is when one of the child
threads runs a command which generates an unlimited amount of output. The
classic example of this is the "yes" command. If you
execute "pexpect.run('yes')", your cpu will sit at 100% forever.
Here is a simple multi-threaded...
| |
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 another
program that executes it as a child process using popen2.Popen4(). I
was attempting to use signals to stop it (using os.kill()) but I keep
running into a problem where sending the signal causes an infinite
loop of printing the message...
|
by: kunalgalav |
last post by:
Hi ,
I am a newbie to python and I am trying to send a signal to my application using a thread.
Here's my code:
import sys
from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import *
class MyThread(QtCore.QThread,QtCore.QObject):
def __init__(self, parent):
|
by: GTalbot |
last post by:
Hello fellow authoring.stylesheets colleagues,
Can someone please explain why the bottom margin of the last inflow
block-level child in an overflowed parent should not have its margin
reachable. I read and re-read carefully the CSS 2.1 spec and I just
can not see why.
Related webpages:
http://www.gtalbot.org/BrowserBugsSection/MSIE8Bugs/MarginBottomChildNotRendered.html
|
by: Iain |
last post by:
Hi,
I have a function that only accepts filenames, rather than file-like
objects (because its a wrapper around a C++ function, I think).
I want to feed some potentially large "files" into this function, but
they are coming in as streams (eg from a url) and so are only
represented in my code as file-like objects.
Can someone give me some pointers as to how I might create some sort
|
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,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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...
|
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...
|
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...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |