Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <pthread.h>
- #include <sys/neutrino.h>
- #include <sys/netmgr.h>
- #include <iostream>
- #include <sys/wait.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <sys/iofunc.h>
- #include <sys/dispatch.h>
- #include <stdio.h>
- using namespace std;
- int capacity = 100;
- int current_capacity = 55;
- static int countBoilers = 0;
- struct params
- {
- int rid;
- int msg;
- };
- struct params2
- {
- int pid;
- int chid;
- int coid;
- };
- void *CountdownBurningFuel()
- {
- while(true)
- {
- if(current_capacity > 1)
- {
- cout << "current_capacity barrel: ";
- cout << current_capacity << endl;
- current_capacity--;
- delay(1000);
- }
- }
- }
- void *BoilerWorking(int* coid)
- {
- static pthread_t tid;
- int id = countBoilers;
- int countFuelNeed = 0;
- int FuelFromMachine = 0;
- cout << "Boiler #" << id << " Working"<<endl;
- cout << "Boiler #" << id << " Connected"<<endl;
- cout << *coid<<endl;
- pthread_create (&tid, 0,(void* (*)(void*)) CountdownBurningFuel ,0);
- while(true)
- {
- int buf;
- if(current_capacity < 50)
- {
- cout << "Send to P1" << endl;
- countFuelNeed = capacity - current_capacity;
- if(countFuelNeed <= 0)
- countFuelNeed = capacity;
- int res = MsgSend(*coid,&countFuelNeed,sizeof(countFuelNeed),&FuelFromMachine,sizeof(FuelFromMachine));
- if(res == -1)
- {
- cout << "Error Send to P1"<< " Boiler #" << id <<endl;
- continue;
- }
- //boiler get from machina fuel
- delay(500);
- current_capacity+=FuelFromMachine;
- cout << "current_capacity barrel: ";
- cout << current_capacity << endl;
- }
- }
- }
- void RunBoilers(int n)
- {
- static int chid = 0;
- static int pid = 0;
- params2 param;
- int fd = open ("/dev/shmem/p1_var",O_RDWR);
- read (fd, &pid, 4);
- read (fd, &chid, 4);
- param.pid = pid;
- param.chid = chid;
- static int coid = ConnectAttach(0,pid,chid,0,0);
- cout << coid << endl;
- if(coid == -1)
- {
- cout << "Error ConnectAttach return -1" << endl;
- return;
- }
- for(int i = 0;i < n;i++)
- {
- static pthread_t tid;
- countBoilers++;
- pthread_create (&tid, 0,(void* (*)(void*)) BoilerWorking ,&coid);
- }
- }
- int main()
- {
- RunBoilers(4);
- getchar();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement