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

RE: Backround process, fork & vfork, synchronization methods?

On Tue, 20 Mar 2001 Timo.Sakari@xxxxxxx.fi wrote:
> > 1) put the driver in a completely separate program, and pipe 
> > it into the
> > user-program somehow (like the user-program opens a pipe and 
> > select() on
> > it to know when the serial driver has gotten the "magic" string)
> One more question from a Linux/Unix programming "newbie": Should
> I start that seperate program from within my main program (how?
> fork/vfork seem not to be the correct solution here),

vfork followed by exec in the child, spawns a separate sub-program. the
parent will be suspended until the exec has started the sub-program, then
it will continue.

> or should I start the app from command line?  Any good pointers
> to information how can I get these two different programs to communicate
> with each other? I guess standard pipes cannot be used here? Is

yes they can, do pipe() before the vfork/exec and make sure the child gets
one part of the pipe as its stdin/stdout.

for example (from our sftpd.c)

        int vpipe[2];
        int retfd;
        int childfd;
        int childrw;
        int pid;

        char *argv[] = { NULL, NULL };
        char *envp[] = { NULL, NULL };


                        retfd = vpipe[0];
                        childfd = vpipe[1];
                        childrw = STDOUT_FILENO;

                if((pid = vfork()) == 0) {
                        /* child */
                        if(childfd != childrw) {
                                dup2(childfd, childrw);
                        close(retfd);          /* just for good measure */
                        argv[0] = filename;
                        execve(filename, argv, envp);
                        /* execve failed */
                        syslog(LOG_ERR, "execve %s failed: %m (%d)",
vt->real, errno);

at this point, you have a running child program whose output comes to the
parent programs filedescriptor "retfd" (one end of the pipe) 

see the manpage for 'pipe' for further information...

you get the childs process id in "pid" if you need to kill it or something
later. filename points to the program filename to be executed.