Sponsored Content
Full Discussion: Controlling child processes
Top Forums Programming Controlling child processes Post 49131 by Driver on Thursday 25th of March 2004 03:54:31 PM
Old 03-25-2004
> I have to guarantee that when I send the
> command down the pipe and then the signal,
> this is all done atomically

I assume that by ``atomically'', you mean that every receipt of a signal indicates the availability of exactly one new command and that no signals can be lost.

First of all, you should be familair with signal handling in general. If you are not, you should read the sigaction(2) manual help page to begin with. Another question is what your children are doing while not serving commands of the parent. Because of the very limited things you can do within a signal handler (usually, you should not do more than set a flag), the way you handle requests will depend greatly upon this.

If they are not doing anything, you could simply call pause(), install a signal handler which does not restart interrupted system calls (sa_flags = 0 with sigaction()) and handle everything after pause() returns with errno = EINTR. Otherwise, you would have to integrate some kind of flag into your child's main loop which is checked on a regular basis and which is set by the signal handler.

> I would also like to use semaphores to guard
> this section of the code, I do not want to
> travel the disabling interrupts route or busy waiting.

The signal being handled can be blocked while your signal handler is executing, but this could lead to signal loss if the parent sends more than one signal before the child gets to handle it. If your target operating system(s) support(s) the POSIX realtime extension function sigqueue(), you could use that instead of bothering with semaphores to avoid loss (alas, the various BSD's do not have this function, Linux and UNIX(R) branded systems do, however).

Of course write()'s of less than 512 bytes blocks by the parent are guaranteed to take place atomically already, so you could equally well ignore lost signals and read all commands there are whenever you receive a signal.

> If this possible can someone point me to
> some sample code or resources for all
> tasks, creation of pipes and writing
> commands to it, signaling the process and
> using semaphores.

This page seems like a good overview:

http://www.cs.cf.ac.uk/Dave/C/CE.html

I also recommend the excellent book ``Advanced Programming in the UNIX Environment'' by Richard Stevens.
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Controlling processes knowing the PID's

Dear all, suppose that I start a process (named "father"). "father" starts in turns a process called "child" with an execv call (after a fork). In this way "father" will be notified if "chlid" crashes (SIGCHILD mechanism). The problem is: if "father" crashes, how can I do to be recreate a... (1 Reply)
Discussion started by: npalmentieri
1 Replies

2. UNIX for Dummies Questions & Answers

what are parent and child processes all about?

I don't follow what these are... this is what my text says... "When a process is started, a duplicate of that process is created. This new process is called the child and the process that created it is called the parent. The child process then replaces the copy for the code the parent... (1 Reply)
Discussion started by: xyyz
1 Replies

3. Shell Programming and Scripting

Parent/Child Processes

Hello. I have a global function name func1() that I am sourcing in from script A. I call the function from script B. Is there a way to find out which script called func1() dynamically so that the func1() can report it in the event there are errors? Thanks (2 Replies)
Discussion started by: yoi2hot4ya
2 Replies

4. Programming

fork() and child processes

Hello, How many child processes are actually created when running this code ? #include <signal.h> #include <stdio.h> int main () { int i ; setpgrp () ; for (i = 0; i < 10; i++) { if (fork () == 0) { if ( i & 1 ) setpgrp () ; printf ("Child id: %2d, group: %2d\n", getpid(),... (0 Replies)
Discussion started by: green_dot
0 Replies

5. Shell Programming and Scripting

fork() and child processes

Hello, How many child processes are actually created when running this code ? #include <signal.h> #include <stdio.h> int main () { int i ; setpgrp () ; for (i = 0; i < 10; i++) { if (fork () == 0) { if ( i & 1 ) setpgrp () ; printf ("Child id: %2d, group: %2d\n",... (1 Reply)
Discussion started by: green_dot
1 Replies

6. UNIX for Advanced & Expert Users

killing all child processes

Hi, Is there a way I can kill all the child processes of a process, given its process id. Many thanks in advance. J. (1 Reply)
Discussion started by: superuser84
1 Replies

7. Programming

Controlling a child's stdin/stdout (not working with scp)

All, Ok...so I know I *should* be able to control a process's stdin and stdout from the parent by creating pipes and then dup'ing them in the child. And, this works with all "normal" programs that I've tried. Unfortunately, I want to intercept the stdin/out of the scp application and it seems... (9 Replies)
Discussion started by: DreamWarrior
9 Replies

8. Windows & DOS: Issues & Discussions

Controlling AIX processes remotely using a NET app on a Windows server?

I have a .NET application that remotely starts, stops, and gets status of Windows services and scheduled tasks. I would like to add the capability of starting, stopping, and getting status of remote AIX applications also. Based on some preliminary research, one option may be to use 3rd party .NET... (0 Replies)
Discussion started by: auser1
0 Replies

9. Shell Programming and Scripting

Controlling the Number of Child processes

I am trying to implement the below using Ksh script on a Lx machine. There is a file(input_file) with 100K records. For each of these records, certain script(process_rec) needs to be called with the record as input. Sequential processing is time-consuming and parallel processing would eat up... (2 Replies)
Discussion started by: APT_3009
2 Replies

10. Shell Programming and Scripting

Get all child processes of a process

is there a universal way of getting the children of a particular process? i'm looking for a solution that works across different OSes...linux, aix, sunos, hpux. i did a search online and i kept finding answers that were specific to Linux..i.e. pstree. i want to be able to specify a process... (2 Replies)
Discussion started by: SkySmart
2 Replies
signal(2)							System Calls Manual							 signal(2)

NAME
signal - Modifies signal functions SYNOPSIS
#include <signal.h> void (*signal( int sig, void (*function)(int)) (int) ); STANDARDS
Interfaces documented on this reference page conform to industry standards as follows: signal(): XPG4, XPG4-UNIX Refer to the standards(5) reference page for more information about industry standards and associated tags. PARAMETERS
Defines the signal. All signals are valid with the exception of SIGKILL and SIGSTOP. Specifies the address of a signal handler. DESCRIPTION
The signal function provides compatibility for older versions of the operating system whose function is a subset of the sigaction function. The signal function sets the action associated with a signal. The function parameter uses the values SIG_DFL, SIG_IGN, or it can point to an address of a signal handler. A SIG_DFL value specifies the default action that is to be taken when the signal is delivered. A value of SIG_IGN specifies that the sig- nal has no effect on the receiving process. A pointer to a function requests that the signal be caught; that is, the signal should cause the function to be called. These actions are more fully described in the <signal.h> file. NOTES
The sigaction() function provides a more comprehensive and reliable mechanism for controlling signals and is recommended instead of sig- nal() for new applications. [Tru64 UNIX] The effect of calling the signal function behavior differs depending on whether the calling program is linked with either of the special libraries, libbsd or libsys5, which supply BSD or System V signaling characteristics respectively. If neither library is used, the behavior is the same as that of the sigaction function with all the flags set to 0 (zero). If the libsys5 library is used (through compilation with the -lsys5 switch), then the specified signal is not blocked from delivery when the handler is entered, and the disposi- tion of the signal reverts to SIG_DFL when the signal is delivered. If the libbsd library or the bsd_signal() function is used, the behav- ior is the same as that of the sigaction() function with the SA_RESTART flag set. [Tru64 UNIX] When compiled in the X/Open UNIX environment, calls to the signal() function are internally renamed by prepending _E to the function name. When you are debugging a module that includes the libc version of the signal() function and for which _XOPEN_SOURCE_EXTENDED has been defined, use _Esignal to refer to the signal() call. See standards(5) for information on when the _XOPEN_SOURCE_EXTENDED macro is defined. RETURN VALUES
Upon successful completion of the signal function, the value of the previous signal action is returned. Otherwise, SIG_ERR is returned and errno is set to indicate the error. ERRORS
The signal() function sets errno to the specified values for the following conditions: The sig parameter is not a valid signal number or an attempt was made to catch a signal that cannot be caught or to ignore a signal that cannot be ignored. SEE ALSO
Commands: kill(1) Functions: acct(2), bsd_signal(2), exit(2), kill(2), pause(3), ptrace(2), setjmp(3), sigaction(2), sigblock(2), sigpause(3), sigproc- mask(2), sigstack(2), sigsuspend(2), sigvec(2), umask(2), wait(2) Files: signal(4) Standards: standards(5) signal(2)
All times are GMT -4. The time now is 02:35 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy