fork() and execvp() problem

fork() and execvp() problem

Post by Timo Be » Wed, 01 Aug 2001 04:52:44



Hi,

i want to write a little prog which executes another one using the execvp()
function. The executed program should be monitored and when it exits some
action should happen.
I wrote the following little code fragment which works very well so long
the executed prog does not fork into background, meaning that it exits its
parent process after forking.
So my question is:
Is there a way to trace the executed prog and/or to get the pid of the new
process if it forks into background?
If that can't be accomplished using plain c maybe it can be done using
the linux proc filesystem?

---<snip>---

pid_t childpid = fork();

switch( childpid ) {
        case -1:   /* fork failed */
            perror("fork(main)");
            exit( EXIT_FAILURE );
        case 0:    /* fork succeed - child process */
                execvp( argv_prog[0], argv_prog );
                // this point should never be reached
                perror("execvp(main)");
                exit( EXIT_FAILURE );
            break;
        default:   /* fork succeed - parent process */
            cout << "childprocess running" << endl;
            // wait until child exits
            int wait_result, status;
            waitpid( childpid, &status, WUNTRACED );
            cout << "childprocess exited" << endl;
            exit( EXIT_SUCCESS );
            break;

Quote:}

                                                                                                 arginfo.rm_pidfile_flag,
---<snap>---

-timo

--

| Timo Benk    
| Germany
| Registered Linux User #186431
| Fax/Voicemail:        +49/0180 505258732297

 
 
 

fork() and execvp() problem

Post by Paul Sa » Sat, 04 Aug 2001 02:00:05



> Hi,

> i want to write a little prog which executes another one using the execvp()
> function. The executed program should be monitored and when it exits some
> action should happen.
> I wrote the following little code fragment which works very well so long
> the executed prog does not fork into background, meaning that it exits its
> parent process after forking.
> So my question is:
> Is there a way to trace the executed prog and/or to get the pid of the new
> process if it forks into background?
> If that can't be accomplished using plain c maybe it can be done using
> the linux proc filesystem?

I am not sure what the problem with your code is. You can't easily
find out if a child's child dies, if that is what you mean. You
*could* maybe open a pipe, then the child will still have the pipe
open, the child's child will still have the pipe open. Then you can
select() on the pipe & when the pipe is invalid for writing (because
the child and grandchild have died.) the signal SIGPIPE will be
generated. I hope this helps.

--
Aleph-null bottles of beer on the wall,
Aleph-null bottles of beer,
        You take one down, and pass it around,
Aleph-null bottles of beer on the wall.

 
 
 

1. if linked -static, pthreads become defunct on system, fork, or execvp

Hello, I was wondering if any of the sharp minds out there could tell me
the cause of the following problems.

I have
linux version 2.2.14
redhat 6.1
gcc version egcc-2.91.66

If I link my program that uses pthreads with the -static switch if
find that my theads stop working and become defunct if I call system, fork,
or exec.

The problem goes away if I leave off the -static switch. yes, I am using
-D_REENTRANT.

pthread is the only library I am linking to other than the default libraries.

Does anybody know what the problem is?

----------

Paul Elliott                          Telephone: 1(512)837-9345

http://www.io.com/~pelliott/pme/      Austin TX 78758-3117

2. cd recorder recommendations?

3. fork + execvp

4. Shell question

5. pipe ... fork ... execvp ... segmentation fault

6. Access to Windows Neighbourhood workgroup from Linux

7. fork execvp deadlock

8. irqs on large machines (patch)

9. comsat forking, forking and forking (3.2.0)

10. Matrox Mystique ands X.

11. execvp problem

12. Problems with execvp

13. having a problem with execvp