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
sigvec(2)							System Calls Manual							 sigvec(2)

NAME
sigvec - Provides a compatibility interface to the sigaction() function SYNOPSIS
#include <sys/signal.h> int sigvec ( int signal, struct sigvec *in_vec, struct sigvec *out_vec ); PARAMETERS
Specifies the signal number. Points to a sigvec() structure that specifies the action to be taken when the specified signal is delivered, the mask to be used when calling the signal handler, and the flags that modify signal behavior. Points to a sigvec() structure that is set to the previous signal action state on successful return from the sigvec() function. DESCRIPTION
The sigvec() function is provided for compatibility to old UNIX systems; its function is a subset of that available with the sigaction() function. Like the sigaction() function, the sigvec() function allows the user to set the action to take upon the receipt of a signal and to specify a signal handler mask to block signals before calling the signal handler. However, only signals with values 1 to 31 can be masked on entry to a signal-handler set up with the sigvec() function. The sigvec() structure has the following members: void (*sv_handler)( ); int sv_mask; int sv_flags; The sv_handler field specifies the action for the signal, and can be SIG_DFL, SIG_IGN, or the address of a signal handler function. See the sigaction() function for a detailed description of the signal actions. The sv_mask field specifies a mask which specifies signals to block (in addition to any signals already blocked at time of delivery) when the signal handler function is called for the signal. Signal i is blocked if the i-th bit of the mask is set. Only signals with values 1 to 31 can be masked with the sigvec() function. The sv_flags field contains flags that further specify signal behavior. If SV_ONSTACK is set, the signal handler runs on the signal stack specified by the sigstack() function; otherwise, the signal handler runs on the stack of the process receiving the signal. If SV_INTERRUPT is set, a system call that is interrupted by signal returns a value of -1 with errno set to [EINTR]; otherwise, a system call interrupted by signal is restarted. If the value of the in_vec parameter is a null pointer, then the signal handler information is not set. If the value of the out_vec parame- ter is null, then the previous signal handler information is not returned. Once a signal handler is assigned, it remains assigned until another call to the sigvec(), signal(), sigaction(), or exec function is made. NOTES
The sigvec() function is provided for compatibility only, and its use is not recommended. Programs should use the sigaction() function instead. The sigvec() function does not check the validity of the sv_handler field pointer. If it points to a location outside of the process address space, the process receives a memory fault when the system attempts to call the signal handler. If the sv_handler field points to anything other than a function, the results are unpredictable. The signal-handler function can be declared as follows: void handler ( int signal ); RETURN VALUES
Upon successful completion, a value of 0 (zero) is returned. If the sigvec() function fails, a value of -1 is returned and errno is set to indicate the error. ERRORS
If the sigvec() function fails, no new signal handler is installed and errno may be set to one of the following values: The in_vec or out_vec parameter points to a location outside of the process' address space. The signal parameter is not a valid signal number. An attempt was made to ignore or supply a handler for the SIGKILL signal. RELATED INFORMATION
Functions: kill(2), ptrace(2), sigaction(2), sigblock(2), sigpause(3), sigstack(2) delim off sigvec(2)
All times are GMT -4. The time now is 07:53 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy