void MemGraph::Timeout() { m_timer.start(static_cast<int>(updatePeriod->value()*1000)); const size_t allocated = m_capacity - GetSharedMemory().get_free_memory(); const double ratio = allocated/(double)m_capacity; graph->AddData(QDateTime::currentDateTime(),ratio); current->setText(QString::number(allocated / 1024 /1024)); currentPercent->setText(QString::number(static_cast<int>(ratio * 100))); }
MemGraph::MemGraph(QWidget* /*parent*/): m_timer(this), m_capacity(GetSharedMemory().get_size()) { setupUi(this); // this sets up GUI // signals/slots mechanism in action connect(historyLength, SIGNAL(valueChanged(double)), this, SLOT(HistoryChanged(double)) ); connect(updatePeriod, SIGNAL(valueChanged(double)), this, SLOT(PeriodChanged(double))); connect(verticalScale, SIGNAL(valueChanged(int)), this, SLOT(ScaleChanged(int))); connect(&m_timer, SIGNAL(timeout()), this, SLOT(Timeout())); m_timer.setSingleShot(true); m_timer.start(static_cast<int>(updatePeriod->value()*1000)); graph->SetHistoryLength(static_cast<int>(historyLength->value()*60)); total->setText(QString::number(m_capacity / 1024 / 1024)); }
int initsem(key_t key , int nsems) { int i; semun arg; semid_ds buf; sembuf sb; int semid; semid = semget(key,nsems , IPC_CREAT | IPC_EXCL | 0666); printf( "After semget\n"); if(semid >=0) { printf("semid > 0\n"); GetAndInitialiseSharedMemory(); printf("After initialising shared memory \n"); // prepare sembuf as it will be passed to semop to initialise the semapores in the semaphore set. sb.sem_op = 1; sb.sem_flg = 0; // arg.val initialaises the semaphore with the number of resouces available arg.val = 1; for(sb.sem_num = 0 ; sb.sem_num <nsems ; sb.sem_num ++) { if(semop(semid , &sb , 1) == -1) { // error in semop ree the semaphore int e = errno; semctl(semid , 0 , IPC_RMID); // remove the semphore errno = e; return -1; } } } else if( errno == EEXIST) { GetSharedMemory(); // the other process got the seaphore int ready = 0; semid = semget(key , nsems , 0); if(semid < 0) { // error return semid; } // wait for other process to initialise semaphore arg.buf = &buf; for(int i = 0 ; i< MAX_RETRIES ;i++) { semctl(semid , nsems-1 , IPC_STAT , arg); if( arg.buf -> sem_otime != 0 ) { ready = 1; } else { sleep(1); } } if(!ready) { errno = ETIME; return -1; } } else { return semid; // error condition } return semid; }