//---------------------------------------------------------------------- // // ProcessModuleInit // // Initialize the process module. This involves initializing all // of the process control blocks to appropriate values (ie, free // and available). We also need to initialize all of the queues. // //---------------------------------------------------------------------- void ProcessModuleInit () { int i; dbprintf ('p', "ProcessModuleInit: function started\n"); AQueueInit (&freepcbs); AQueueInit(&runQueue); AQueueInit (&waitQueue); AQueueInit (&zombieQueue); // For each PCB slot in the global pcbs array: for (i = 0; i < PROCESS_MAX_PROCS; i++) { dbprintf ('p', "Initializing PCB %d @ 0x%x.\n", i, (int)&(pcbs[i])); // First, set the internal PCB link pointer to a newly allocated link if ((pcbs[i].l = AQueueAllocLink(&pcbs[i])) == NULL) { printf("FATAL ERROR: could not allocate link in ProcessModuleInit!\n"); GracefulExit(); } // Next, set the pcb to be available pcbs[i].flags = PROCESS_STATUS_FREE; // Finally, insert the link into the queue if (AQueueInsertFirst(&freepcbs, pcbs[i].l) != QUEUE_SUCCESS) { printf("FATAL ERROR: could not insert PCB link into queue in ProcessModuleInit!\n"); GracefulExit(); } } // There are no processes running at this point, so currentPCB=NULL currentPCB = NULL; dbprintf ('p', "ProcessModuleInit: function complete\n"); }
int CondInit(Cond *c) { if (!c) return SYNC_FAIL; if (AQueueInit (&c->waiting) != QUEUE_SUCCESS) { printf("FATAL ERROR: could not initialize lock waiting queue in CondInit!\n"); exitsim(); } return SYNC_SUCCESS; }
//------------------------------------------------------- // // mbox_t MboxCreate(); // // Allocate an available mailbox structure for use. // // Returns the mailbox handle on success // Returns MBOX_FAIL on error. // //------------------------------------------------------- mbox_t MboxCreate() { mbox_t mbox_no = MBOX_NUM_MBOXES; for(i = 0; i < MBOX_NUM_MBOXES;i++) { if(MailBox[i].inuse == false) { mbox_no = i; break; } } if(mbox_no == MBOX_NUM_MBOXES) { printf("MailBox cannot be assigned to the calling process : %d\n", GetCurrentPid()); return MBOX_FAIL; } MailBox[mbox_no].inuse = true; if((MailBox[mbox_no].lock = LockCreate()) == SYNC_FAIL) { printf("MailBox cannot associate a lock with itself for calling process : %d\n", GetCurrentPid()); return MBOX_FAIL; } if((MailBox[mbox_no].moreData = CondCreate(MailBox[mbox_no].lock)) == SYNC_FAIL) { printf("MailBox cannot associate a cond var for buffer emptiness calling process : %d\n", GetCurrentPid()); return MBOX_FAIL; } if((MailBox[mbox_no].moreSpace = CondCreate(MailBox[mbox_no].lock)) == SYNC_FAIL) { printf("MailBox cannot associate a cond var for buffer saturation for calling process : %d\n", GetCurrentPid()); return MBOX_FAIL; } if(AQueueInit(&MailBox[mbox_no].buffers) == QUEUE_FAIL) { printf("FATAL Error : Available mailbox : %d cannot have its buffer queue initialized for process : %d\n", mbox_no, GetCurrentPid()); return MBOX_FAIL; //exitsim(); } for(i = 0; i < PROCESS_MAX_PROCS; i++) { MailBox[mbox_no].procs_link[i] = 0; } //MailBox[mbox_no].tail = 0; //MailBox[mbox_no].head = 1; MailBox[mbox_no].process_count = 0; //printf("Created mailbox with handle : %d\n", mbox_no); return mbox_no; }
//--------------------------------------------------------------------- // // SemInit // // Initialize a semaphore to a particular value. This just means // initting the process queue and setting the counter. // //---------------------------------------------------------------------- int SemInit (Sem *sem, int count) { if (!sem) return SYNC_FAIL; if (AQueueInit (&sem->waiting) != QUEUE_SUCCESS) { printf("FATAL ERROR: could not initialize semaphore waiting queue in SemInit!\n"); exitsim(); } sem->count = count; return SYNC_SUCCESS; }
int LockInit(Lock *l) { if (!l) return SYNC_FAIL; if (AQueueInit (&l->waiting) != QUEUE_SUCCESS) { printf("FATAL ERROR: could not initialize lock waiting queue in LockInit!\n"); exitsim(); } l->pid = -1; return SYNC_SUCCESS; }
//---------------------------------------------------------------------- // // ProcessModuleInit // // Initialize the process module. This involves initializing all // of the process control blocks to appropriate values (ie, free // and available). We also need to initialize all of the queues. // //---------------------------------------------------------------------- void ProcessModuleInit () { int i,j; dbprintf ('p', "Entering ProcessModuleInit\n"); AQueueInit (&freepcbs); AQueueInit (&runQueue); AQueueInit (&waitQueue); AQueueInit (&zombieQueue); // For each PCB slot in the global pcbs array: for (i = 0; i < PROCESS_MAX_PROCS; i++) { dbprintf ('p', "Initializing PCB %d @ 0x%x.\n", i, (int)&(pcbs[i])); // First, set the internal PCB link pointer to a newly allocated link if ((pcbs[i].l = AQueueAllocLink(&pcbs[i])) == NULL) { printf("FATAL ERROR: could not allocate link in ProcessModuleInit!\n"); exitsim(); } // Next, set the pcb to be available pcbs[i].flags = PROCESS_STATUS_FREE; //------------------------------------------------------- // STUDENT: Initialize the PCB's page table here. //------------------------------------------------------- //////////////////////////////////// // TODO3 CHANGE FOR 2 LEVEL DONE // JSM, Set all entries of every page table to 0 (Invalid) since no pages have been allocated for any process // and no level 2 page tables have been allocated for any processes for (j = 0; j < MEM_L1_PAGE_TABLE_SIZE; j++) { pcbs[i].pagetable[j] = 0x00000000; } /////////////////////////////////// // Finally, insert the link into the queue if (AQueueInsertFirst(&freepcbs, pcbs[i].l) != QUEUE_SUCCESS) { printf("FATAL ERROR: could not insert PCB link into queue in ProcessModuleInit!\n"); exitsim(); } } // There are no processes running at this point, so currentPCB=NULL currentPCB = NULL; dbprintf ('p', "Leaving ProcessModuleInit\n"); }
//------------------------------------------------------- // // mbox_t MboxCreate(); // // Allocate an available mailbox structure for use. // // Returns the mailbox handle on success // Returns MBOX_FAIL on error. // //------------------------------------------------------- mbox_t MboxCreate() { mbox_t mbox; uint32 intrval; //grabbing a mailbox should be an atomic operation intrval = DisableIntrs(); for(mbox=0; mbox<MBOX_NUM_MBOXES; mbox++){ if(mboxs[mbox].inuse == 0){ mboxs[mbox].inuse = 1; break; } } RestoreIntrs(intrval); if((mboxs[mbox].l = LockCreate()) == SYNC_FAIL){ printf("FATAL ERROR: Could not create lock for mbox\n"); exitsim(); } if((mboxs[mbox].s_msg_full = SemCreate(0)) == SYNC_FAIL) { printf("Bad sem create in mbox create\n "); exitsim(); } if((mboxs[mbox].s_msg_empty = SemCreate(MBOX_MAX_BUFFERS_PER_MBOX)) == SYNC_FAIL) { printf("Bad sem create in mbox create\n "); exitsim(); } if(mbox == MBOX_NUM_MBOXES) return MBOX_FAIL; if(AQueueInit(&mboxs[mbox].messages) != QUEUE_SUCCESS){ printf("FATAL ERROR: Could not initialize mbox messsage queue\n"); exitsim(); } if(AQueueInit(&mboxs[mbox].pids) != QUEUE_SUCCESS){ printf("FATAL ERROR: Could not initialize mbox pid queue\n"); exitsim(); } return mbox; }
int AQueueModuleInit() { int i; if (AQueueInit(&freeLinks) != QUEUE_SUCCESS) { printf("FATAL ERROR: could not initialize freeLinks queue in AQueueModuleInit!\n"); exitsim(); } dbprintf ('q', "Initializing %d links.\n", QUEUE_MAX_LINKS); for (i = 0; i < QUEUE_MAX_LINKS; i++) { // Initialize link structure linkpool[i].next = NULL; linkpool[i].prev = NULL; linkpool[i].object = NULL; // Add link to freeLinks queue if (AQueueInsertLast(&freeLinks, &(linkpool[i])) != QUEUE_SUCCESS) { printf("FATAL ERROR: could not insert link into freeLinks in AQueueModuleInit!\n"); // Adds structure to global queue of free links exitsim(); } } return QUEUE_SUCCESS; }