LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Solaris / OpenSolaris (http://www.linuxquestions.org/questions/solaris-opensolaris-20/)
-   -   about parent and child process (http://www.linuxquestions.org/questions/solaris-opensolaris-20/about-parent-and-child-process-74270/)

winwar 07-20-2003 06:06 AM

about parent and child process
 
If parent process created a child process by "fork()", how can parent end the child process? Thanks!

dharmender_rai 07-22-2003 05:10 AM

Hi,
After forking, there are two different processes sharing some thing like text (code) section etc. Child knows the process id of its parent ( that information can be got from getppid() ) but it is not possible for the parent to know the id's of its children.
There is a work around for this. You can use shared memory ( list implementation) that would be used by the children to put there and parent's id while the parent would go through that list and if its id is there on the node as the parent id, it would kill the child process using kill() as child has put its pid information there.

Hope that it would help you to some extent.

Dharmender Rai

shishir 07-23-2003 01:22 AM

ithink there is no problem in knowing the pid of a child process ...
so kill() would work just fine...


fork returns twice..once in the child (where it returns 0)
and once in the parent where it is a positive value that is the pid of the child process..you can alway do a kill on it

>>>code
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
int main(void)
{
int pid;
pid = fork();

if ( pid == 0)
{
printf("in child with pid = %d\n",getpid());
sleep(30);
_exit(0);
}
else
{
printf("n parent where child pid is = %d\n",pid);
kill(pid,SIGINT);
}
return 0;
}

dharmender_rai 07-23-2003 06:07 AM

thanks. i just couldn't think in that way.


All times are GMT -5. The time now is 08:48 PM.