Advertisement
cunha1

Untitled

Mar 24th, 2020
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.22 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. // nemres dinamicki alocirat array u mainu s duljinom n jer se tu jos n ne zna
  15. int *TRAZIM = new int[10];
  16. int *BROJ = new int[10] {0};
  17.  
  18. int
  19. max (int BROJ[])
  20. {
  21.     int max = BROJ[0];
  22.     for (int i = 1; i < n; i++) {
  23.         if (BROJ[i] > max) {
  24.             max = BROJ[i];
  25.         }
  26.     }
  27.     return max;
  28. }
  29.  
  30. void
  31. udi_u_kriticni_odsjecak (int i, int TRAZIM[], int BROJ[])
  32. {
  33.     TRAZIM[i] = 1;
  34.     BROJ[i] = max(BROJ) + 1;
  35.     TRAZIM[i] = 0;
  36.  
  37.     for (int j = 0; j < n; j++) { // do n-1 je zapravo <= n-1, to jest <n
  38.         while (TRAZIM[j] != 0) { // <> znaci != u pseudokodu
  39.             // ne ispisuj nist dok piste nista, to je radno cekanje
  40.         }
  41.         while (BROJ[j] != 0
  42.          && (BROJ[j] < BROJ[i] || (BROJ[j] == BROJ[i] && j < i))) {
  43.         }
  44.     }
  45. }
  46.  
  47. void
  48. izadi_iz_kriticnog_odsjecka (int i, int BROJ[]) {
  49.     BROJ[i] = 0;
  50. }
  51.  
  52. void
  53. proc (int i, int TRAZIM[], int BROJ[])
  54. {
  55.     for (int k = 1; k <= 5; k++) {
  56.         udi_u_kriticni_odsjecak (i, TRAZIM, BROJ);
  57.         for (int m = 1; m <= 5; m++) {
  58.            
  59.             cout << endl << "Proces: " << i << ", K.O.  br: " << k << " " << "("
  60.             << m << "/5)" << endl;
  61.             sleep(1);
  62.            
  63.         }
  64.         izadi_iz_kriticnog_odsjecka (i, BROJ);
  65.     }
  66. }
  67.  
  68. void
  69. brisi (int sig)
  70. {
  71.     shmdt (TRAZIM);
  72.     shmdt (BROJ);
  73.     shmctl (Id, IPC_RMID, NULL);
  74.     exit (0);
  75. }
  76.  
  77.  
  78. int
  79. main () {
  80.     Id = shmget (IPC_PRIVATE, sizeof (int) * 100, 0600);
  81.     if (Id == -1) {
  82.       exit (1);
  83.     }
  84.     sigset (SIGINT, brisi);
  85.     cout<<"int *)N: "; cin>>n;
  86.     TRAZIM = (int*)shmat (Id, NULL, 0);
  87.     BROJ = (int*) shmat (Id, NULL, 0);
  88.     for(int i=1;i<=n;i++) {
  89.         switch(fork()) {
  90.             case 0: {
  91.                 proc(i,TRAZIM,BROJ);
  92.                 exit(0);
  93.                 break;
  94.             }
  95.             default: {
  96.                 break;
  97.             }
  98.         }
  99.        
  100.     }
  101.     for(int i=0;i<n;i++){
  102.         wait(NULL);
  103.     }
  104.     brisi (0);
  105.     sleep(10);    
  106.     return 0;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement