LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-15-2006, 01:42 PM   #1
DiAvOl
LQ Newbie
 
Registered: May 2006
Posts: 2

Rep: Reputation: 0
Sending signal to child process


First of all hello, (this is my first post)

I'm using a daemon which takes requests (fetces rows) from a database and then starts an rsync (remote synchronization tool provided by samba) with the exec() command. I want to add a feature so that the daemon can stop (kill -STOP) an rsync process and then continue later (kill -CONT).

The Tree is like this:

-parent
\_forked-process (known pid)
\__Rsync-process (executed using exec command, unknown pid)

My problem is that after executing the exec command inside the forked-process the pid of the rsync-process is unknown (exec() does not return the pid) so I don't know it's value to stop it later...

Any help appreciated
Thanks for your time & sorry for my bad english
 
Old 05-15-2006, 02:10 PM   #2
pmarques
LQ Newbie
 
Registered: Jul 2003
Posts: 28

Rep: Reputation: 15
Use process group?

You might try to use "setpgrp" after "fork" and before "exec" to set the process group of the child process.

Then you can use "killpg" to send a signal to every child in that group.
 
Old 05-15-2006, 03:37 PM   #3
ioerror
Member
 
Registered: Sep 2005
Location: Old Blighty
Distribution: Slackware, NetBSD
Posts: 536

Rep: Reputation: 30
Quote:
-parent
\_forked-process (known pid)
\__Rsync-process (executed using exec command, unknown pid)
Exec doesn't create a new pid. The _only_ function in unix that causes a new pid to be created is fork. So you already know the pid.
 
Old 05-15-2006, 03:54 PM   #4
sundialsvcs
Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 5,425

Rep: Reputation: 1159Reputation: 1159Reputation: 1159Reputation: 1159Reputation: 1159Reputation: 1159Reputation: 1159Reputation: 1159Reputation: 1159
Unless I am sadly mistaken, exec replaces the calling process with the one that you are executing. The call returns only if it does not succeed.

So, if you fork a child process which then execs another program-file, the PID of the child has not changed. It has ceased to be an instance of the original parent-process and has become an instance of the program that it execed.

So your diagram is incorrect. There are never three layers.
 
Old 05-15-2006, 07:41 PM   #5
pmarques
LQ Newbie
 
Registered: Jul 2003
Posts: 28

Rep: Reputation: 15
other applications can fork too

rsync might fork even more processes to distribute the work to be done (or maybe threads, depending on what they share).

This is what I assumed from the diagram, and thus the suggestion to give a different process group id to the child.
 
Old 05-15-2006, 10:59 PM   #6
DiAvOl
LQ Newbie
 
Registered: May 2006
Posts: 2

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by pmarques
rsync might fork even more processes to distribute the work to be done (or maybe threads, depending on what they share).

This is what I assumed from the diagram, and thus the suggestion to give a different process group id to the child.
This is correct.. I'm trying to apply your solution
 
Old 05-16-2006, 01:16 PM   #7
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Create a pipe with 'pipe', fork the process with 'fork', replace standard output of the forked process with the input pipe descriptor using 'dup2', then 'execv' your new process. The exec'ed program inherits the replaced stdout, which will print to the pipe which the original process has a descriptor for.
http://www.gnu.org/software/libc/man...reating-a-Pipe
http://www.gnu.org/software/libc/man...ng-Descriptors
ta0kira

PS Do the same thing with stdin if you want to send info TO the process; then you can use 'fgets_unlocked' to check for a signal periodically without freezing the thread waiting for input.
http://www.gnu.org/software/libc/man...tml#Line-Input

Last edited by ta0kira; 05-16-2006 at 01:21 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how a father process know which child process send the signal SIGCHLD icoming Programming 10 07-20-2010 08:26 AM
How to kill a Child and all its subsequent child process in C shayer009 Programming 3 12-04-2007 01:40 AM
Killing a child process from another child marri Programming 6 10-01-2004 08:08 PM
Sending a signal to a process (Help me) rajesh_b Programming 2 09-17-2004 02:30 AM
child process -1 exited due to signal 6 ashfaq Linux - Software 1 04-09-2004 06:42 AM


All times are GMT -5. The time now is 09:17 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration