Unix: Need help with fork() or other methods of multithreading 
Author Message
 Unix: Need help with fork() or other methods of multithreading

Hi-

I've written a cgi-bin script in c.  This script is responsible for, after
processing the appropriate information from an HTML form, firing off a shell
script that takes anywhere from 15-45 mins. to complete.

I've figured out how to get that script to execute using either execve() or
system().  The only problem is that these appear to, upon successful execution
of the shell script, exit (rather than return to the c script).  I did a man on
system and execve and noticed that they both spawn their own child processes,
but never return to the caller.

Is there some way to fork() and then execute the script in a child process so
that my c script can tell the user that it successfully started the script?
And how does one go about using the process ID that fork() returns to actually
do anything useful?  The man documentation on this appears to be somewhat
skimpy...

Thanks for the help!

"To Err is human,
       to sort of foul things up you need a computer,
            to really ruin your day use a Microsoft product"



Tue, 01 Jan 2002 03:00:00 GMT  
 Unix: Need help with fork() or other methods of multithreading


SNIP

Quote:
> system().  The only problem is that these appear to, upon successful
execution
> of the shell script, exit (rather than return to the c script).  I did
a man on
> system and execve and noticed that they both spawn their own child
processes,
> but never return to the caller.

> Is there some way to fork() and then execute the script in a child
process so
> that my c script can tell the user that it successfully started the
script?
> And how does one go about using the process ID that fork() returns to
actually
> do anything useful?  The man documentation on this appears to be
somewhat
> skimpy...

system() will return after completion.

this should work too:
if(!fork())
{
        system("./world_domination");
        exit(0);

Quote:
}

else
{
        printf("Process started\n");

Quote:
}

Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.


Tue, 01 Jan 2002 03:00:00 GMT  
 Unix: Need help with fork() or other methods of multithreading

Quote:

> Is there some way to fork() and then execute the script in a child process so
> that my c script can tell the user that it successfully started the script?
> And how does one go about using the process ID that fork() returns to actually
> do anything useful?  The man documentation on this appears to be somewhat
> skimpy...

The book you need is Stevens' Advanced Programming in the
UNIX Environment. It gets into a lot of detail about this
kind of stuff.

Scott



Tue, 01 Jan 2002 03:00:00 GMT  
 Unix: Need help with fork() or other methods of multithreading
Hi,

Quote:


> > Is there some way to fork() and then execute the script in a child process so
> > that my c script can tell the user that it successfully started the script?
> > And how does one go about using the process ID that fork() returns to actually
> > do anything useful?  The man documentation on this appears to be somewhat
> > skimpy...

> The book you need is Stevens' Advanced Programming in the
> UNIX Environment. It gets into a lot of detail about this
> kind of stuff.

I have this one it's a good book, but of course we'll gets lots of this kind of

Regards

--
Jarmo Tiittanen
Svanstr?minkuja 5-7 B22
00870 HELSINKI
http://www.dlc.fi/~jacxs




Tue, 01 Jan 2002 03:00:00 GMT  
 Unix: Need help with fork() or other methods of multithreading
: Is there some way to fork() and then execute the script in a child
: process so that my c script can tell the user that it successfully started
: the script?  And how does one go about using the process ID that fork()
: returns to actually do anything useful?  The man documentation on this
: appears to be somewhat skimpy...

Your question is better suited for comp.unix.programmer as fork() is a
Unix system call.  However, you should know that using fork()/exec*()
combinations are widely used in Unix programming.  You may also want to
look at the man page for waitpid().

In the future it is better to post questions like these to
comp.unix.programmer where folks who specialize in programming in the Unix
environment hang out.  By doing this you maximize getting a better, more
accurate/robust answer to your question.  comp.lang.c is for questions
dealing with standard C only; platform specific questions (even though
your program is written in C your question is not a C language question)
should be directed to newsgroups which discuss features of that specific
platform.

Paul

--

Director, X-ray Structural Facility |   phone: (919) 515-7362
Department of Chemistry - Box 8204  |   FAX:   (919) 515-5079
North Carolina State University     |
Raleigh, NC, 27695-8204
http://laue.chem.ncsu.edu/web/xray.welcome.html



Fri, 04 Jan 2002 03:00:00 GMT  
 Unix: Need help with fork() or other methods of multithreading
Fri, 16 Jul 1999 17:38:38 GMT,

Quote:


> SNIP

> > system().  The only problem is that these appear to, upon successful
> execution
> > of the shell script, exit (rather than return to the c script).  I did
> a man on
> > system and execve and noticed that they both spawn their own child
> processes,
> > but never return to the caller.

> > Is there some way to fork() and then execute the script in a child
> process so
> > that my c script can tell the user that it successfully started the
> script?
> > And how does one go about using the process ID that fork() returns to
> actually
> > do anything useful?  The man documentation on this appears to be
> somewhat
> > skimpy...

C programs aren't normally called "scripts".  

Quote:
> system() will return after completion.

More precisely system() (if implemented at all, and successful)
must wait for the specified program to complete, then return;
in Unix this will almost certainly be done by fork()ing a child
and having the child exec* the desired script (or program)
while the parent waits for it.  

Quote:
> this should work too:
> if(!fork())
> {
>         system("./world_domination");
>         exit(0);
> }
> else
> {
>         printf("Process started\n");
> }

No, here the child will wait for the grandchild --
and lose any error status system() managed to keep --
but the parent doesn't wait for the child.  
The idiom is:

#if UNIX_SPECIFIC /* and very incomplete */
  pid = fork();
  if( pid == -1 ) { /* handle error */ }
  /*else*/ if( pid == 0 ) /* child*/
  {
    execv("pgm",args); /* or other exec* variant */
    /* error handling for exec failure */
    exit(some_error); /* clean up extra child */
  }
  else /* parent */
  {
    printf("Started %d\n", (int)pid);
    /* or other computation and/or reporting */
    err = wait(pid); /* or wait3() until pid occurs */
    printf("Completed %d\n", (int)pid);
    /* or other reporting, including err */
 }
#endif

The child pid can also be used to kill() it, in
some system-specific ways to check it's status,
and to build almost-guaranteed-unique filenames,
among other things.  

This is all OFFTOPIC for comp.lang.c and belongs in
something more like comp.unix.programmer.  Or get
any textbook or reference on Unix programming;
Stevens is the classic (but mine is not to hand)
and there must be at least one O'Reilly applicable.  

- david.thompson at but not for trintech.com



Sat, 05 Jan 2002 03:00:00 GMT  
 Unix: Need help with fork() or other methods of multithreading
[I hope this isn't a duplicate again. Dang Netscape!]

Fri, 16 Jul 1999 17:38:38 GMT,

Quote:


> SNIP

> > system().  The only problem is that these appear to, upon successful
> execution
> > of the shell script, exit (rather than return to the c script).  I did
> a man on
> > system and execve and noticed that they both spawn their own child
> processes,
> > but never return to the caller.

> > Is there some way to fork() and then execute the script in a child
> process so
> > that my c script can tell the user that it successfully started the
> script?
> > And how does one go about using the process ID that fork() returns to
> actually
> > do anything useful?  The man documentation on this appears to be
> somewhat
> > skimpy...

C programs aren't normally called "scripts".  

Quote:
> system() will return after completion.

More precisely system() (if implemented at all, and successful)
must wait for the specified program to complete, then return;
in Unix this will almost certainly be done by fork()ing a child
and having the child exec* the desired script (or program)
while the parent waits for it.  

Quote:
> this should work too:
> if(!fork())
> {
>         system("./world_domination");
>         exit(0);
> }
> else
> {
>         printf("Process started\n");
> }

No, here the child will wait for the grandchild --
and lose any error status system() managed to keep --
but the parent doesn't wait for the child.  
The idiom is:

#if UNIX_SPECIFIC /* and very incomplete */
  pid = fork();
  if( pid == -1 ) { /* handle error */ }
  /*else*/ if( pid == 0 ) /* child*/
  {
    execv("pgm",args); /* or other exec* variant */
    /* error handling for exec failure */
    exit(some_error); /* clean up extra child */
  }
  else /* parent */
  {
    printf("Started %d\n", (int)pid);
    /* or other computation and/or reporting */
    err = wait(pid); /* or wait3() until pid occurs */
    printf("Completed %d\n", (int)pid);
    /* or other reporting, including err */
 }
#endif

The child pid can also be used to kill() it, in
some system-specific ways to check it's status,
and to build almost-guaranteed-unique filenames,
among other things.  

This is all OFFTOPIC for comp.lang.c and belongs in
something more like comp.unix.programmer.  Or get
any textbook or reference on Unix programming;
Stevens is the classic (but mine is not to hand)
and there must be at least one O'Reilly applicable.  

- david.thompson at but not for trintech.com



Sat, 05 Jan 2002 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Need urgent help with this (fork, dup, pipes)

2. URGENT : Help needed with system()/fork() call

3. Need advice/help with fork()

4. need help with ?fork()?

5. DAO & OTHERS ISAM METHODS

6. Help needed with concurrent programing: ie Fork()

7. GA Multi Jobs/Fortune 100 and others/Unix,C,Lans,Programming

8. need a lot help on multithreading. thanks

9. Need help on mfc multithreading issue

10. Multithreading - need Help

11. Multithreading - need Help

12. unix fork() question..

 

 
Powered by phpBB® Forum Software