Link to home
Start Free TrialLog in
Avatar of ocean
ocean

asked on

fork: Resource temporarily unavailable

Hi,

This is a PIC related problem(at least I think it is), please
look at it and help if you can.  

The system is IRIX6.2 on Indigo2, compiler is C++ 7.1.

I needed a function that starts a child process, and returns the
pid and output of the child process.  Since there is no such a
function in unix, I wrote my own using pipe(to pass output back
to parent from child), fork(to create the child process, and to
get the child pid), and exec(to replace the child process with
the specified system command).

Then I wrapped this function in a daemon that accepts requests from
a message queue. My other applications can send system cmd to the
daemon and get pid and output of the new process back.  Since
we are not using multithreading, this is very helpful to manage
multiple processes.

However, after running the daemon after a while, the program can't
fork any more and gives: "fork: Resource temporarily unavailable".
After that, "no more process available" messages are given by
the shells.  Everything will be back to normal after this daemon
is stopped.

I have checked that the virtual swap space has >100M free, the
number of processes is <20 for the user and <100 for the system.

What kind of resources is used out?  Could it be caused by the
message queue that I am using?  


The function looks like this:
char *system1 (char *cmd, int *pid, int needoutput)
{
    int pfd[2];
    FILE *fd;
    pid_t child;
    char *out, buff[100];

    if(pipe(pfd) < 0){...}      //
    if ((child = fork ())<0){...}
    if (child == 0){
        dup2(pfd[1],1);
        close(pfd[0]);

        execv() or execvp()     // replace child with cmd
    }else{
        close(pfd[1]);
        *pid = child;           // return child pid

        if(needoutput){
          if (!(fd = fdopen(pfd[0],"r"))){...}
          if (fgets(buff,100,fd)){
                // processing child output
                // allocate and set out string
          }
          fclose(fd);
        }
        close(pfd[0]);
        return out;             // return child output
    }
}


Please give advice!  Thanks!

ASKER CERTIFIED SOLUTION
Avatar of prade
prade

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of prade
prade

try out the UNIX FAQ section 1.6 in the Web:
http://www.whitefang.com/unix/faq_2.html#SEC13
or the mirror at http://www.erlenstar.demon.co.uk/unix/