Advertisement
cunha1

Untitled

Mar 24th, 2020
182
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. // 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.             cout << endl << "Proces: " << i << ", K.O.  br: " << k << " " << "("
  59.             << m << "/5)" << endl;
  60.             sleep(1);
  61.         }
  62.         izadi_iz_kriticnog_odsjecka (i, BROJ);
  63.     }
  64. }
  65.  
  66. void
  67. brisi (int sig)
  68. {
  69.     shmdt (TRAZIM);
  70.     shmdt (BROJ);
  71.     shmctl (Id, IPC_RMID, NULL);
  72.     exit (0);
  73. }
  74.  
  75.  
  76. int
  77. main () {
  78.     Id = shmget (IPC_PRIVATE, sizeof (int) * 100, 0600);
  79.     if (Id == -1) {
  80.       exit (1);
  81.     }
  82.     sigset (SIGINT, brisi);
  83.     cout<<"int *)N: "; cin>>n;
  84.     TRAZIM = (int*)shmat (Id, NULL, 0);
  85.     BROJ = (int*) shmat (Id, NULL, 0);
  86.     for(int i=1;i<=n;i++) {
  87.         switch(fork()) {
  88.             case 0: {
  89.                 proc(i,TRAZIM,BROJ);
  90.                 exit(0);
  91.                 break;
  92.             }
  93.             default: {
  94.                 break;
  95.             }
  96.         }
  97.         for(int i=0;i<n;i++){
  98.             wait(NULL);
  99.         }
  100.     }
  101.    
  102.     brisi (0);
  103.     sleep(10);    
  104.     return 0;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement