[c++/linux]possix_spawn und waitpid

kickerxy123

Erfahrenes Mitglied
Code:
bool createProcess(string pathWithExe, string arguments)
{
  replace(&pathWithExe, "\\", "/");
  replace(&pathWithExe, "//", "/");

     fflush(nullptr);
     pid_t pid;


   string pathEnv = "";
   string rawPathWithExe = pathWithExe;

   if(true)
   {
     pathWithExe    = getValidAbsolutePath(pathWithExe);
     pathEnv      = getSuperiorFolder(pathWithExe);
     rawPathWithExe    = pathWithExe;
     pathWithExe    = "\"" + pathWithExe + "\"";
   }
   string args = arguments;
   arguments   = pathWithExe + " " + arguments;

   vector<string> a;
   split(rawPathWithExe, "/", &a);
   if(a.size() < 1) return false;
     int status;
     if(args == "")
     {
  char *argv[] = {(char*)a[a.size()-1].c_str(), (char *) 0};
  status = posix_spawn(&pid, pathWithExe.c_str(), nullptr, nullptr, argv, environ);
     }
     else
     {
      char *argv[]  = {(char*)a[a.size()-1].c_str(), (char*)args.c_str(), (char *) 0};
      status = posix_spawn(&pid, pathWithExe.c_str(), nullptr, nullptr, argv, environ);
     }

   if(status != 0)
   {

  const string err = strerror(status);
       ccout("failed to posix_spawn process! Err: " + err);
       return false;
       #endif
   }

   sleep(1000);

   sleep(50);

     fflush(nullptr);

     while(true)
     {
  sleep(500);
  int wpid;
  if((wpid=waitpid(pid, &status, WNOHANG)) < 1)
  {
  if(wpid == -1)
  {
  ccout("failed to waitpid for pid=" + _STRING(pid) + ": " + _STRING(errno) + "!\n");
  return false;
  }
  else
  {
  if(WIFEXITED(status) == 0){ ccout("process successfully finished.\n");}
  else ccout("process finished with error\n");
  break; // end of children runtime
  }
  }
  cout << "running\n";
     }

   }
Habe es zusammengekürzt, aber sollte noch passen...
Aufgerufen wird es mit createProcess("testApp", "");
Ich erhalte:
1. "running"
2. "failed to waitpid for *irgendeine Pid*: 10"


(10 == ECHILD)

Letzlich bin ich mir nicht einmal sicher, ob der Prozess überhaupt korrekt spawned, da ich keinerlei console Output von ihm erhalte, dieser aber direkt am Anfang cout << irgendwas; ausgibt.....
Aber possix_spawn scheint ja kein Fehler zurück zu geben.

Kann mir jemand helfen? Bin nicht so die große Leuchte im Linux Bereich

vg
kickerxy

#edit: warum gibt es keine cpp tags die das ganze schöner formatieren würde...
#edit: centos 6.5 x86
 
Zuletzt bearbeitet:

Neue Beiträge

Zurück