Advertisement
cunha1

Untitled

Mar 24th, 2020
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.20 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <sys/types.h>
  4. #include <sys/wait.h>
  5. #include <sys/ipc.h>
  6. #include <sys/shm.h>
  7. #include <unistd.h>
  8. #include <signal.h>
  9.  
  10. using namespace std;
  11.  
  12. int Id;
  13. int n;
  14.  
  15. struct tshared {
  16.     int TRAZIM[10];
  17.     int BROJ[10];
  18. };
  19.  
  20. tshared* share;
  21.  
  22.  
  23.  
  24.  
  25. int
  26. max (int BROJ[])
  27. {
  28.     int max = BROJ[0];
  29.     for (int i = 1; i < n; i++) {
  30.         if (BROJ[i] > max) {
  31.             max = BROJ[i];
  32.         }
  33.     }
  34.     return max;
  35. }
  36.  
  37. void
  38. udi_u_kriticni_odsjecak (int i, tshared* share)
  39. {
  40.     share->TRAZIM[i] = 1;
  41.     share->BROJ[i] = max(share->BROJ) + 1;
  42.     share->TRAZIM[i] = 0;
  43.  
  44.     for (int j = 0; j < n; j++) { // do n-1 je zapravo <= n-1, to jest <n
  45.         while (share->TRAZIM[j] != 0) { // <> znaci != u pseudokodu
  46.             // ne ispisuj nist dok piste nista, to je radno cekanje
  47.         }
  48.         while (share->BROJ[j] != 0
  49.          && (share->BROJ[j] < share->BROJ[i] || (share->BROJ[j] == share->BROJ[i] && j < i))) {
  50.         }
  51.     }
  52. }
  53.  
  54. void
  55. izadi_iz_kriticnog_odsjecka (int i, tshared* share) {
  56.     share->BROJ[i] = 0;
  57. }
  58.  
  59. void
  60. proc (int i, tshared* share)
  61. {
  62.     for (int k = 1; k <= 5; k++) {
  63.         udi_u_kriticni_odsjecak (i, share);
  64.         for (int m = 1; m <= 5; m++) {
  65.            
  66.             cout << endl << "Proces: " << i+1 << ", K.O.  br: " << k << " " << "("
  67.             << m << "/5)" << endl;
  68.             sleep(1);
  69.         }
  70.         izadi_iz_kriticnog_odsjecka (i, share);
  71.     }
  72. }
  73.  
  74. void
  75. brisi (int sig)
  76. {
  77.     shmdt (share->TRAZIM);
  78.     shmdt (share->BROJ);
  79.     shmctl (Id, IPC_RMID, NULL);
  80.     exit (0);
  81. }
  82.  
  83. int
  84. main () {
  85.     Id = shmget (IPC_PRIVATE, sizeof (int) * 100, 0666);
  86.     if (Id == -1) {
  87.       exit (1);
  88.     }
  89.     sigset (SIGINT, brisi);
  90.     cout<<"int *)N: "; cin>>n;
  91.    
  92.     share = (tshared*)shmat (Id, NULL, 0);
  93.     for(int i=0;i<n;i++) {
  94.         switch(fork()) {
  95.             case 0: {
  96.                 proc(i,share);
  97.                 exit(0);
  98.                 break;
  99.             }
  100.             default: {
  101.                 break;
  102.             }
  103.         }
  104.        
  105.     }
  106.  
  107.    
  108.     for(int i=0;i<n;i++){
  109.         wait(NULL);
  110.     }
  111.     brisi (0);
  112.     sleep(10);    
  113.     return 0;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement