PDA

View Full Version : pid's



quantt
08-13-2009, 02:59 AM
hi, how I can get everyone child pid's the some parent?

Salem
08-13-2009, 03:49 AM
Add them to some record when you call fork()
Remove them from that record when you waitpid() for them to exit

Or are you trying to create some kind of process tree?

quantt
08-13-2009, 05:02 AM
for instance, i use it function many times... I can tell parent from child (1), but how I can tell from child with B child A?


int main(void){
launcher("A");
launcher("B");
}
void launcher(char *put){
pid_t pid;
TELL_WAIT();
if ((pid = fork()) < 0) {
printf("fork error\n");
}
if (pid == 0) {
glob++;
var++;

if(strcmp(put,"A")){
printf("output from child glob = %d, var = %d , put = A \n",glob, var);


}
if(strcmp(put,"B")){
printf("output from child glob = %d, var = %d , put = B \n",glob, var);
TELL_CHILD(pid); // I want call child A from child B
WAIT_CHILD();
}
//WAIT_PARENT(); //1
} else {
if(strcmp(put,"A")){
printf("pid = %d, parent_pid=%u,glob = %d, var = %d, xxx %d put = A \n", getpid(), getppid(), glob, var, pid);
}

if(strcmp(put,"B")){
printf("pid = %d, parent_pid=%u,glob = %d, var = %d, xxx %d put = B\n", getpid(), getppid(), glob, var, pid);
//TELL_CHILD(pid); //1
}
}
}
and why parent_pid different

pid = 31483, parent_pid=4460,glob = 6, var = 88, xxx 31485 put = B
output from child glob = 7, var = 89 , put = B
pid = 31483, parent_pid=4460,glob = 6, var = 88, xxx 31486 put = A
pid = 31485, parent_pid=31483,glob = 7, var = 89, xxx 31487 put = A
output from child glob = 7, var = 89 , put = A
output from child glob = 8, var = 90 , put = A

matsp
08-13-2009, 05:16 AM
Your code starts three children:
A, which then returns to start a B process.
and B started from the main process.

So you have two "B" processes, one with the main process as parent, and one with A as parent.

You may want to change your code around so that the launch of A doesn't return to start B.

--
Mats

quantt
08-13-2009, 05:47 AM
right, how make one parent with two child's and telling from one child another child?

MK27
08-13-2009, 11:10 AM
right, how make one parent with two child's and telling from one child another child?

Use the pid of each child.

You might also want to investigate getppid() (get parent process id) and this stuff:

Launching Jobs - The GNU C Library (http://www.gnu.org/s/libc/manual/html_node/Launching-Jobs.html)