serial_t* Create_Serial() { int i=0; serial_t *new_serial = (struct serializer *)malloc(sizeof(struct serializer)); new_serial->no_of_queues = 0; new_serial->crowd_count = 0; new_serial->queues[0] = Create_Queue(new_serial); new_serial->queues[1] = Create_Queue(new_serial); sem_init(&(new_serial->queue_lock),0,1); return new_serial; }
serial_t Create_Serial() { //Allocate space serial_t new_serial = (struct SERIAL_T* ) malloc(sizeof(struct SERIAL_T)); //Initialize mutex lock pthread_mutex_init(&(new_serial->mutexlock),NULL); //Initialize other variables new_serial->queue_count = 0; new_serial->crowd_count = 0; //Entry Q Create_Queue(new_serial); //Crowd leave Q Create_Queue(new_serial); return new_serial; }
//Initializes the disk scheduler void Init_ds(int ncylinders, float CylinderSeekTime) { // Instantiate the serializer, queues for requests in the upward and downward direction, and a // crowd to encapsulate active requests serializer = Create_Serial(); up_queue = Create_Queue(serializer); down_queue = Create_Queue(serializer); disk_access_crowd = Create_Crowd(serializer); //Initialize the disk's state variables cylinder_count = ncylinders; disk_access_sequence_number = 1; disk_request_sequence_number = 1; head_position = 0; }
Void QMSS_SemaphoreMP_init(SemaphoreMP_Object *obj, Int count) { int i; obj->QMSS_sem_Hnd = Create_Queue(); for(i=0 ; i< count ; i++) { queue_push_elem(obj->QMSS_sem_Hnd ,1); } }
void Init_dp(int nphilosophers) { nphil = nphilosophers; ser = Create_Serial(); waiting_q = Create_Queue(ser); eat_crowd = Create_Crowd(ser); think_crowd = Create_Crowd(ser); // init the forks array forks = malloc(sizeof(sem_t) * nphil); int i; for(i = 0; i < nphil; i++) sem_init(&(forks[i]), 0, 1); }
//Unit Test our Queues int main(int argc, const char *argv[]) { my_serial = Create_Serial(); queue_t my_queue = Create_Queue(my_serial); pthread_t thread_ID1,thread_ID2 ; void* exitstatus ; pthread_create(&thread_ID1 , NULL, testMtd ,(void*) my_queue ) ; pthread_create(&thread_ID2 , NULL, testMtd ,(void*) my_queue ) ; pthread_join(thread_ID2 , &exitstatus ) ; pthread_join(thread_ID1 , &exitstatus ) ; }