Fork multiple Applications and Monitor.

Hi, I am looking to kick off multiple homegrown C++ applications in a linux environment. I also need to monitor the applications and restart them if they die.

Ideally I would loop threw a list of applications forking and exec them. I am having trouble returning from the first execv call. (I have not implemented the loop to run the multiple applications - Taking this one step at a time.) Am I going about this right? Should I be using fork and exec? Is there a better way to start and monitor the applications?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <sys/types.h>
#include <signal.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

int main(int argc, char *argv[]) {

 
	char *arguments[] = { "", NULL };
	int pid;
	if ((pid = fork()) < 0) {
	    cout << "Error\n";
	}
	else if (pid == 0) {
 
	    execv("Program1", arguments);
            // The above call does not return
            cout << "Program1 has returned " << endl;
 
	}
	else {
 
	}

 
        // Loop Forever and Monitor the PIDs 
 
        while (1){
    	    cout<<"Should Check the PID for Child Processes" << endl;
            sleep (10);
        }

	 
	return 0;
}
man wrote:
RETURN VALUE
The exec() functions return only if an error has occurred. The return value is -1, and errno is set to indicate the error.

Last edited on
Topic archived. No new replies allowed.