Advertisement
cunha1

Untitled

Mar 11th, 2020
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.01 KB | None | 0 0
  1. #include <iostream>
  2. #include <time.h>
  3. #include <signal.h>
  4. #include <unistd.h>
  5. #include <array>
  6.  
  7. using namespace std;
  8.  
  9. // ovo je obicno polje, ali definirano preko std::array jer ima neke dodatne funkcije koje nas Lovrencic nije naucio (npr array.size())
  10. array<int,4> PRIORITET;
  11. array<int,3> sigs = {SIGINT,SIGQUIT,SIGTSTP}; // signali poredani po prioritetu, SIGINT najnizi, SIGTSTP najvisi
  12.  
  13. int TEKUCI_PRIORITET;
  14.  
  15. void obrada_prekida(int j)
  16. {
  17.    cout << "Poceo obradu prekida " << j << endl;
  18.    for (int i = 1; i < 6; i++) {
  19.       cout << "Obrada prekida " << j << ": " << i << "/5" << endl;
  20.       sleep(1);
  21.    }
  22.    
  23.    cout << "Zavrsio obradu prekida " << j << endl;
  24. }
  25.  
  26. void prekidna_rutina (int sig)
  27. {
  28.    int i;
  29.    time_t t;
  30.  
  31.    time(&t);
  32.    cout << "Prekidna rutina pozvana u: " << ctime(&t) << endl;
  33.  
  34.    // u ovom switchu se odreduje koji je prioritet pozvanog prekida (i-prioritet)
  35.    switch (sig) {
  36.       case SIGINT:
  37.          i = 1;
  38.          break;
  39.       case SIGQUIT:
  40.          i = 2;
  41.          break;
  42.       case SIGTSTP:
  43.          i = 3;
  44.          break;
  45.       default:
  46.          return;
  47.    }
  48.    cout << "Razina prekida: " << i << endl;
  49.    if(i > TEKUCI_PRIORITET) {
  50.       PRIORITET[i] = TEKUCI_PRIORITET;
  51.       TEKUCI_PRIORITET = i;
  52.       // ignoriramo signale nizeg prioriteta
  53.       for(size_t x=0;x<sigs.size();x++) {
  54.          if(i > x) sigignore(sigs[x]);
  55.       }
  56.       obrada_prekida(i);
  57.       // nakon obradenog prekida, pratimo signale manje od njega
  58.       for(size_t x=0;x<sigs.size();x++) {
  59.          if(i > x) sigset(sigs[x], prekidna_rutina);
  60.       }
  61.       TEKUCI_PRIORITET = PRIORITET[i];
  62.    }
  63. }
  64.  
  65. int main (void)
  66. {
  67.    // cim pokrenemo program, cekamo signale
  68.    for(size_t x=0;x<sigs.size();x++) {
  69.       sigset(sigs[x], prekidna_rutina);
  70.    }
  71.    cout << "Poceo osnovni program " << endl;
  72.    for (int i = 1; i < 31; i++)
  73.    {
  74.       cout << "Glavni program: " << i << "/30" << endl;
  75.       sleep(1);
  76.    }
  77.    cout << "Zavrsio osnovni program" << endl;
  78.    return 0;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement