Controlling child processes


 
Thread Tools Search this Thread
Top Forums Programming Controlling child processes
# 1  
Old 03-25-2004
Controlling child processes

Hello all, I am trying to create n child processes and control them from a parent process; say make child 3 print its pid and then child 5 do the same and some other stuff. Is there a way to accomplishing this after all the child processes are created via a call to fork().
Thank you,
FG
# 2  
Old 03-25-2004
> Is there a way to accomplishing this after all the child processes are created via a call to fork().

No. Unless child and parent explicitly define a protocol to control such things on a voluntary basis, e.g. the parent to could send a certain signal to a child in order to have it react in a pre-defined manner.

Given that all your target processes are children, the parent could create a pipe with each of them and send a signal whenever you've just sent a command down the pipe. The children would check the state of the pipe whenever the signal arrives and take appropriate actions. Of course, this might not be the best way depending on your requirements (apparently you don't seem to know what you want either), but it's a pretty asynchronous one.

Other than that, you could in theory kludge your own machine code into the child's text segment e.g. by using ptrace() or /proc/<pid>/mem or somesuch, but this is hardly applicable, so I'd guess you want to do the former.
# 3  
Old 03-25-2004
I gave it some thought, and based on your reply I would like to do the following: "the parent could create a pipe with each of them and send a signal whenever you've just sent a command down the pipe"
-- I have to guarantee that when I send the command down the pipe and then the signal, this is all done atomically, 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.
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.
Thank you.
FI
# 4  
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.
# 5  
Old 03-25-2004
Thank you for all your suggestions and resources, just some insight into the problem I am trying to solve, I have to create n process and print the process number to a file m times; this is a sample:
% a.out 5 3 temp

should result in temp having the following contents:
1
2
3
4
5
1
2
3
4
5
1
2
3
4
5

Can you think of another solution other than the possibilities discussed.
Thank you,
FG
# 6  
Old 03-25-2004
In other words, the children are doing nothing while waiting for the parent's notification. I agree that you will need a semaphore then, because the order in which signals are handled cannot be enforced in a reliable manner without resorting to changing scheduling policies, which is definitely not justified in this case.

I would probably do this:
Have each child install a signal handler for SIGUSR1, without restarting interrupted system calls. Have a loop calling pause() for the desired number of times the PID shall be written. After the parent has signaled the current child, it goes to sleep on a semaphore. After the child has written its PID to the file, it awakes the parent by incrementing the semaphore and calls pause() again. The parent goes on to signal the next child, and so on. Good luck
# 7  
Old 03-25-2004
Thank you for all your help, I will be coding this weekend.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Login or Register to Ask a Question