Advertisement
Guest User

p2.cpp

a guest
Dec 9th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.26 KB | None | 0 0
  1. #include <pthread.h>
  2. #include <sys/neutrino.h>
  3. #include <sys/netmgr.h>
  4. #include <iostream>
  5. #include <sys/wait.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <fcntl.h>
  9. #include <sys/iofunc.h>
  10. #include <sys/dispatch.h>
  11. #include <stdio.h>
  12.  
  13. using namespace std;
  14.  
  15.  
  16.     int capacity = 100;
  17.     int current_capacity = 55;
  18.     static int countBoilers = 0;
  19.     struct params
  20.     {
  21.      int rid;
  22.      int msg;
  23.     };
  24.     struct params2
  25.     {
  26.         int pid;
  27.         int chid;
  28.         int coid;
  29.     };
  30.  
  31.     void *CountdownBurningFuel()
  32.     {
  33.         while(true)
  34.             {
  35.                 if(current_capacity > 1)
  36.                 {
  37.                     cout << "current_capacity barrel: ";
  38.                     cout << current_capacity << endl;
  39.                     current_capacity--;
  40.                     delay(1000);       
  41.                 }
  42.             }
  43.     }
  44.     void *BoilerWorking(int* coid)
  45.     {
  46.         static pthread_t tid;
  47.         int id = countBoilers;
  48.         int countFuelNeed = 0;
  49.         int FuelFromMachine = 0;
  50.         cout <<  "Boiler #" << id << " Working"<<endl; 
  51.         cout <<  "Boiler #" << id << " Connected"<<endl;
  52.         cout << *coid<<endl;
  53.        
  54.         pthread_create (&tid, 0,(void* (*)(void*)) CountdownBurningFuel ,0);
  55.         while(true)
  56.         {
  57.         int buf;
  58.    
  59.        
  60.         if(current_capacity < 50)
  61.         {
  62.             cout << "Send to P1" << endl;
  63.             countFuelNeed = capacity - current_capacity;
  64.             if(countFuelNeed <= 0)
  65.                 countFuelNeed = capacity;
  66.            
  67.             int res = MsgSend(*coid,&countFuelNeed,sizeof(countFuelNeed),&FuelFromMachine,sizeof(FuelFromMachine));
  68.             if(res == -1)
  69.             {
  70.                 cout << "Error Send to P1"<< " Boiler #" << id <<endl;
  71.                 continue;
  72.             }
  73.             //boiler get from machina fuel
  74.             delay(500);
  75.             current_capacity+=FuelFromMachine;
  76.             cout << "current_capacity barrel: ";
  77.                 cout << current_capacity << endl;
  78.         }
  79.    
  80.     }
  81.        
  82.  }
  83.  
  84.  
  85.  
  86. void RunBoilers(int n)
  87. {  
  88.    
  89.    
  90.     static int chid = 0;
  91.     static int pid = 0;
  92.     params2 param;
  93.     int fd = open ("/dev/shmem/p1_var",O_RDWR);
  94.     read (fd, &pid, 4);
  95.     read (fd, &chid, 4);
  96.     param.pid = pid;
  97.     param.chid = chid;
  98.     static int coid = ConnectAttach(0,pid,chid,0,0);
  99.     cout << coid << endl;
  100.     if(coid == -1)
  101.         {
  102.             cout << "Error ConnectAttach return -1" << endl;
  103.             return;
  104.         }
  105.     for(int i = 0;i < n;i++)
  106.     {
  107.         static pthread_t tid;
  108.         countBoilers++;
  109.         pthread_create (&tid, 0,(void* (*)(void*)) BoilerWorking ,&coid);
  110.  
  111.     }
  112.  
  113.            
  114. }
  115. int main()
  116. {
  117.     RunBoilers(4);
  118.     getchar();
  119.     return 0;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement