c++ - Supervisor Program Forking to a Multi-threaded Child -


first off, allow me describe scenario:

i developed supervisory program on linux forks , uses execv(), in child process, launch multi-threaded application. supervisory program acting watchdog multi-threaded application. if multi-threaded application not send sigusr1 signal supervisor after period of time supervisory program kill child using pid_t fork() call , repeat process again.

here code supervisory program:

#include <sys/types.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <signal.h> #include <iostream> #include <cerrno> time_t heartbeattime;  void signalhandler(int signum) {     //std::cout << "signal (" << signum << ") received.\n";      time(&heartbeattime); }  int main(int argc, char *argv[]) {     pid_t cpid, ppid;     int result = 0;     bool programlaunched = false;     time_t now;     double timediff;     int error;     char parentid[25];     char *myargv[2];      // parent process id     ppid = ::getpid();     // initialize child process id     cpid = 0;     // copy pid char array     sprintf(parentid, "%i", ppid);     // set array pass program     myargv[0] = parentid;     myargv[1] = 0;      // print out of p pid     std::cout << "parent id: " << myargv[0] << "\n";      // register sigusr1 signal     signal(sigusr1, signalhandler);     // register sigchld children processes exit     signal(sigchld, sig_ign);      // initialize heart beat time     time(&heartbeattime);      // loop forever , ever, amen.     while (1)     {         // check see if program has been launched         if (programlaunched == false)         {             std::cout << "forking process\n";             // fork process launch application             cpid = fork();             std::cout << "child pid: " << cpid << "\n";         }          // check if fork successful         if (cpid < 0)         {             std::cout << "error in forking.\n";             // error in forking             programlaunched = false;         }         else if (cpid == 0)         {             // check if need launch application             if (programlaunched == false)             {                 // send message output                 std::cout << "launching application...\n";                 // launch application                 result = execv("./myapp", myargv);                  std::cout << "execv result = " << result << "\n";                 // check if program launched has failed                 if (result != -1)                 {                     // indicate program has been launched                     programlaunched = true;                     // exit child process                     return 0;                 }                 else                 {                     std::cout << "child process terminated; bad execv\n";                     // flag program has not been launched                     programlaunched = false;                     // exit child process                     return -1;                 }             }         }         // in parent process         else         {             // current time             time(&now);              // time difference between program heartbeat time , current time             timediff = difftime(now, heartbeattime);              // check if need restart our application             if ((timediff > 60) && (programlaunched == true))             {                 std::cout << "killing application\n";                 // kill child process                 kill(cpid, sigint);                 // indicate process ended                 programlaunched = false;                 // reset heart beat time                 time(&heartbeattime);                 return -1;             }              // check see if child application running             if (kill(cpid, 0) == -1)             {                 // error                 error = errno;                 // check if process running                 if (error == esrch)                 {                     std::cout << "process not running; start it.\n";                     // process not running.                     programlaunched = false;                     return -1;                 }             }             else             {                 // child process running                 programlaunched = true;             }         }         // give process time off.         sleep(5);     }     return 0; } 

this approach worked until ran problem library using. didn't of killing , ended tying ethernet port in endless loop of never releasing - not good.

i tried alternative method. modified supervisory program allow exit if had kill multi-threaded application , created script launch supervisor program crontab. used shell script found on stackoverflow.

#!/bin/bash #make-run.sh #make sure process running.  export display=:0 #needed if running simple gui app.  process=yourprocessname makerun="/usr/bin/program"  if ps ax | grep -v grep | grep $process > /dev/null     exit else     $makerun & fi  exit 

i added crontab run every minute. helpful , restarted supervisory program in turn restarted multi-threaded application noticed problem of multiple instances of multi-threaded application being launched. i'm not sure why happening.

i know i'm hacking i'm backed corner implementation. i'm trying work.

suggestions?


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -