///////////////////////////////////////////////////// //-------------------------------- //methods child's class //////////////////////////////////////////////////// //print all list starting from the last element bool derlist::revprint( ) const { list* pel = getlast( ); if( ( pel == NULL ) && ( getlast( ) == NULL ) ) return 0; cout<<"\nEntered long numbers: "; while ( pel != NULL) { cout<<pel->num<<", "; pel = pel->prev; } }
int Xinu_Default_Scheduling() { register struct pentry *optr; /* pointer to old process entry */ register struct pentry *nptr; /* pointer to new process entry */ /* no switch needed if current process priority higher than next*/ if (((optr = &proctab[currpid])->pstate == PRCURR) && (lastkey(rdytail) < optr->pprio)) { return (OK); } /* force context switch */ if (optr->pstate == PRCURR) { optr->pstate = PRREADY; insert(currpid, rdyhead, optr->pprio); } /* remove highest priority process at end of ready list */ nptr = &proctab[(currpid = getlast(rdytail))]; nptr->pstate = PRCURR; /* mark it currently running */ #ifdef RTCLOCK preempt = QUANTUM; /* reset preemption counter */ #endif ctxsw((int) &optr->pesp, (int) optr->pirmask, (int) &nptr->pesp, (int) nptr->pirmask); /* The OLD process returns here when resumed. */ return OK; }
/*------------------------------------------------------------------------ * resched -- reschedule processor to highest priority ready process * * Notes: Upon entry, currpid gives current process id. * Proctab[currpid].pstate gives correct NEXT state for * current process if other than PRCURR. *------------------------------------------------------------------------ */ int resched() { register struct pentry *optr; /* pointer to old process entry */ register struct pentry *nptr; /* pointer to new process entry */ register int pflag; char *oldStackPtr = NULL; proctab[currpid].time = proctab[currpid].time + tod - proctab[currpid].oldtime; optr = &proctab[currpid]; pflag = sys_pcxget(); if ( optr->pstate == PRCURR ) { /* no switch needed if current prio. higher than next */ /* or if rescheduling is disabled ( pflag == 0 ) */ if ( pflag == 0 || lastkey(rdytail) < optr->pprio ) return; /* force context switch */ optr->pstate = PRREADY; insert(currpid,rdyhead,optr->pprio); } else if ( pflag == 0 ) { kprintf("pid=%d state=%d name=%s", currpid,optr->pstate,optr->pname); panic("Reschedule impossible in this state"); } /* remove highest priority process at end of ready list */ nptr = &proctab[(currpid=getlast(rdytail))]; nptr->pstate = PRCURR; /* mark it currently running */ preempt = QUANTUM; /* reset preemption counter */ _pglob = nptr->pglob; /* retrieve global environment */ proctab[currpid].oldtime=tod; if (!optr->pregs || !nptr->pregs) { kprintf("Rescheduling failed: pregs = 0\n"); return; } /* if (!debug_log) debug_log = xinu_open("resched.log", O_RDWR|O_CREAT|O_TRUNC); dprintf(1, "Old: %s, New: %s\n", optr->pname, nptr->pname);*/ oldStackPtr = optr->pregs; ctxsw(&optr->pregs,&nptr->pregs); if (currpid != 0 && optr->pregs > optr->pbase + optr->plen) panic("stack overflow"); if (optr->phastrap) { optr->phastrap = FALSE; /* mark trap as serviced */ if (optr->ptfn != NULL) (*optr->ptfn)(optr->ptarg); } /* The OLD process returns here when resumed. */ return; }
dirEntry *getlast( dirEntry *filelist ) { systemlog( 4, "enterd getlast.\n" ); if( filelist->next != NULL ) { filelist = getlast( filelist->next ); } return filelist; }
/* Aging Scheduler */ int aging_resched() { register struct qent *oqptr; /* pointer to the currently running process in queue entry table */ register struct pentry *opptr; /* pointer to the currently running process in process entry table */ register struct qent *nqptr; /* pointer to the newly selected process in queue entry table */ register struct pentry *npptr; /* pointer to the newly selected process in process entry table */ int opid = currpid; /* no switch needed if current process priority higher than next*/ oqptr = &q[opid]; opptr = &proctab[opid]; /* set the priority of current process to base priority */ q[currpid].qkey = proctab[currpid].pprio; if ( ( opptr->pstate == PRCURR) && (lastkey(rdytail) < oqptr->qkey) ) { updprocprio(); /* update priorities of all the processes in ready queue */ #ifdef RTCLOCK preempt = QUANTUM; /* reset preemption counter */ #endif return(OK); } /* remove highest priority process at end of ready list */ npptr = &proctab[ (currpid = getlast(rdytail)) ]; npptr->pstate = PRCURR; /* mark it currently running */ updprocprio(); /* update priorities of all the processes in ready queue */ /* put old process in ready queue if it is the current executing process */ if (opptr->pstate == PRCURR) { opptr->pstate = PRREADY; insert(opid,rdyhead,opptr->pprio); } #ifdef RTCLOCK preempt = QUANTUM; /* reset preemption counter */ #endif ctxsw((int)&opptr->pesp, (int)opptr->pirmask, (int)&npptr->pesp, (int)npptr->pirmask); /* The OLD process returns here when resumed. */ return OK; }
/* Name: l_get_next_writer * * Desc: Gets the next writer to whom the write lock should be awarded. It goes * through the list and makes the proc ready, if any and returns TRUE. If * eligible writers are not available, it just returns FALSE. * * Params: * lid - read lock ID * * Returns: int * TRUE - if eligible writer is available * FLASE - otherwise */ int l_get_next_writer(int lid) { int next_pid = EMPTY;; int nextrw = L_GET_NEXTRW(lid); DTRACE_START; /* Set the lstate based on the next available waiter. */ if (LT_WRITE == nextrw) { DTRACE("DBG$ %d %s> lid %d state changed from %s to Write\n", \ currpid, __func__, currpid, L_GET_LSTATESTR(lid)); L_SET_LSTATE(lid, LS_WRITE); } else if (EMPTY == nextrw) { DTRACE("DBG$ %d %s> lid %d state changed from %s to Free\n", \ currpid, __func__, currpid, L_GET_LSTATESTR(lid)); L_SET_LSTATE(lid, LS_FREE); } else { DTRACE("DBG$ %d %s> bad nextrw %d for lid %d - should have been " \ " WRITE or EMPTY\n", \ currpid, __func__, currpid, lid, next_pid); ASSERT(0); goto RETURN_FALSE; } if (EMPTY == (next_pid = getlast(L_GET_LWTAIL(lid)))) { #ifdef DBG_ON l_print_lock_details(lid); #endif /* DBG_ON */ DTRACE("DBG$ %d %s> bad pid %d in nextq tail %d for lid %d\n", \ currpid, __func__, next_pid, L_GET_NEXTQ(lid), lid); ASSERT(0); goto RETURN_FALSE; } L_DEC_NWWRITE(lid); L_INC_NAWRITE(lid); /* Enqueue the waiting writer in ready queue. */ ready(next_pid, RESCHNO); l_pidmap_oper(lid, next_pid, L_MAP_RELEASE, L_MAP_SET); DTRACE("DBG$ %d %s> lid %d next writer lock for pid %d\n", \ currpid, __func__, lid, next_pid); l_recal_next(lid); DTRACE_END; return TRUE; RETURN_FALSE: DTRACE_END; return FALSE; }
/* Name: l_get_next_readers * * Desc: Gets the next set of reader(s) to whom the read lock should be awarded. * It goes through the list and makes the procs ready, if any and returns * TRUE. If eligible readers are not available, it just returns FALSE. * * Params: * lid - read lock ID * * Returns: int * TRUE - if eligible readers are available * FLASE - otherwise */ int l_get_next_readers(int lid) { int next_pid = EMPTY; int nextrw = L_GET_NEXTRW(lid); /* Set the lstate based on the next available waiter. */ if (LT_READ == nextrw) { DTRACE("DBG$ %d %s> lid %d state changed from %s to Read\n", \ currpid, __func__, currpid, L_GET_LSTATESTR(lid)); L_SET_LSTATE(lid, LS_READ); } else if (EMPTY == nextrw) { DTRACE("DBG$ %d %s> lid %d state changed from %s to Free\n", \ currpid, __func__, currpid, L_GET_LSTATESTR(lid)); L_SET_LSTATE(lid, LS_FREE); } else { DTRACE("DBG$ %d %s> bad nextrw %d for lid %d - should have been " \ " READ or EMPTY\n", \ currpid, __func__, currpid, lid, next_pid); ASSERT(0); goto RETURN_FALSE; } /* Fetch all possible readers. */ while (LT_READ == L_GET_NEXTRW(lid)) { if (EMPTY == (next_pid = getlast(L_GET_LRTAIL(lid)))) { DTRACE("DBG$ %d %s> bad nextq %d for lid %d\n", \ currpid, __func__, currpid, lid, next_pid); break; } DTRACE("DBG$ %d %s> next reader pid %d for lid %d\n", \ currpid, __func__, currpid, next_pid, lid); L_DEC_NWREAD(lid); L_INC_NAREAD(lid); ready(next_pid, RESCHNO); l_pidmap_oper(lid, next_pid, L_MAP_RELEASE, L_MAP_SET); DTRACE("DBG$ %d %s> lid %d next reader lock for pid %d\n", \ currpid, __func__, lid, next_pid); l_recal_next(lid); } DTRACE_END; return TRUE; RETURN_FALSE: DTRACE_END; return FALSE; }
//------------------------------------------- //delete all numbers greater than minimal * 3 void derlist::delmin( ) { list *min = getlast( ), *pel, *temp; pel = getfirst( ); while ( pel != NULL ) { if( pel->num > 3*(min->num) ){ temp = pel; pel = del( temp ); cout<<"Removing..."; } else pel = pel->next; } }
/* * This function makes the current process block state. */ int dskresched() { register struct pentry *optr; register struct pentry *nptr; STATWORD ps; disable(ps); optr = &proctab[currpid]; optr -> pstate = PRSUSP; nptr = &proctab[(currpid = getlast(rdytail))]; nptr -> pstate = PRCURR; #ifdef RTCLOCK preempt = QUANTUM; #endif restore(ps); ctxsw((int)&optr -> pesp, (int)optr -> pirmask, (int)&nptr -> pesp, (int)nptr -> pirmask); return OK; }
void _resched_aging(struct pentry *optr, struct pentry *nptr) { int oldpreempt = preempt; preempt = MAXINT; /* Loop through everything in the queue and increase each entries priority by a factor of 1.2 */ int next = q[rdyhead].qnext; while (q[next].qnext != EMPTY) { q[next].qkey = min(MAX_PRIORITY, ceil(q[next].qkey * 1.2)); next = q[next].qnext; } /* Put the current process back into the queue with its base priority */ if (optr->pstate == PRCURR) { optr->pstate = PRREADY; insert(currpid, rdyhead, optr->pprio); } /* Now get the item with the highest priority */ int oldcurrpid = currpid; currpid = getlast(rdytail); /* If the process from the end of the list is the current process, no context switch is necessary */ if (currpid == oldcurrpid) { optr->pstate = PRCURR; preempt = oldpreempt; return; } /* Otherwise, switch contexts to the new current process */ nptr = &proctab[currpid]; nptr->pstate = PRCURR; #ifdef RTCLOCK preempt = QUANTUM; /* Reset preempt counter */ #endif ctxsw((int)&optr->pesp, (int)optr->pirmask, (int)&nptr->pesp, (int)nptr->pirmask); return; }
int ldelete(int ldes) { STATWORD ps; struct lentry *lptr; int pid, lockID; disable(ps); lockID = getLockID(ldes); if( isbadlock(lockID) || (lptr=&locktab[lockID])->lstate == LOCKFREE ) { restore(ps); return(SYSERR); } //lptr = &locktab[ldes]; lptr->lstate = LOCKFREE; lptr->locked = 0; lptr->acquiredby[currpid]=-1; lptr->lockType = DELETED; lptr->lprio=-1; if( nonempty(lptr->lhead) ) { while( (pid=getlast(lptr->lhead)) != EMPTY ) { proctab[pid].waitPriority = -1; proctab[pid].lockID = -1; proctab[pid].procLockType[lockID] = DELETED; proctab[pid].pwaitret = DELETED; ready(pid, RESCHNO); } resched(); } restore(ps); return(OK); }
void clearEntrys( dirEntry *filelist ) { dirEntry *prev; systemlog( 4, "clearEntrys.\n" ); if( filelist != NULL ) { filelist = getlast( filelist ); while( filelist->prev != NULL ) { prev = filelist; filelist = filelist->prev; filelist->next = NULL; free( prev ); prev = NULL; } free( filelist ); filelist = NULL; } }
int bsolve( int m, double *sy, int *iy, int *pny ) { int i, j, jr, ny=*pny; int k, row, row2, consistent=TRUE; double beta; double eps; static double *y=NULL, *yy=NULL; static int *tag=NULL; static int currtag=1; double starttime, endtime; starttime = (double) clock(); if ( y == NULL) CALLOC( y, m, double); if ( yy == NULL) CALLOC( yy, m, double); if ( tag == NULL) CALLOC( tag, m, int); if ( newcol == NULL) MALLOC( newcol, m, double ); if (inewcol == NULL) MALLOC( inewcol, m, int ); for (k=0; k<ny; k++) { i = irowperm[iy[k]]; y[i] = sy[k]; tag[i] = currtag; addtree(i); } if (rank < m) eps = EPSSOL * maxv(sy,ny); /*------------------------------------------------------+ | -1 | | y <- L y */ for (i=getfirst(); i < rank && i != -1; i=getnext()) { beta = y[i]; for (k=0; k<degL[i]; k++) { row = L[i][k].i; if (tag[row] != currtag) { y[row] = 0.0; tag[row] = currtag; addtree(row); } y[row] -= L[i][k].d * beta; } } /*------------------------------------------------------+ | Apply refactorization row operations. */ for (jr=0; jr<nr; jr++) { /*--------------------------------------------------+ | Gather sparse vector. */ k=0; for (j=col_outs[jr]; j<=imaxs[jr]; j++) { if (tag[j] == currtag) { sy[k] = y[j]; iy[k] = j; k++; tag[j]--; deltree(j); } } ny = k; /*--------------------------------------------------+ | Scatter and permute. */ for (k=0; k<ny; k++) { i = iperm[jr][iy[k]]; y[i] = sy[k]; tag[i] = currtag; addtree(i); } /*--------------------------------------------------+ | Apply row operations. */ row = rows[jr]; for (k=0; k<ngauss[jr]; k++) { row2 = row_list[jr][k]; if (tag[row] != currtag) { y[row] = 0.0; tag[row] = currtag; addtree(row); } if (tag[row2] == currtag) { y[row] -= gauss[jr][k] * y[row2]; } } } /*------------------------------------------------------+ | -1 | | Set aside sparse intermediate vector L P a for | | j | | refactorization routine. */ nnewcol = 0; for (i=getfirst(); i != -1; i=getnext()) { if ( ABS(y[i]) > EPS ) { newcol [nnewcol] = y[i]; inewcol[nnewcol] = i; nnewcol++; } } /*------------------------------------------------------+ | -1 | | y <- U y */ for (i=getlast(); i >= rank && i != -1; i=getprev()) { if ( ABS( y[i] ) > eps ) consistent = FALSE; y[i] = 0.0; } for ( ; i>=0; i=getprev()) { beta = y[i]/diag[i]; for (k=0; k<degU[i]; k++) { row = U[i][k].i; if (tag[row] != currtag) { y[row] = 0.0; tag[row] = currtag; addtree(row); } y[row] -= U[i][k].d * beta; } y[i] = beta; } ny = 0; for (i=getfirst(); i != -1; i=getnext()) { if ( ABS(y[i]) > EPS ) { sy[ny] = y[i]; iy[ny] = colperm[i]; ny++; } } *pny = ny; currtag++; killtree(); endtime = (double) clock(); cumtime += endtime - starttime; return consistent; }
/*------------------------------------------------------------------------ * resched -- reschedule processor to highest priority ready process * * Notes: Upon entry, currpid gives current process id. * Proctab[currpid].pstate gives correct NEXT state for * current process if other than PRREADY. *------------------------------------------------------------------------ */ int resched() { STATWORD PS; register struct pentry *optr; /* pointer to old process entry */ register struct pentry *nptr; /* pointer to new process entry */ disable(PS); /* no switch needed if current process priority higher than next*/ if ( ( (optr= &proctab[currpid])->pstate == PRCURR) && (lastkey(rdytail)<optr->pprio)) { restore(PS); return(OK); } #ifdef STKCHK /* make sure current stack has room for ctsw */ asm("movl %esp, currSP"); if (currSP - optr->plimit < 48) { kprintf("Bad SP current process, pid=%d (%s), lim=0x%lx, currently 0x%lx\n", currpid, optr->pname, (unsigned long) optr->plimit, (unsigned long) currSP); panic("current process stack overflow"); } #endif /* force context switch */ if (optr->pstate == PRCURR) { optr->pstate = PRREADY; insert(currpid,rdyhead,optr->pprio); } /* remove highest priority process at end of ready list */ nptr = &proctab[ (currpid = getlast(rdytail)) ]; nptr->pstate = PRCURR; /* mark it currently running */ #ifdef notdef #ifdef STKCHK if ( *( (int *)nptr->pbase ) != MAGIC ) { kprintf("Bad magic pid=%d value=0x%lx, at 0x%lx\n", currpid, (unsigned long) *( (int *)nptr->pbase ), (unsigned long) nptr->pbase); panic("stack corrupted"); } /* * need ~16 longs of stack space below, so include that in check * below. */ if (nptr->pesp - nptr->plimit < 48) { kprintf("Bad SP pid=%d (%s), lim=0x%lx will be 0x%lx\n", currpid, nptr->pname, (unsigned long) nptr->plimit, (unsigned long) nptr->pesp); panic("stack overflow"); } #endif /* STKCHK */ #endif /* notdef */ #ifdef RTCLOCK preempt = QUANTUM; /* reset preemption counter */ #endif #ifdef DEBUG PrintSaved(nptr); #endif // When performing a context switch between processes we must also // switch between memory spaces. This is accomplished by adjusting // the PDBR register with every context switch. // // 5 - Context switch // - every process has separate page directory // - before ctxsw() load CR3 with the process' PDBR set_PDBR(VA2VPNO(nptr->pd)); #if DUSTYDEBUG kprintf("switching to process %d\n", (nptr - proctab)); #endif ctxsw(&optr->pesp, optr->pirmask, &nptr->pesp, nptr->pirmask); #ifdef DEBUG PrintSaved(nptr); #endif /* The OLD process returns here when resumed. */ restore(PS); return OK; }
/*------------------------------------------------------------------------ * resched -- reschedule processor to highest priority ready process * * Notes: Upon entry, currpid gives current process id. * Proctab[currpid].pstate gives correct NEXT state for * current process if other than PRREADY. *------------------------------------------------------------------------ */ int resched() { STATWORD PS; register struct pentry *optr; /* pointer to old process entry */ register struct pentry *nptr; /* pointer to new process entry */ register int i; disable(PS); /* no switch needed if current process priority higher than next*/ if ( ( (optr= &proctab[currpid])->pstate == PRCURR) && (lastkey(rdytail)<optr->pprio)) { restore(PS); return(OK); } #ifdef STKCHK /* make sure current stack has room for ctsw */ asm("movl %esp, currSP"); if (currSP - optr->plimit < 48) { kprintf("Bad SP current process, pid=%d (%s), lim=0x%lx, currently 0x%lx\n", currpid, optr->pname, (unsigned long) optr->plimit, (unsigned long) currSP); panic("current process stack overflow"); } #endif /* force context switch */ if (optr->pstate == PRCURR) { optr->pstate = PRREADY; insert(currpid,rdyhead,optr->pprio); } /* remove highest priority process at end of ready list */ nptr = &proctab[ (currpid = getlast(rdytail)) ]; nptr->pstate = PRCURR; /* mark it currently running */ #ifdef notdef #ifdef STKCHK if ( *( (int *)nptr->pbase ) != MAGIC ) { kprintf("Bad magic pid=%d value=0x%lx, at 0x%lx\n", currpid, (unsigned long) *( (int *)nptr->pbase ), (unsigned long) nptr->pbase); panic("stack corrupted"); } /* * need ~16 longs of stack space below, so include that in check * below. */ if (nptr->pesp - nptr->plimit < 48) { kprintf("Bad SP pid=%d (%s), lim=0x%lx will be 0x%lx\n", currpid, nptr->pname, (unsigned long) nptr->plimit, (unsigned long) nptr->pesp); panic("stack overflow"); } #endif /* STKCHK */ #endif /* notdef */ #ifdef RTCLOCK preempt = QUANTUM; /* reset preemption counter */ #endif #ifdef DEBUG PrintSaved(nptr); #endif //OS proj 3 modify write_cr3(nptr->pdbr); ctxsw(&optr->pesp, optr->pirmask, &nptr->pesp, nptr->pirmask); #ifdef DEBUG PrintSaved(nptr); #endif /* The OLD process returns here when resumed. */ restore(PS); return OK; }
int printtotalnr( dirEntry *filelist ) { systemlog( 4, "printtotalnr\n" ); filelist = getlast( filelist ); return filelist->number; }
main(){ nd *head=NULL, *temp; char input[256], inputcopy[256], word[20], result[20];; int jump, cnt, numofDigits; char delim, *a, * token; char wordcopy[20], tempword[20], dashed[20], prev[20]; int numdash; // ------ int i, cnter, num, j, dash, hundflag=0, thouflag=0, hundthouone=0; printf("enter words : "); fgets(input, sizeof(input), stdin); input[ strlen(input) - 1 ] = '\0'; strncpy ( inputcopy, input, 256); a = strtok (input," -"); strncpy( word, a, 20); while(a!=NULL ){ //loops throught the words // strncpy( word, a, 20); if (strcmp (word, "thousand") == 0 ){ //ignores the word "thousand" but sets its flag; does not insert something in the list thouflag =1; } else if( strcmp (word, "hundred") == 0){ hundflag =1; //ignores the word "hundred" but sets its flag; does not insert something in the list }else if( strcmp (word, "million") == 0){ //ignores the word "million" but sets its flag; does not insert something in the list }else{ //if word is not thousand, hundred or million, it finds it in the fucntion // zero to nine and returns its equivalent digit if (numofDigits>1) hundthouone =1; num = zerotonine(word); if ( num == 66){ printf("Wrong input. enter again.\n"); exit(0); }else if (num == 69) exit(0); insert(&head, temp, num); numofDigits++; } a = strtok (NULL , " -"); if (a!= NULL ) { //takes note of the prev and current word strncpy ( prev, word, 20); strncpy( word, a, 20); } num =0; } //checks of the last word is thousand, hundred or million to add the zeroes if (( strcmp (word, "thousand") == 0 || strcmp (word, "hundred") == 0 || strcmp (word, "million") == 0 ) && a==NULL ){ int a, times; if (strcmp (word, "thousand") == 0){ if (hundflag ==1 && thouflag ==1 ){//three hundred thousand //300000 times =5; }else { // 326 000 // three hundred twenty-six thousand //22 0000 //thirty thousand times = 3; } if ( checkty(prev) == 1){ times = times +1; } }else if (strcmp (word, "hundred") == 0){ // printf("hundred diri \n"); times= 2; }else if (strcmp (word, "million") == 0){ times = 6; } for (a = 0; a < times; a++){ insert(&head, temp, 0); numofDigits++; } } //checks of 1001 801 or the like, if the number has zeroes between it if ( strcmp (prev, "hundred") == 0 || strcmp (prev, "thousand") == 0 ){ int b, x, tempnum; tempnum = getlast(&head); deletenode(&head); if (strcmp (prev, "thousand") == 0 ) x=2; if (strcmp (prev, "hundred") == 0 ) x=1; for (b= 0;b < x; b++){ insert(&head, temp, 0); numofDigits++; } insert(&head, temp, tempnum); } //checks of last word ends in -ty if ( checkty(word) == 1 || (checkty(word) == 1 && strcmp (prev, "hundred") == 0)){ insert(&head, temp, 0); numofDigits++; } display(head); printf("\n\n"); free(head); }
void _resched_linux(struct pentry *optr, struct pentry *nptr) { int oldpreempt = preempt; preempt = MAXINT; /* Copy over however much counter the current process had left */ int counter_diff = proctab[currpid].pcounter - oldpreempt; proctab[currpid].pcounter -= counter_diff; /* Check to see if there are any processes on the ready queue which are able to run and have quantum left */ int done_with_epoch = 1; int item = q[rdytail].qprev; /* Loop over backwards, looking for can_run, pstate == PRREADY and counter > 0 */ while (q[item].qprev != EMPTY) { /* If we find an eligible process, flag the epoch as not done */ if (proctab[item].pcan_run && (proctab[item].pstate == PRREADY || proctab[item].pstate == PRCURR) && proctab[item].pcounter > 0) { done_with_epoch = 0; break; } /* Move on to the previous item */ item = q[item].qprev; } /* If the epoch is over or we have no proceses eligible to execute for this epoch */ if (done_with_epoch) { /* Make sure the null proc has a counter of 0 */ proctab[NULLPROC].pcounter = 0; /* Loop over each process */ int pid; for (pid=1; pid<NPROC; pid++) { if (proctab[pid].pstate == PRFREE) continue; /* Set this process as runnable, since we're starting a new epoch */ proctab[pid].pcan_run = 1; /* Copy the priority over to the field to use during epochs, since priority cannot change during runtime */ proctab[pid].pprio2 = proctab[pid].pprio; /* If this process has quantum left over the threshold, factor that in */ if (proctab[pid].pcounter >= rollover_threshold) { proctab[pid].pquantum = floor(proctab[pid].pcounter/2) + proctab[pid].pprio2; proctab[pid].pcounter = proctab[pid].pquantum; } /* Otherwise, set it's quantum counter to it's priority */ else { proctab[pid].pquantum = proctab[pid].pcounter = proctab[pid].pprio2; } /* If this process is on the ready queue, make sure it's in the right spot since it's key may have changed */ if (proctab[pid].pstate == PRREADY) { int new_goodness = proctab[pid].pcounter + proctab[pid].pprio2; /* If the new value is between the processes before and after this one, then it's safe to just change the value */ if (new_goodness >= q[q[pid].qprev].qkey && new_goodness <= q[q[pid].qnext].qkey) { q[pid].qkey = new_goodness; } /* If it's not though, we need to dequeue the process and insert it back in with the new goodness value */ else { dequeue(pid); insert(pid, rdyhead, new_goodness); } } } /* Then put the current process back into the ready queue, if it wants to */ if (optr->pstate == PRCURR) { optr->pstate = PRREADY; insert(currpid, rdyhead, proctab[currpid].pcounter + proctab[currpid].pprio2); } } /* Put the current process back into the ready queue */ else if (optr->pstate == PRCURR) { int new_goodness = optr->pcounter > 0 ? optr->pcounter + optr->pprio2 : 0; if (new_goodness > lastkey(rdytail)) { if (currpid != 0) preempt = optr->pcounter; else preempt = QUANTUM; return; } optr->pstate = PRREADY; insert(currpid, rdyhead, new_goodness); } /* Get the item with the highest 'goodness' */ currpid = getlast(rdytail); /* Otherwise, switch contexts to the new current process */ nptr = &proctab[currpid]; nptr->pstate = PRCURR; #ifdef RTCLOCK if (currpid != 0) preempt = nptr->pcounter; else preempt = QUANTUM; #endif ctxsw((int)&optr->pesp, (int)optr->pirmask, (int)&nptr->pesp, (int)nptr->pirmask); return; }
int release(int numlocks,int ldes1) { struct lentry *lptr; struct pentry *pptr; int i; pptr=&proctab[currpid]; if (pptr->deleted[ldes1]==1) //check if the locks has been deleted { pptr->deleted[ldes1]==0; pptr->pwaitret=OK; //continue; return (SYSERR); } //if bad lock if(isbadlock(ldes1) || (lptr=&lockarr[ldes1])->lstate==LFREE ||pptr->pwaitret==DELETED) { pptr->pwaitret=OK; return (SYSERR); } if (pptr->pinh != 0) { pptr->pinh=0; //chprio(currpid,pptr->prioh); } if (lptr->lstate==READ) { lptr->holder[currpid]=-1; pptr->lockdesc[ldes1]=-1; if (lptr->r_count>0) //if more readers present in the waiting queue { lptr->r_count--; if (lptr->r_count ==0 && !isEmpty(lptr->lhead)) { } else return OK; } } else //change the holder of the process and of the locks { lptr->holder[currpid]=-1; pptr->lockdesc[ldes1]=-1; } pptr->pinh=0; //check for the inverted priority for(i=0;i<50;i++) { if (pptr->lockdesc[i]==1) { changePriority(i,getlast((&lockarr[i])->ltail)); } } pop_3(lptr->lhead,lptr->ltail,ldes1); //continue; return OK; }
//function handeles the deperived case of writers and pops the process accordingly int pop_3(int head,int tail,int ldesc1) { struct lentry *tptr; struct pentry *pptr; tptr=&lockarr[ldesc1]; int max=head,pid,next; int count=1; int rd[50],i=1; next =q[head].qnext; int cc=1; if (isEmpty(head)) //if one in the ready queue change the { tptr->lstate=LUSED; return OK; //continue ; } while(next <tail ) //calculate the max writer { pptr = &proctab[next]; if (pptr->wait_type==WRITE) max=next; next=q[next].qnext; } if (max !=head) if ( (&proctab[max])->dep==3 ) //has been deprieved 3 times { pptr = &proctab[max]; pptr->dep=0; pptr->wait_lock=-1; pptr->wait_type=-1; ready(dequeue(max),RESCHNO); //ready(dequeue(max),RESCHYES); return OK; //continue; } if (q[max].qnext ==tail && max !=head) //writer is the last element { pid=getlast(tail); pptr = &proctab[pid]; pptr->dep=0; pptr->wait_lock=-1; pptr->wait_type=-1; ready(pid,RESCHNO); return OK; //continue; } // do this only if the last writer priority = priority of last reader next=q[tail].qprev; if (max ==head) { while (next !=head) { pptr= &proctab[next]; pptr->wait_lock=-1; pptr->wait_type=-1; ready(dequeue(next),RESCHNO); next=q[tail].qprev; } } else { while(next!=max) { pptr= &proctab[next]; pptr->wait_lock=-1; pptr->wait_type=-1; ready(dequeue(next),RESCHNO); /*dfsdfsfsdfsdfsdfSD*/ next=q[tail].qprev; } (&proctab[max])->dep++; } return OK; }
/*----------------------------------------------------------------------- * resched -- reschedule processor to highest priority ready process * * Notes: Upon entry, currpid gives current process id. * Proctab[currpid].pstate gives correct NEXT state for * current process if other than PRREADY. *------------------------------------------------------------------------ */ int resched() { register struct pentry *optr; /* pointer to old process entry */ register struct pentry *nptr; /* pointer to new process entry */ int itemA, itemB, itemC,proc; int num, i; //STATWORD ps; optr = &proctab[currpid]; if(sched_type==RANDOMSCHED) { /*if(count_random==0) { count_random++; enqueue(dequeue(0),queueC_tail); }*/ if((!nonempty(queueC_head)) && (!nonempty(queueC_head)) && (!nonempty(queueC_head))) { return OK; } proctab[currpid].quantumLeft = preempt; //quantum used in last execution if(switched_to_randomsched==1) { switched_to_randomsched=0; proctab[currpid].procCpuTicks += (proctab[currpid].assignedQuantum - preempt); } else { proctab[currpid].procCpuTicks += (QUANTUM - preempt); } //proctab[currpid].procCpuTicks += (QUANTUM - preempt); /*Make current process as ready*/ if (optr->pstate == PRCURR) { //kprintf("\nen inside "); optr->pstate = PRREADY; if(((optr->pprio)>=66) && ((optr->pprio)<=99)) { //kprintf("\nReady Queue A, %s", optr->pname); enqueue(currpid, queueA_tail); } else if(optr->pprio>=33 && optr->pprio<=65) { //kprintf("\nReady Queue b, %s", optr->pname); enqueue(currpid, queueB_tail); } else if(optr->pprio>=0 && optr->pprio<=32) { //kprintf("\nReady Queue c, %s", optr->pname); enqueue(currpid, queueC_tail); //enqueue(0,queueC_tail); } } //srand(1); generate_random: num = rand(); //kprintf("\n rand 1: %d", num); num = (num%97); //kprintf("\n normalized rand 1: %d", num); if(num>=50) { if(nonempty(queueA_head)) { itemA = getfirst(queueA_head); //kprintf("\nresched Queue A, %d",itemA); nptr = &proctab[ (currpid = itemA)];//getfirst(queueA_head)) ]; nptr->pstate = PRCURR; /* mark it currently running */ #ifdef RTCLOCK preempt = QUANTUM; /* reset preemption counter */ #endif ctxsw((int)&optr->pesp, (int)optr->pirmask, (int)&nptr->pesp, (int)nptr->pirmask); /* The OLD process returns here when resumed. */ return OK; } else goto generate_random;//resched(); } else if(num>20 && num <50) { if(nonempty(queueB_head)) { itemB = getfirst(queueB_head); //kprintf("\nresched Queue B, %d, %s",itemB, proctab[itemB].pname); nptr = &proctab[ (currpid = itemB)];//getfirst(queueB_head)) ]; nptr->pstate = PRCURR; /* mark it currently running */ #ifdef RTCLOCK preempt = QUANTUM; /* reset preemption counter */ #endif ctxsw((int)&optr->pesp, (int)optr->pirmask, (int)&nptr->pesp, (int)nptr->pirmask); /* The OLD process returns here when resumed. */ return OK; } else goto generate_random;//resched(); } else if(num<=20) { if(nonempty(queueC_head)) { itemC = getfirst(queueC_head); //kprintf("\nresched Queue C, %d, %s",itemC, proctab[itemC].pname); nptr = &proctab[ (currpid = itemC)];//getfirst(queueC_head)) ]; nptr->pstate = PRCURR; /* mark it currently running */ #ifdef RTCLOCK preempt = QUANTUM; /* reset preemption counter */ #endif ctxsw((int)&optr->pesp, (int)optr->pirmask, (int)&nptr->pesp, (int)nptr->pirmask); /* The OLD process returns here when resumed. */ return OK; } else goto generate_random;//resched(); } } else if(sched_type == LINUXSCHED) { //int usedPreempt = preempt; int highProc; int oldPriority = proctab[currpid].pprio; //save the current process' priority so that the effect of chprio() or change in priority to the quantum // or goodness is reflected in new epoch //int procGoodness = 0; int startNewEpoch = 1; //disable(ps); proctab[currpid].quantumLeft = preempt; if(switched_to_linuxsched==1) { proctab[currpid].runnableProc = 1; switched_to_linuxsched=0; proctab[currpid].procCpuTicks += (QUANTUM - preempt); proctab[currpid].assignedQuantum = proctab[currpid].assignedQuantum -(QUANTUM - preempt); } else { proctab[currpid].procCpuTicks += (proctab[currpid].assignedQuantum - preempt); proctab[currpid].assignedQuantum = proctab[currpid].quantumLeft; } if(optr->pstate == PRCURR) { optr->pstate = PRREADY; insert(currpid, rdyhead, proctab[currpid].procGoodness); } highProc = rdytail; while(q[highProc].qprev!=rdyhead && q[highProc].qprev>=0 && q[highProc].qprev<NPROC) //q[temp].qnext!=rdytail && q[temp].qnext>=0 && q[temp].qnext<NPROC { //kprintf("\t pbdsf%s",proctab[q[highProc].qprev].pname); if((proctab[q[highProc].qprev].runnableProc == 1) && (proctab[q[highProc].qprev].assignedQuantum > 0 ) ) //q[highProc].qnext!=0 && { //kprintf("\n bacha hai : %d, %s",q[highProc].qprev, proctab[q[highProc].qprev].pname); startNewEpoch = 0; break; } highProc = q[highProc].qprev; } if(startNewEpoch == 1) { //kprintf("\nNew Epoch started---*******"); for(i=0; i<NPROC; i++) { if(proctab[i].pstate != PRFREE) { oldPriority = proctab[i].pprio; //prevent priority change effect in calculation of quantum //proctab[i].runnableProc = 1; //calculate quantum of each process either sleeping/waiting or ready if(proctab[i].runnableProc == 1) { if(proctab[i].assignedQuantum > 0) { proctab[i].assignedQuantum = (oldPriority + (proctab[i].assignedQuantum /2)); proctab[i].procGoodness = (oldPriority + proctab[i].quantumLeft); //kprintf("\nNew quantum and goodness %d, %d, %s",proctab[i].assignedQuantum, proctab[i].procGoodness, proctab[i].pname); } else { proctab[i].assignedQuantum = oldPriority; proctab[i].procGoodness = 0;//(oldPriority); //kprintf("\nused up quantum %d, %d, %s",proctab[i].assignedQuantum, proctab[i].procGoodness, proctab[i].pname); } } proctab[i].runnableProc = 1; //kprintf("\nFinal values of quantum and goodness %d, %d, %s",proctab[i].assignedQuantum, proctab[i].procGoodness, proctab[i].pname); dequeue(i); insert(i, rdyhead, proctab[i].procGoodness); } } } //goodness is to be calculated for process in same epoch also ? -> no highProc = rdytail; while(q[highProc].qprev!=rdyhead && q[highProc].qprev>=0 && q[highProc].qprev<NPROC) { /*kprintf("\n Highest proc based on goodness %d, %d, %d", q[highProc].qprev, proctab[q[highProc].qprev].procGoodness, proctab[q[highProc].qprev].assignedQuantum); if(proctab[q[highProc].qprev].pstate == PRREADY) { kprintf("\nstate: PRREADY, %d", proctab[q[highProc].qprev].runnableProc); }*/ if( (proctab[q[highProc].qprev].runnableProc == 1) && (proctab[q[highProc].qprev].assignedQuantum > 0 ) )//&& (proctab[q[highProc].qprev].pstate==PRREADY) && q[highProc].qprev!=0 ) { //kprintf(" \ninside----------- %d", currpid); currpid = dequeue(q[highProc].qprev); //kprintf(" \nnew pid----------- %d", currpid); //if(currpid != 0) //{ nptr = &proctab[currpid]; nptr->pstate = PRCURR; /* mark it currently running */ #ifdef RTCLOCK preempt = nptr->assignedQuantum; /* reset preemption counter */ #endif ctxsw((int)&optr->pesp, (int)optr->pirmask, (int)&nptr->pesp, (int)nptr->pirmask); /* The OLD process returns here when resumed. */ return ;// OK; break; } else if( (q[rdyhead].qnext == 0) && (q[rdytail].qprev== 0)) { //proctab[currpid].assignedQuantum = QUANTUM; // kprintf(" \ninside----------- %d", currpid); nptr = &proctab[(currpid=0)]; nptr->pstate = PRCURR; /* mark it currently running */ #ifdef RTCLOCK preempt = QUANTUM; /* reset preemption counter */ #endif ctxsw((int)&optr->pesp, (int)optr->pirmask, (int)&nptr->pesp, (int)nptr->pirmask); /* The OLD process returns here when resumed. */ return;// OK; break; } highProc = q[highProc].qprev; } } else if(sched_type!=RANDOMSCHED && sched_type!=LINUXSCHED) { /* no switch needed if current process priority higher than next*/ if ( ( optr->pstate == PRCURR) && (lastkey(rdytail)<optr->pprio)) { return(OK); } /* force context switch */ if (optr->pstate == PRCURR) { optr->pstate = PRREADY; insert(currpid,rdyhead,optr->pprio); } proctab[currpid].quantumLeft = preempt; proctab[currpid].procCpuTicks += (QUANTUM - preempt); /* remove highest priority process at end of ready list */ nptr = &proctab[ (currpid = getlast(rdytail)) ]; nptr->pstate = PRCURR; /* mark it currently running */ #ifdef RTCLOCK preempt = QUANTUM; /* reset preemption counter */ #endif ctxsw((int)&optr->pesp, (int)optr->pirmask, (int)&nptr->pesp, (int)nptr->pirmask); /* The OLD process returns here when resumed. */ return OK; } }
/*----------------------------------------------------------------------- * resched -- reschedule processor to highest priority ready process * * Notes: Upon entry, currpid gives current process id. * Proctab[currpid].pstate gives correct NEXT state for * current process if other than PRREADY. *------------------------------------------------------------------------ */ int resched() { register struct pentry *optr; /* pointer to old process entry */ register struct pentry *nptr; /* pointer to new process entry */ int rand_num,temp_currpid,i; /* no switch needed if current process priority higher than next*/ if(used_scheduler == RANDOMSCHED) { optr= &proctab[currpid]; } else if(used_scheduler == LINUXSCHED) { optr= &proctab[currpid]; } else { if ( ( (optr= &proctab[currpid])->pstate == PRCURR) && (lastkey(rdytail)<optr->pprio)) { return(OK); } } /* force context switch */ if (optr->pstate == PRCURR) { optr->pstate = PRREADY; optr->arrival_time = ctr1000; if(preempt >= 0) optr->quantum_left = preempt; insert(currpid,rdyhead,optr->pprio); } /* remove highest priority process at end of ready list */ if(used_scheduler != LINUXSCHED) { execution_time[currpid]+=(QUANTUM-preempt); } else execution_time[currpid]+= (proctab[currpid].quantum_left -preempt); //kprintf("\nPreempt: %d",preempt); if(used_scheduler == RANDOMSCHED) { if(q[rdyhead].qnext == rdytail) return(OK); do { rand_num = rand()%100; // kprintf("Random Number: %d",rand_num); if(rand_num >=80) temp_currpid = random_getpid(1); else if(rand_num <80 && rand_num >=50) temp_currpid = random_getpid(2); else temp_currpid = random_getpid(3); }while(temp_currpid == -1); currpid = temp_currpid; nptr = &proctab[currpid]; dequeue(currpid); //kprintf("\n Current PID : %d",currpid); } else if(used_scheduler == LINUXSCHED) { temp_currpid = linux_getpid(); if(temp_currpid == -1) { new_epoch(); temp_currpid = linux_getpid(); if(temp_currpid == -1) { temp_currpid=0;; } } currpid = temp_currpid; nptr = &proctab[currpid]; dequeue(currpid); } else { nptr = &proctab[ (currpid = getlast(rdytail)) ]; } nptr->pstate = PRCURR; /* mark it currently running */ #ifdef RTCLOCK if(used_scheduler != LINUXSCHED) preempt = QUANTUM; /* reset preemption counter */ else { preempt = proctab[currpid].quantum_left; } #endif ctxsw((int)&optr->pesp, (int)optr->pirmask, (int)&nptr->pesp, (int)nptr->pirmask); /* The OLD process returns here when resumed. */ return OK; }