Exemple #1
0
void clockHandler(){
  int lastTime = USLOSS_Clock();
  ++timeTracker;
  if(USLOSS_Clock()-lastTime >= 5){
    // USLOSS_Console("Clock Fun at %d\n",USLOSS_Clock());
    // clock semaphore
    timeTracker = 0;
    // clock semaphore!!!!!!
    P1_V(&semTable[Clock_Sema]);
    dispatcher();
  }
  
}
Exemple #2
0
void block(int mboxID, int block, int size, char message[], int *realSize) {
    // get the correct process from the process table
    process *proc = &processTable[getpid() % 50];

    // add info to that process
    proc->pid = getpid();
    proc->blockStatus = block;
    proc->mboxID = mboxID;
    proc->timeAdded = USLOSS_Clock();
    proc->size = size;

    // if a zero slot message box wants to send a message
    if (message != NULL && block == SENDBLOCK) {
        memcpy(proc->message, message, size);
    }

    // actually block
    blockMe(block);

    if (message != NULL && block == RECEIVEBLOCK) {
        memcpy(message, processTable[getpid() % 50].message, size);
        *realSize = processTable[getpid() % 50].size;
    }

    if (block == RECEIVEBLOCK) {
        // empty out this slot in the processTable
        processTable[getpid() % 50].pid = -1;
        processTable[getpid() % 50].blockStatus = -1;
        processTable[getpid() % 50].message[0] = '\0';
        processTable[getpid() % 50].size = -1;
        processTable[getpid() % 50].mboxID = -1;
        processTable[getpid() % 50].timeAdded = -1;
    }
}
Exemple #3
0
int P2_Startup(void *arg){
  
  USLOSS_Console(" \n---------Starting Test 11 ----------\n");
  int status;
  int r;
  int temp;
  int i=0;
  
  while(i<10){
    //USLOSS_Console("before: %d\t", temp=USLOSS_Clock());
	++i;
    temp=USLOSS_Clock();
    r=P1_WaitDevice(USLOSS_CLOCK_DEV, 0, &status);
    USLOSS_Console("wait period : %d\n", USLOSS_Clock()-temp);
  }
 
  USLOSS_Console(" ---------Ending Test 11 ----------\n");
  return 0;
}
Exemple #4
0
/* ------------------------------------------------------------------------
   Name - dispatcher
   Purpose - runs the highest priority runnable process
   Parameters - none
   Returns - nothing
   Side Effects - runs a process
   ----------------------------------------------------------------------- */
void dispatcher()
{
  Check_Your_Privilege();

  int_disable();
  // USLOSS_Console("dispatcher Called, ready List: ");
  // printList(&readyHead);
  /*Adjust Runttime for current process*/
  int timeRun;
  if (procTable[currPid].lastStartedTime==FIRST_RUN) {
    timeRun=0;
  }else{
      timeRun=USLOSS_Clock()-procTable[currPid].lastStartedTime;
  }
  procTable[currPid].cpuTime+=timeRun;
  /*
   * Run the highest priority runnable process. There is guaranteed to be one
   * because the sentinel is always runnable.
   */
  // USLOSS_Console("In dispatcher PID -- before:  %d\n", currPid);
  int oldpid = currPid;
  PCB* readyListPos=&readyHead;

// USLOSS_Console("dispatcher Check point 1\n");
  readyListPos->nextPCB->state=0;//set state to running
  /*Set Proc state to ready unless it has quit or been killed*/
  if(procTable[oldpid].state==0){
    procTable[oldpid].state=1;
  }
  // USLOSS_Console("dispatcher Check point 2\n");
  currPid = readyListPos->nextPCB->PID;
  procTable[currPid].lastStartedTime=USLOSS_Clock();
  // USLOSS_Console("dispatcher Check point 3\n");
  /*Adds currpid to end of its priority section in the Rdy list*/
  removeFromList(currPid);
  addToReadyList(currPid);
  /*Reenable Interrupts*/
  int_enable();
  // USLOSS_Console("In dispatcher switching to PID:  %d\n", currPid); 
  USLOSS_ContextSwitch(&(procTable[oldpid]).context, &(readyListPos->nextPCB->context));

}
void wakeUpProcesses(void){
	P1_P(clockListSemaphore);
        int i;
       	long now = USLOSS_Clock();
        for(i = 0; i < P1_MAXPROC;i++){
        	if(clockList[i].inUse == 1){
                	if((now - clockList[i].startTime) >= clockList[i].waitingTime){
                       		P1_V(clockList[i].sem);
                                clockList[i].inUse = 0;
                        }
                }
        }
        P1_V(clockListSemaphore);
}
int P2_Sleep(int seconds){
	if(permissionCheck() || seconds < 0){
		return -1;
	}
	P1_Semaphore s = P1_SemCreate(0);
	P1_P(clockListSemaphore);
	int i;
	for(i = 0; i < P1_MAXPROC;i++){
		if(clockList[i].inUse == 0){
			clockList[i].inUse = 1;
			clockList[i].sem = s;
			clockList[i].startTime = USLOSS_Clock();
			clockList[i].waitingTime = seconds * US_IN_S;
			break;
		}
	}
	P1_V(clockListSemaphore);
	P1_P(s);
	P1_SemFree(s);
	return 0;
}
void sysHandler(int type,void *arg) {
	if(arg == NULL){
		USLOSS_Console("USLOSS_Sysargs is NULL!\n");
		return;
	}
	USLOSS_Sysargs *sysArgs = (USLOSS_Sysargs *) arg;
	int retVal = 0;
	int retVal2 = 0;
	int sectSize = 0;
	int sectors = 0;
	int tracks = 0;
	interruptsOn();
	switch (sysArgs->number) {
	case SYS_TERMREAD:
		retVal = P2_TermRead((int)sysArgs->arg3, (int)sysArgs->arg2, sysArgs->arg1);
                if(retVal >= 0){
                        sysArgs->arg4 = (void *)0;
                        sysArgs->arg2 = (void *)retVal;
                }else{
                        sysArgs->arg4 = (void *)-1;
                }
                break;
	case SYS_TERMWRITE:
		retVal = P2_TermWrite((int)sysArgs->arg3,(int)sysArgs->arg2,(char *)sysArgs->arg1);
                if(retVal >= 0){
                        sysArgs->arg4 = (void *)0;
                        sysArgs->arg2 = (void *)retVal;
                }else{
                        sysArgs->arg4 = (void *)-1;
                }
		break;
	case SYS_SPAWN: //Part 1
		retVal = P2_Spawn(sysArgs->arg5, sysArgs->arg1, sysArgs->arg2,
				(int) sysArgs->arg3, (int) sysArgs->arg4);
		if (retVal == -3 || retVal == -2) {
			sysArgs->arg4 = (void *) -1;
			sysArgs->arg1 = (void *) -1;
		} else {
			sysArgs->arg1 = (void *) retVal;
			sysArgs->arg4 = (void *) 0;
		}
		break;
	case SYS_WAIT: //Part 1
		retVal = P2_Wait(&retVal2);
		if(retVal == -1){
			sysArgs->arg1 = (void *)-1;
			sysArgs->arg4 = (void *)-1;
			sysArgs->arg2 = (void *)-1;
		}else{
			sysArgs->arg1 = (void *)retVal;
			sysArgs->arg2 = (void *)retVal2;
			sysArgs->arg4 = (void *)0;
		}
		break;
	case SYS_TERMINATE: //Part 1
		P2_Terminate((int)sysArgs->arg1);
		break;
	case SYS_SLEEP: // Part 1
		retVal = P2_Sleep((int)sysArgs->arg1);
		sysArgs->arg4 = (void *) retVal;
		break;
	case SYS_DISKREAD:
		retVal = P2_DiskRead((int)sysArgs->arg5,(int)sysArgs->arg3,(int) sysArgs->arg4,(int)sysArgs->arg2,(void *)sysArgs->arg1);
                if (retVal == -1) {
                        sysArgs->arg1 = (void *)retVal;
                        sysArgs->arg4 = (void *)-1;
                }
                else if (retVal == 0) {
                        sysArgs->arg1 = (void *)0;
                        sysArgs->arg4 = (void *)0;
                }else {
                        sysArgs->arg1 = (void *)retVal; //output is the disk's status register
                        sysArgs->arg4 = (void *)0;
                }
		break;
	case SYS_DISKWRITE:
		retVal = P2_DiskWrite((int)sysArgs->arg5,(int)sysArgs->arg3,(int) sysArgs->arg4,(int)sysArgs->arg2,(void *)sysArgs->arg1);
		if (retVal == -1) {
			sysArgs->arg1 = (void *)retVal;
			sysArgs->arg4 = (void *)-1;
		}
		else if (retVal == 0) {
			sysArgs->arg1 = (void *)0;
			sysArgs->arg4 = (void *)0;
		}else {
			sysArgs->arg1 = (void *)retVal; //output is the disk's status register
			sysArgs->arg4 = (void *)0;
		}
		break;
	case SYS_DISKSIZE:
		retVal = P2_DiskSize((int)sysArgs->arg1,&sectSize,&sectors,&tracks);
		if (retVal == -1) {
			sysArgs->arg4 = (void *)-1;
		}else {
			sysArgs->arg4 = (void *)0;
			sysArgs->arg1 = (void *)sectSize;
			sysArgs->arg2 = (void *)sectors;
			sysArgs->arg3 = (void *)tracks;
		}
		break;
	case SYS_GETTIMEOFDAY: //Part 1
		sysArgs->arg1 = (void *)USLOSS_Clock();
		break;
	case SYS_CPUTIME: //Part 1
		sysArgs->arg1 = (void *)P1_ReadTime();
		break;
	case SYS_DUMPPROCESSES: //Part 1
		P1_DumpProcesses();
		break;
	case SYS_GETPID: //Part 1
		sysArgs->arg1 = (void *) P1_GetPID();
		break;
	case SYS_SEMCREATE: //Part 1
		retVal = (int)sysArgs->arg1;
		if(retVal < 0){
			sysArgs->arg4 = (void *)-1;
		}else{
			sysArgs->arg4 = (void *)0;
			sysArgs->arg1 = (void *) semAdd(P1_SemCreate(retVal));
		}
		break;
	case SYS_SEMP: // Part 1
		if(validSem((int)sysArgs->arg1) == 0){
			sysArgs->arg4 = (void *) -1;
			return;
		}
		retVal = P1_P(userSemList[(int)sysArgs->arg1].sem);
		if(retVal < 0){
			sysArgs->arg4 = (void *) -1;
		}
		else{
			sysArgs->arg4 = (void *) 0;
		}
		break;
	case SYS_SEMV: // Part 1
                if(validSem((int)sysArgs->arg1) == 0){
                        sysArgs->arg4 = (void *) -1;
                        return;
                }
		retVal = P1_V(userSemList[(int)sysArgs->arg1].sem);
		if(retVal < 0){
			sysArgs->arg4 = (void *) -1;
		}
		else{
			sysArgs->arg4 = (void *) 0;
		}
		break;
	case SYS_SEMFREE: // Part 1
                if(validSem((int)sysArgs->arg1) == 0){
                        sysArgs->arg4 = (void *) -1;
                        return;
                }
		
		retVal = semDelete((int)sysArgs->arg1);
		if(retVal < 0){
			sysArgs->arg4 = (void *) -1;
		}
		else{
			sysArgs->arg4 = (void *) 0;
		}
		break;
	case SYS_MBOXCREATE: //Part 1
		retVal = P2_MboxCreate((int)sysArgs->arg1,(int)sysArgs->arg2);
		if(retVal == -2){
			sysArgs->arg1 = (void *) -1;
			sysArgs->arg4 = (void *) -1;
		}else{
			sysArgs->arg1 = (void *) retVal;
			sysArgs->arg4 = (void *) 0;
		}
		break;
	case SYS_MBOXRELEASE: // Part 1
		retVal = P2_MboxRelease((int)sysArgs->arg1);
		if(retVal < 0){
			sysArgs->arg4 = (void *) -1;
		}else{
			sysArgs->arg4 = (void *) 0;
		}
		break;
	case SYS_MBOXSEND: // Part 1
		retVal = P2_MboxSend((int)sysArgs->arg1,sysArgs->arg2,(int *)&sysArgs->arg3);
		if(retVal < 0){
			sysArgs->arg4 = (void *) -1;
		}else{
			sysArgs->arg4 = (void *) 0;
		}
		break;
	case SYS_MBOXRECEIVE: // Part 1
                retVal = P2_MboxReceive((int)sysArgs->arg1,sysArgs->arg2,(int *)&sysArgs->arg3);
                if(retVal < 0){
                        sysArgs->arg4 = (void *) -1;
                }else{
                        sysArgs->arg4 = (void *) 0;
			sysArgs->arg2 = (void *) retVal;
                }
		break;
	case SYS_MBOXCONDSEND: // Part 1
                retVal = P2_MboxCondSend((int)sysArgs->arg1,sysArgs->arg2,(int *)&sysArgs->arg3);
                if(retVal == -1){
                        sysArgs->arg4 = (void *) -1;
                }else if(retVal == -2){
			sysArgs->arg4 = (void *) 1;
		}
		else{
                        sysArgs->arg4 = (void *) 0;
                }
		break;
	case SYS_MBOXCONDRECEIVE: // Part 1
                retVal = P2_MboxCondReceive((int)sysArgs->arg1,sysArgs->arg2,(int *)&sysArgs->arg3);
                if(retVal == -1){
                        sysArgs->arg4 = (void *) -1;
                }else if(retVal == -2){
                        sysArgs->arg4 = (void *) 1;
                }
                else{
                        sysArgs->arg4 = (void *) 0;
			sysArgs->arg2 = (void *) retVal;
                }
		break;
	default:
		P1_Quit(1);
	}
}
Exemple #8
0
int P1_ReadTime(void){
    return USLOSS_Clock()-procTable[currPid].lastStartedTime+procTable[currPid].cpuTime;
}