Presentation is loading. Please wait.

Presentation is loading. Please wait.

19/03/2006 SSST CS260 F. Hadziomerovic 1 Kernel Queues ^ MININT 432 14 24 25 332 MAXINT ^ 4 2 Head = 32 Tail = 33 NPROC - 1 NPROC 5 4 14 2 32 3 2 25 33.

Similar presentations


Presentation on theme: "19/03/2006 SSST CS260 F. Hadziomerovic 1 Kernel Queues ^ MININT 432 14 24 25 332 MAXINT ^ 4 2 Head = 32 Tail = 33 NPROC - 1 NPROC 5 4 14 2 32 3 2 25 33."— Presentation transcript:

1 19/03/2006 SSST CS260 F. Hadziomerovic 1 Kernel Queues ^ MININT MAXINT ^ 4 2 Head = 32 Tail = 33 NPROC - 1 NPROC Head at 32 MININT 4 ^ Tail at 33 MAXINT ^ 2 pid qkey qnext qprev q array of qent structures /* q.h */ #define NQENT NPROC+NSEM+NSEM+4 struct qent { int qkey; int qnext; int qprev; } extern struct qent q[ ]; extern int nextqueue; /* inline procedures*/ #define isempty(head) (q[(list)].qnext>=NPROC) #define nonempty(head) (q[(list)].qnext < NPROC) #define firstkey(head) (q[q[(head)].qnext].qkey) #define lastkey(tail) (q[q[(tail)].qprev].qkey) #define firstid(head) (q[(head)].qnext) #define EMPTY -1 Queue as a linked list

2 19/03/2006 SSST CS260 F. Hadziomerovic 2 FIFO queue procedures /* enqueue – insert item at the tail */ int enqueue (int item, int tail) { struct qent *tptr; struct qent *mptr; tptr = &q[tail]; mprt = &q[item]; mptr->qnext = tail; /* 1 */ mptr->qprev = tptr->qprev; /* 2 */ q[tptr->qprev].qnext = item; /* 3 */ tprt->qprev = item; /* 4 */ return(item); } prev item tail /* dequeue – removes item from the list */ int dequeue (int item) { struct qent *mptr; mprt = &q[item]; q[mptr->qprev].qnext = mptr->qnext; /*1*/ q[mptr->qnext].qprev = mptr->qprev; /*2*/ return(item); } prev item next 1 2

3 19/03/2006 SSST CS260 F. Hadziomerovic 3 Priority queue procedures /* insert – insert item in priority order */ int insert (int proc, int head, int key) { int next; int prev; next = q[head].qnext; while (q[next].qkey < key) next = qnext[next].qnext; q[proc].qnext = next; /* 1 */ q[proc].qprev = prev = q[next].qprev; /*2*/ q[proc].qkey = key; /* 3 */ q[prev].qnext = proc; /* 4 */ q[next].qprev = proc; /* 5 */ return(OK); /* OK is 1 */ } /* getfirst returns first process on the list */ int getfirst (int head) { int proc; if ( (proc = q[head].qnext) < NPROC) return(dequeue (proc) ); else return(EMPTY); } /* getlast returns last process from the list */ int getlast (int tail) { int proc; if ( (proc = q[tail].qprev) < NPROC) return(dequeue (proc) ); else return(EMPTY); }

4 19/03/2006 SSST CS260 F. Hadziomerovic 4 Queue initialization /* newqueue intializes new linked list */ int newqueue ( ) { struct qent *hptr; struct qent *tptr; int hindex, tindex; hptr = &q[hindex = nextqueue++] ; /* nextqueue is global variable */ tptr = &q[tindex = nextqueue ++ ] ; hptr->qnext = tindex; hptr->qprev = EMPTY; hptr->qkey = MININT; tptr->qnext = EMPTY; tptr->qprev = hindex; tptr->qkey = MAXINT; return (hindex); }

5 19/03/2006 SSST CS260 F. Hadziomerovic 5 /* proc.h – isbadpid */ /* process state constants */ #define PRCURR \01 /* running */ #define PRFREE \02 /* process slot is free */ #define PRREADY \03 #define PRRECV \04 /* waiting for msg */ #define PRSLEEP \05 #define PRSUSP \06 /* suspended */ #define PRWAIT \07 /* waiting in sem que */ #define isbadpid(x) (x =NPROC) struct pentry { char pstate; int pprio; int psem; /* sem where proc waits */ int pmsg; /* msg sent to this process */ char *pregs; /* saved regs (SP) */ char *pbase; /* base of the run-time stack */ word plen; /* stack length */ char pname[PNMLEN+1]; /* proc name */ int pargs; /* number of arguments */ int (*paddr) ( ); /* code address */ } extern struct pentry proctab [ ]; extern int numproc; /* no of currently active proc*/ extern int nextproc; /* next free slot */ extern int currpid; /* pid of running proc */

6 19/03/2006 SSST CS260 F. Hadziomerovic 6 /* resched.c */ #include int resched { register struct pentry *optr; register struct pentry *nptr; optr = &proctab[currpid]; if (lastkey(rdytail) pprio) return; /* no switch */ nptr = &proctab[ currpid = getlast(rdytail) ]; nptr->pstate = PRCURR; preempt = QUANTUM; /* no of ticks before preemption */ ctxsw ( &optr->pregs, &nptr->pregs ); /* the old process returns here when resumed */ return; }

7 19/03/2006 SSST CS260 F. Hadziomerovic 7 ; ctxsw.asm - _ctxsw _ctxsw proc near 1.push bp 2.mov bp, sp 3.pushf ; push flags 4.cli ; clear interrupts 5.push r1 ; save regs on stack push rn 8.mov bx, [bp + 4] 9.mov [bx], sp 10.mov bx, [bp + 6] 11.mov sp, [bx] 12.pop rn pop r1 15.popf 16.pop bp 17.ret nptr optr flags bp SP ret addr rn r SP 11


Download ppt "19/03/2006 SSST CS260 F. Hadziomerovic 1 Kernel Queues ^ MININT 432 14 24 25 332 MAXINT ^ 4 2 Head = 32 Tail = 33 NPROC - 1 NPROC 5 4 14 2 32 3 2 25 33."

Similar presentations


Ads by Google