bug-commoncpp
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Non blocking Process::spawn, Process::join


From: Robert Kling
Subject: Non blocking Process::spawn, Process::join
Date: 28 Aug 2003 18:24:43 +0200

Hi

I dont know how "active" the patch manager is on savannah.gnu.org so I
post this patch here as well. This code enables both blocking and
non-blocking Process::spawn functions. In case of blocking (the default
behaviour, wait = true) the spawned process' return code is returned. In
nonblocking (wait = false) the spawned process' pid is returned. You can
join a spawned process by Process::join. 

Patch is against commoncpp2-1.0.13 and only tested on linux (but should
work on WIN32 as well).

Cheers,
Robert

Add to include/cc++/process.h: NOTE! Code not tested for WIN32
***************************************************************

        /**
         * Spawn a process and optionally wait for it's exit code.  In win32
         * this is done with the spawn system call.  In posix,
         * this is done with a fork, execvp and waitpid.
         *
         * @warning The implementation differences between posix and
         * win32 systems may cause side effects.
         *
         * @return if 'wait' is true return process' exit code, otherwise pid 
of newly spawned process.
         * @param exec name of executable.
         * @param argv list of command arguments.
         * @param wait wait for process' exit code.
         */
        static int spawn(const char *exec, const char **argv, bool wait = true);

        /**
         * Join a previously spawned process.
         *
         * @return exit code from joined process.
         * @param pid pid of joinable process.
         */
        static int join(int pid);

Add to src/process.cpp: NOTE! Code not tested for WIN32
***************************************************************

#ifdef  WIN32
int Process::spawn(const char *exename, const char **args, bool wait)
{
        if (wait)
                return ::spawnvp(P_WAIT, (char *)exename, (char **)args);
        
        return ::spawnvp(P_NOWAIT, (char *)exename, (char **)args);
}
#else
int Process::spawn(const char *exename, const char **args, bool wait)
{
        int pid, status;

        pid = vfork();

        if(pid == -1)
                return -1;

        if(!pid)
        {
                execvp((char *)exename, (char **)args);
                _exit(-1);
        }

        if (wait)
        {
#ifdef  __FreeBSD__
                wait4(pid, &status, WUNTRACED, NULL);
#else
                waitpid(pid, &status, WUNTRACED);
#endif

#ifdef  WEXITSTATAUS
                return WEXITSTATUS(status);
#else
                return status;
#endif
        }

        return pid;
}
#endif

#ifdef  WIN32
int Process::join(int pid)
{
        int status;

        waitpid(pid, &status);

        return status;
}
#else
int Process::join(int pid)
{
        int status;

#ifdef  __FreeBSD__
        wait4(pid, &status, WUNTRACED, NULL);
#else
        waitpid(pid, &status, WUNTRACED);
#endif

#ifdef  WEXITSTATAUS
        return WEXITSTATUS(status);
#else
        return status;
#endif
}
#endif





reply via email to

[Prev in Thread] Current Thread [Next in Thread]