Question on fork(), exec(), kill()

Farnham David farnham at spot.Colorado.EDU
Thu May 16 06:18:21 AEST 1991


I'm having trouble getting rid of processes which I've killed.
I have a situation where the main program calls a function which
fork()'s and exec()'s.  This function returns the pid of the
child to the main program.  The main program then kill()'s this child.

I don't seem to have any problem killing the child, but after several
iterations I run out of process space and I can no longer fork().
Could someone please shed some light on what I'm doing wrong.  Mild
flames are tolerable if I'm doing something REALLY stupid :-)

Using: Sun sparc 2, 4.1.1
Code follows:
----------------------------------------
#include <stdio.h>    /* this is main() and fun() */
#include <signal.h>

int fun();

main()
{
    int pid;

    while (1) {
        pid = fun();
        sleep(1);
        if ((kill(pid,SIGKILL)) == -1) {
            fprintf (stderr,"Kill failed\n");
            exit(1);
        }
    }
}

int fun()
{
    int pid;

    switch(pid = fork()) {
        case -1:
            fprintf (stderr,"Can't fork\n");
            break;
        case 0:
            execl("./tst","tst",(char *)NULL);
            fprintf (stderr,"Can't exec\n");
            break;
        default:
            return pid;
    }
}
----------------------------------------
#include <stdio.h>  /* this is tst.c */

main()
{
    puts ("in tst");
    while (1)
        ;
}
----------------------------------------

Dave Farnham
Ball Aerospace
Electro-Optical/Cryogenics Division
P.O. Box 1062
Boulder, CO  80306
{ farnham at spot.colorado.edu, farnham at handel.cs.colostate.edu }



More information about the Comp.unix.questions mailing list