QNX – Illustration 04

Le code proposé à droite est celui de l'illustration 04 proposé dans le document remis en classe.

Quelques liens pour en savoir plus sur des outils clés de cet exemple :

#include <sys/neutrino.h>
#include <iostream>
#include <cstdlib>
#include <cerrno>
#include <unistd.h>
using namespace std;

class AjusterHorlogeTR {
   _clockperiod cp_bak;
public:
   AjusterHorlogeTR(const AjusterHorlogeTR&) = delete;
   AjusterHorlogeTR& operator=(const AjusterHorlogeTR&) = delete;
   AjusterHorlogeTR(unsigned long ns) {
      _clockperiod cp;
      cp.nsec = ns;
      cp.fract = 0; // usage futur
      if (ClockPeriod(CLOCK_REALTIME, &cp, &cp_bak, 0) == -1)
         throw errno;
   }
   ~AjusterHorlogeTR() {
      ClockPeriod(CLOCK_REALTIME, &cp_bak, 0, 0);
   }
};

const sigevent *rappel(void *area, int) {
  ++(*static_cast<int*>(area));
  return nullptr;
}
void test(int &n) {
   const int INTR_HORLOGE_TR = 0;
   int fct_id = InterruptAttach(
      INTR_HORLOGE_TR, rappel, &n, sizeof n, 0
   );
   if (fct_id == -1) {
      cerr << "ERREUR: attacher a interruption " << INTR_HORLOGE_TR
           << ", code " << errno << '\n';
      exit(-1);
   }
   sleep(2);
   InterruptDetach(fct_id);
}
int main() {
   timespec ts;
   if (clock_getres(CLOCK_REALTIME, &ts) == -1) {
      cerr << "ERREUR: lecture, resolution horloge, code "
           << errno << endl;
      exit (-1);
   }
   cout << "Frequence, horloge TR:" << endl
        << "\tun tic chaque " << ts.tv_nsec << " nanoseconde (donc ~"
        << ts.tv_nsec / 1000 << " microseconde)" << endl;
   //
   // Accorder privileges pour E/S
   //
   ThreadCtl(_NTO_TCTL_IO, 0);
   int nb_rappels_normal = 0;
   test(nb_rappels_normal);
   cout << "Nb rappels, 2 secondes, rythme normal: "
        << nb_rappels_normal << endl;
   int nb_rappels_modifie = 0;
   {
      AjusterHorlogeTR ahTR(500'000);
      test(nb_rappels_modifie);
   }
   cout << "Nb rappels, 2 secondes, rythme modifie: "
        << nb_rappels_modifie << endl;
}

Valid XHTML 1.0 Transitional

CSS Valide !