Terminating a child process 
Author Message
 Terminating a child process

Hi all,
I am currently developing a C program on AIX 4.0 that interacts with a
proprietary communication protocol server. The program involves
establishing sessions, receiving data and writing data to Oracle
database.
I am forking a child process to write the data to the database. The
problem I face is that the forked child processes do not terminate after

the function is executed. But once the parent terminates, all the child
processes terminates.

The attached code is a copy of the mechanism used in the actual code.
Can anyone tell me why the child processes do not terminate ?

Code begins here :
#include <stdio.h>
#include <signal.h>
#include <sys/errno.h>
#include <unistd.h>
int main()
{
 int i=0;
 pid_t pid;
 while (1)
 {
  if (i==10)
   exit(0);
  pid = fork();
  if (pid == 0)
  {
   print_val();
   exit(0);
  }
  i++;
  sleep(3);
 }

Quote:
}

int print_val()
{
 int i,j;
 for (i=0;i<10;i++)
 {
  j=j+i;
  printf("Value is : %d\n",j);
 }
 return 0;

Quote:
}

Code Ends here.

Thanks for your help,
Sajid.




Tue, 28 Mar 2000 03:00:00 GMT  
 Terminating a child process



Quote:
> Hi all,
> I am currently developing a C program on AIX 4.0 that
interacts with a
> proprietary communication protocol server. The program
involves
> establishing sessions, receiving data and writing data to
Oracle
> database.
> I am forking a child process to write the data to the
database. The
> problem I face is that the forked child processes do not
terminate after

> the function is executed. But once the parent terminates, all
the child
> processes terminates.

> The attached code is a copy of the mechanism used in the
actual code.
> Can anyone tell me why the child processes do not terminate ?

> Code begins here :
> #include <stdio.h>
> #include <signal.h>
> #include <sys/errno.h>
> #include <unistd.h>
> int main()
> {
>  int i=0;
>  pid_t pid;
>  while (1)
>  {
>   if (i==10)
>    exit(0);
>   pid = fork();
>   if (pid == 0)
>   {
>    print_val();
>    exit(0);
>   }
>   i++;
>   sleep(3);
>  }
> }

> int print_val()
> {
>  int i,j;
>  for (i=0;i<10;i++)
>  {
>   j=j+i;
>   printf("Value is : %d\n",j);
>  }
>  return 0;
> }

> Code Ends here.

> Thanks for your help,
> Sajid.



Hi Sajid,

Nope, if you ask the question here, read the answer here.

This question is off topic for comp.lang.c, which discusses the
standard C programming language and not specific operating
system issues.  The header file uistd.h is not part of standard
C, neither is pid_t, fork, or sleep.  These are specific to UNIX
and UNIX like operating systems.

I would suggest you post this question to comp.unix.aix, or a
one of the comp.unix.* groups.

Please read the article "--- Welcome to comp.lang.c!  Please
read this first" which is posted frequently to this group by
Billy Chambless so that in the future you will know what
subjects are on and off topic here.

Jack



Wed, 29 Mar 2000 03:00:00 GMT  
 Terminating a child process

I say adopt the child process, not abort() it.

I'm at home, so I say anything I please.



Wed, 29 Mar 2000 03:00:00 GMT  
 Terminating a child process



Quote:
>I am forking a child process to write the data to the database. The
>problem I face is that the forked child processes do not terminate after

>the function is executed. But once the parent terminates, all the child
>processes terminates.

Read the man page on wait(), waitpid() and related functions, and then go to
comp.unix.programmer. The C language has no built-in facilities for concurrent
programming.
--


Wed, 29 Mar 2000 03:00:00 GMT  
 Terminating a child process

The main problem is that you neglected to wait() on the children;
so long as their parent exists, terminated children remain in a "zombie" state so that there is a place to keep their exit
status, which is made available to the wait()er.
When they have been "orphaned", i.e. their parent has terminated, they are inherited by a "reaper" process (traditionally part
of init, PID#1), which quickly lays them to rest by wait()ing on them.
In cases where the parent does not find it convenient to wait on the children, a useful trick is to make them grandchildren
instead, with the intermediate child process immediately dying; as just described, that means that the children will be
immediately inherited by PID#1, so when they terminate they won't be zombies for more than an instant.
        if ((pid=fork())==0)
                // child process
                if ((pid=fork())>0)
                        _exit(0);       // child suicides after spawning
                else if (pid<0)      // error
                        handle_failure_to_fork(pid);
                else { // pid==0        // grandchild
                        do_slave_processing();
                        _exit(0);       // status sent to PID#1
                }
        else    // parent process
                if (pid<0)   // error
                        handle_failure_to_fork();
                else    // wait for (immediate) suicide of child
                        while (waitpid(pid,&status,0)<0 && errno==EINTR)
                                ;       // wait interrupted by signal, try again



Fri, 31 Mar 2000 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. How to terminate (childof child) process?

2. How to terminate the child process by right way?

3. How to terminate the child process by right way?

4. How to terminate the child process by right way?

5. monitoring the process time of a child process

6. Creating the child process and redirected to parent process

7. Child Process & Parent Process Data :: Win32

8. How to terminate child process when calling process exits?

9. How to terminate the Child thread from the Parent thread

10. MDI Child terminate message to main frame?

11. Process suddenly terminates without notification

12. terminate process

 

 
Powered by phpBB® Forum Software