popen und mehrfach pipes

Brusko

Well-Known Member
Guten Abend,

ich arbeire gerade an diesem Code wo eine Funktion den mpg123 player startet und die andere digitemp,

Code:
void radioplayer ()  // Radio in wartestellung starten und stream für sender&title erstellen
    {
     ostringstream cmd;
     string bufferstring;
     int big_string , pos_one , pos_two;
     char buffer[160];
     cmd << "mpg123 -R < /tmp/test";
     FILE* radioplayer = popen(cmd.str().c_str(),"r");
     while (fgets(buffer,160,radioplayer))
          {
           bufferstring=buffer;
           if (bufferstring.find("ICY-META") != string::npos)
             {
              big_string = bufferstring.size();
              pos_one = bufferstring.find("'");
              bufferstring.erase(0,pos_one + 1);
              pos_two = bufferstring.find(";");
              bufferstring.erase(pos_two - 1);
              pthread_mutex_lock (&radiomutex);
              interpret_title = bufferstring;
              pthread_mutex_unlock (&radiomutex);
              sleep(10);
             }
           else if (bufferstring.find("ICY-NAME") != string::npos)
                  {
                   big_string = bufferstring.size();
                   pos_one = bufferstring.find(":");
                   bufferstring.erase(0,pos_one + 2);
                   pthread_mutex_lock (&radiomutex);
                   radiosender = bufferstring;
                   pthread_mutex_unlock (&radiomutex);
                  }
           bufferstring.erase();
          }
     pclose(radioplayer);
     pthread_mutex_lock (&radiomutex);
     radiosender.erase();
     interpret_title.erase();
     pthread_mutex_unlock (&radiomutex);
    }

und für Digitemp/mbmon

Code:
void readout_temperature (string what , int which, int answer, int to_sleep_late ) //Temperaturen auslesen und Fuehler initialisieren
    {
     ostringstream cmd;
     if (what=="room") 
       {
        cmd << "/usr/local/bin/digitemp_DS9097 -o\"Sensor %R %.2C\" -t " << which << " | awk '/Sensor/ {print $2 , $3}'";
        what.erase();
       }
     else if (what=="harddisktemp")
            {
             cmd << "/usr/local/sbin/smartctl -a /dev/ad0 | awk '/^194/ {print \"Harddisktemp \" $10}'";
             what.erase();
            }
     else if (what=="CPUtemp")
            {
             cmd << "/usr/local/bin/mbmon -T2 -c1 | awk '{print \"CPUtemp \" $1}'";
             what.erase();
            }
     else if (what=="init")
            {
             cmd << "/usr/local/bin/digitemp_DS9097 -s /dev/cuad0 -i >/dev/null 2>&1";
             what.erase();
            }
     FILE* temppipe = popen(cmd.str().c_str(), "r");
     sleep(to_sleep_late); // mus schlafen da const lesefehler, bei init laenger schlafen !!
     if (answer == 1) // wegen rueckanwort im vector
       {
        char buf[40], *bufsensorid, *buftemp;
        float temperaturefloat;
        stringstream temperaturestream;
        string sensoridstring, temperaturestring;
        while (fgets(buf,30,temppipe))
             {	
              chomp(buf);
              bufsensorid = buf;
              buftemp = strchr(bufsensorid,' ');
              *buftemp++ ='\0';
              sensoridstring=bufsensorid;
              temperaturestring=buftemp;
              temperaturestream << temperaturestring;
              temperaturestream >> temperaturefloat;
              pthread_mutex_lock (&sensoridmutex);
              sensorid[which]=sensoridstring;
              pthread_mutex_unlock (&sensoridmutex);
              pthread_mutex_lock (&tempmutex);
              temperature[which] = temperaturefloat;
              pthread_mutex_unlock (&tempmutex);
              sensoridstring.erase();
              temperaturestring.erase();
              cout << temperature[which] << " " << sensorid[which] <<"\n";
              sensoridstring.erase();
              temperaturestring.erase();
             }
       }
     pclose(temppipe);
    }

wenn ich beide einzelnd verwende klappt das, aber verwende ich beide zusammen geht nur der erste (radioplay). Beide laufen mit pthread in unterschiedlichen Threads.
Ich gehe mal von aus das es daran liegt, das im Radioplay die pipe nicht geschlossen wird da Radioplay ständig sendet.
Kann man nur eine Pipe mit popen öffnen ?

mfg Michael
 
Zurück
Oben