예제 #1
0
파일: io_device.c 프로젝트: unixo/vmbo
/*! \fn void *thread_io_device(void *parg)
 *  \brief Thread dispositivo I/O
 *  \details La funzione costituisce il corpo del thread che rappresenta il
 *  dispositivo di I/O. Il suddetto thread sospende la propria esecuzione
 *  fintanto che non siano presenti delle richieste d'accesso al dispositivo.
 *  Il thread termina la propria esecuzione quando sono stati compiuti il
 *  numero massimo d'accessi alla memoria (ad opera dell'MMU).
 *  \param parg         inutilizzato
 *  \return             inutilizzato
 */
static void *
thread_io_device(void *parg)
{
    io_entry_t *req;
    struct timespec timeout;
    int num;

    printf("--> Thread DEVICE I/O avviato [Tmin=%d, Tmax=%d]\n",
           io_dev.Tmin, io_dev.Tmax);

    while (!io_device_should_exit) {
        pthread_mutex_lock(&wait_lock);
        while ((ioreq_count == 0) && !io_device_should_exit) {
            pthread_cond_wait(&wait_cond, &wait_lock);
        }

        if (io_device_should_exit && (ioreq_count == 0)) {
            pthread_mutex_unlock(&wait_lock);
            break;
        }

        /*
         *  Estraggo la prima richiesta e la rimuovo dalla coda, aggiornando
         *  la variabile "ioreq_count" che tiene traccia di quante richieste
         *  sono ancora in attesa.
         */
        req = STAILQ_FIRST(&io_request_head);
        assert(req);

        pthread_mutex_lock(&fifo_lock);
        STAILQ_REMOVE(&io_request_head, req, io_entry, entries);
        ioreq_count--;
        pthread_mutex_unlock(&fifo_lock);

        /*
         *  Genero un numero casuale compreso nell'intervallo chiuso
         *  [Tmin,Tmax] utile a simulare il reperimento dell'informazione dal
         *  dispositivo.
         */
        num = bounded_rand(io_dev.Tmin, io_dev.Tmax);
        timeout.tv_sec = 0;
        timeout.tv_nsec = num * 1000000;
        nanosleep(&timeout, NULL);
        fprintf(LOG_FILE(req->procnum),
                "Richiesta d'accesso servita in %d ms\n", num);

        /*
         *  Aggiorno le statistiche e "risveglio" il processo che ha fatto la
         *  richiesta.
         */
        io_dev.req_count++;
        proc_table[req->procnum]->stats.io_requests++;
        proc_table[req->procnum]->stats.time_elapsed += num;
        pthread_mutex_unlock(&wait_lock);
        pthread_cond_signal(&proc_table[req->procnum]->io_cond);
        XFREE(req);
    }
    printf("<-- Thread DEVICE I/O terminato\n");
    pthread_exit(NULL);
}
예제 #2
0
insertion_data<T> make_insertion_data_range(std::size_t count, std::size_t size,
                                            std::uint32_t seed) {
  seed_seq seq{seed};
  random_engine engine{seq};
  random_bits_engine<T> bits_engine{seq};
  insertion_data<T> data;
  data.indexes.reserve(count);
  data.ordered.reserve(count * size);

  for (std::size_t i = 0; i != count; ++i) {
    for (std::size_t j = 0; j != size; ++j)
      data.ordered.push_back(bits_engine());
    data.indexes.push_back(bounded_rand(engine, i * size + 1));
  }

  return data;
}