Ignore:
Timestamp:
May 29, 2006, 7:27:35 PM (19 years ago)
Author:
ansari
Message:

Ajout gestionnaire signal ds ZThread et correction/adaptation gestion des threads (killthr) dans Commander - Reza 29 mai 2006

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/SophyaLib/SysTools/commander.cc

    r2943 r2955  
    443443static Commander* cur_commander = NULL;
    444444/* --Methode-- */
    445 //! Default constructor. Initializes variable list and copies \c history.pic to \c hisold.pic
    446 Commander::Commander()
     445/*!
     446  \brief Constructor. Initializes variable list and copies \c history.pic to \c hisold.pic
     447  if \b fgsigstop == true , calls ZThread::ActivateExitOnSignal()
     448*/
     449Commander::Commander(bool fgsigzt)
    447450{
    448451system("cp history.pic hisold.pic");
     
    512515usage += "  > thrlist       # List of command execution threads (& as the last character) \n";
    513516usage += "  > clearthrlist  # Removes finished threads from the list  \n";
    514 usage += "  > stopthr Id    # Try to stop a given thread (ThrId=id) sending SIGUSR1 \n";
     517usage += "  > killthr Id    # Try to stop a given thread (ThrId=id) by sending SIGUSR1 \n";
     518usage += "  > cancelthr Id  # Try to cancel a given thread (ThrId=id)  \n";
    515519usage += "  > waitthr       # Waits until all active threads have finished (join()) \n";
    516520usage += "  > exec filename # Execute commands from file \n";
     
    577581AddInterpreter(this);
    578582curcmdi = this;
     583if (fgsigzt) ZThread::ActivateExitOnSignal(SIGUSR1);
    579584}
    580585
     
    18781883  return(0);
    18791884}
    1880 else if (kw == "stopthr") {
    1881   if (tokens.size() < 1) { cout << "Commander::Interpret() Usage: stopthr thrid" << endl;  return(0); }
     1885else if ( (kw == "killthr") || (kw == "cancelthr") )  {
     1886  if (tokens.size() < 1) {
     1887    cout << "Commander::Interpret() Usage: killthr/cancelthr thrid" << endl; 
     1888    return(0);
     1889  }
    18821890  uint_8  id = atol(tokens[0].c_str());
    1883   StopThr(id);
     1891  bool fgkill = false;
     1892  if (kw == "killthr")  fgkill = true;
     1893  StopThr(id, fgkill);
    18841894  return (0);
    18851895}
     
    20262036void Commander::ListThreads()
    20272037{
    2028   cout << "---- Commander::ListThreads()  List of separate execution threads NThread="
    2029        <<  CmdThrExeList.size() << " ----- " << endl;
     2038  cout << "--- Commander::ListThreads()- command execution threads NThread="
     2039       <<  CmdThrExeList.size() << " ---" << endl;
    20302040  for(list<CommandExeThr *>::iterator tit = CmdThrExeList.begin();
    20312041      tit != CmdThrExeList.end(); tit++) {
    20322042    cout << "Id=" << (*tit)->Id();
    2033     if ( (*tit)->IfDone() )  cout << " Finished , Rc= " << (*tit)->getRC();
    2034     else cout << " Executing";
    2035     cout << " (Cmd= " << (*tit)->Keyword() << " " << (*tit)->Tokens() << " )" << endl;
    2036   }
    2037 }
    2038 /* --Methode-- */
    2039 void Commander::StopThr(uint_8 id)
     2043    if ( (*tit)->IfRunning() )  cout << " Executing";
     2044    else if ( (*tit)->IfDone() )  cout << " Finished , Rc= " << (*tit)->getRC();
     2045    else cout << " Stopped/Canceled";
     2046    cout << " (Cmd= " << (*tit)->Keyword() << " " << (*tit)->Tokens().substr(0,35);
     2047    if ((*tit)->Tokens().length() > 35) cout << "... )" << endl;
     2048    else cout << " )" << endl;
     2049  }
     2050}
     2051/* --Methode-- */
     2052void Commander::StopThr(uint_8 id, bool fgkill)
    20402053{
    20412054  for(list<CommandExeThr *>::iterator tit = CmdThrExeList.begin();
    20422055      tit != CmdThrExeList.end(); tit++) {
    2043     if ( ((*tit)->Id() == id) && !((*tit)->IfDone()) ) {
    2044       (*tit)->kill(SIGUSR1); 
    2045       cout << "Commander::StopThr()  Send signal SIGUSR1 to Thread Id= " << id << endl;
     2056    if ( ((*tit)->Id() == id) && ((*tit)->IfRunning()) ) {
     2057      if (fgkill) {
     2058        (*tit)->kill(SIGUSR1); 
     2059        cout << "Commander::StopThr()  Send signal SIGUSR1 to Thread Id= " << id << endl;
     2060      }
     2061      else {
     2062        (*tit)->cancel();
     2063        cout << "Commander::StopThr()  Canceling Thread Id= " << id << endl;
     2064      }
    20462065      return;
    20472066    }
     
    20552074  cout << "---- Commander::CleanThrList()  Cleaning thrlist  ----- \n";
    20562075  list<CommandExeThr *> thrcopie;
     2076  int ncl = 0;
    20572077  for(list<CommandExeThr *>::iterator tit = CmdThrExeList.begin();
    20582078      tit != CmdThrExeList.end(); tit++) {
    2059     if ( (*tit)->IfDone() ) {
     2079    if ( ((*tit)->IfEnded() || (*tit)->IfStopped()) && (ncl < 3) ) {
    20602080      cout << " Thread Id= " << (*tit)->Id() << " rc= " << (*tit)->getRC() << " Cleaned" << endl;
    20612081      delete (*tit);
     2082      ncl++;
    20622083    }
    20632084    else thrcopie.push_back((*tit));
Note: See TracChangeset for help on using the changeset viewer.