📄 swap.c
字号:
} else{ datastart = swap->psize.entry + swap->psize.tsize; } dsize = swap->psize.dsize + swap->psize.bsize; tsize = swap->psize.tsize;#ifdef TSWAPprintf("Sizes %x %x %x\n",tsize, dsize, swap->psize.stacks);pswap();#endif /* Get text area back into core */ if(swap->textseg == SWAPDISK) gettext(swap); else bytecp(memstart(SWAPSEG, swap->textseg), swap->psize.entry, tsize); /* Get data area from swap space into core */ bytecp(memstart(SWAPSEG, swap->dataseg), datastart, dsize); bytecp((memstart(SWAPSEG, swap->dataseg) + dsize), swap->psize.ustack - swap->psize.stacks, swap->psize.stacks);#ifdef TSWAPprintf("SWAPIN: swaped in \n");#endif if(swap->pid != SWAPSTICK){ swap->pid = 0; if(swap->textseg != SWAPDISK) endmem(SWAPSEG, swap->textseg); if(swap->dataseg != SWAPDISK) endmem(SWAPSEG, swap->dataseg); }#ifdef TSWAPpswap();#endif return 0;}/* * Gettext - Gets Object named text segment from disk into core. */gettext(swap)struct Swapspace *swap;{ struct file file; struct inode *iptr; if(iptr = getiptr(swap->object.majdev, swap->object.mindev, swap->object.inode)){ /* Open file */ if(f_open(&file, relock(iptr), READABLE) == -1){ freeiptr(iptr); return -1; } f_seek(&file, sizeof(struct bhdr), 0); /* Read in text segment */ if(f_read(&file, swap->psize.entry, swap->psize.tsize) != swap->psize.tsize){ /* read error */ f_close(&file); freeiptr(iptr); return -1; } f_close(&file); freeiptr(iptr); return 0; } else return -1;}/* Swap table status enquiry */s_status(no,pointer)int no;struct Swapspace *pointer;{ struct Swapspace *swap; /* Checks if swap table entry exists */ if(no >= NSWAPS) return -1; /* Get swapspace entry and write to given location */ swap = &swapspace[no]; bytecp(swap,pointer,sizeof(struct Swapspace)); return (int)pointer;}/* * Swapcheck() Checks if a given object is in the swapspace */swapcheck(object,type)struct Object *object;int type;{ int swapno; struct Swapspace *swap; swap = swapspace; for(swapno = 0; swapno < NSWAPS; swapno++){ if(swap->pid && (swap->pid != SWAPCREATE) && (swap->object.majdev == object->majdev) && (swap->object.mindev == object->mindev) && (swap->object.inode == object->inode)){ /* Return the segment number if found */ if(type & STEXT){ if(swap->textseg != SWAPDISK) return swap->textseg; } else return swap->dataseg; } swap++; } return -1;}/* * Getsarea() Gets a segment area in the swap space of a mininum size * of totals, returns number of swap area or * -1 if not posible. * If type is SSTICK then only tries a bit. * If trying to swap out a text area then if unable to * find a place returns SWAPDISK (-2) to indicate that * the area on disk could be used. * If trying to swap out a data area and and no space is * available, then a text area will be released and its * apropriate area on disk will be used for later * retreivial. */getsarea(totals,type)unsigned long totals;int type;{ short swapno, segno, ts; struct Swapspace *swap;#ifdef TSWAPprintf("GETSAREA: tidyied swap size %x\n",totals);pswap();#endif /* Check if there is a big enough segment available */ if((segno = getmem(SWAPSEG, totals)) != -1) return segno;#ifdef TSWAPprintf("STAGE 1 fail\n");#endif /* Compress the segspace up and try again */ mcomp_seg(SWAPSEG, swapupseg); /* Compress seg area */ if((segno = getmem(SWAPSEG, totals)) != -1) return segno;#ifdef TSWAPprintf("STAGE 2 fail\n");#endif /* If still no free space see if a sticky proccess can be removed */ swap = swapspace; /* Sets up swap area pointer to first element */ for(swapno=0; swapno <NSWAPS; swapno++){ /* Check if there is a sticky proccess in this segment * and has a text area of sufficient size to hold the data * And no real proccess is using this text segment */ if((swap->pid == SWAPSTICK) && (swap->psize.tsize >= totals) && (swap->textseg != SWAPDISK) && (nlockmem(SWAPSEG, swap->textseg) == 1)){ /* Realease swap segments for use */ endmem(SWAPSEG, swap->textseg); endmem(SWAPSEG, swap->dataseg); /* Clears and returns segno */ swap->pid = 0; if((segno = getmem(SWAPSEG,totals)) != -1) return segno; } swap++; }#ifdef TSWAPprintf("STAGE 3 fail\n");#endif /* If trying to swap out a sticky proccess give up at this point */ if(type & SSTICK) return -1; /* Else compress the seg space completely */ swapclr(); /* Remove all sticky proccesses */ mcomp_seg(SWAPSEG, swapupseg); /* Compress seg area */ if((segno = getmem(SWAPSEG, totals)) != -1) return segno; /* If still no free space and swaping the data segment of a running * process, see if a text segment can be removed */ if(state.warning) printf("SWAP: getting difficult text area on disk\n"); swap = swapspace; /* Sets up swap area pointer to first element */ for(swapno=0; swapno <NSWAPS; swapno++){ /* Check if there is a proccess in this segment * and has a text area of sufficient size to hold the data * If so realese segment and set all segment pointers * to SWAPDISK. */ if(swap->pid && (swap->psize.tsize >= totals) && (swap->textseg != SWAPDISK)){ /* Realease swap text segment for use */ while(endmem(SWAPSEG, swap->textseg) != -1); ts = swap->textseg; /* Alter swapspace segment number pointers */ /* Segment is now on the disk */ swap = swapspace; for(swapno = 0; swapno < NSWAPS; swapno++){ if(swap->textseg == ts) swap->textseg= SWAPDISK; swap++; } break; } swap++; } /* The last ditch atempt to copy the data segment if this fails * Goodbye!!!!!!!! */ mcomp_seg(SWAPSEG, swapupseg); /* Compress seg area */ if((segno = getmem(SWAPSEG, totals)) != -1) return segno; /* If still no room and tring to swap a text segment then return * SWAPDISK to indicate that the area will be on the disk */ if(type & STEXT) return SWAPDISK; printf("SWAP: Swap space out of room\n"); pswap(); return -1;}/* * Swapupseg() Update swap segments */swapupseg(from, to)int from, to;{ int swapno; struct Swapspace *swap; /* Alter swapspace segment number pointers */ swap = swapspace; for(swapno = 0; swapno < NSWAPS; swapno++){ if(swap->textseg == from) swap->textseg = to; if(swap->dataseg == from) swap->dataseg = to; swap++; } return 0;}/* * Swapclr() Clears all sticky processes from swap area */swapclr(){ short swapno; struct Swapspace *swap; /* Remove all sticky proccesses */ swap = &swapspace[0]; /* Sets up swap area pointer to first segment */ for(swapno = 0; swapno < NSWAPS; swapno++){ /* If a sticky proccess remove */ if(swap->pid == SWAPSTICK){ /* Realease swap segments for use */ endmem(SWAPSEG, swap->textseg); endmem(SWAPSEG, swap->dataseg); swap->pid = 0; } swap++; }}pswap(){ long c,total; struct Minseg *seg; struct Swapspace *swap; swap = &swapspace[0]; printf("Swap space\n"); printf("Pid Text Data totalsize\n"); for(c=0; c<NSWAPS; c++){ total = swap->psize.dsize+swap->psize.bsize+swap->psize.stacks; total += swap->psize.tsize; printf("%d ",swap->pid); printf("%x %x %x\n",swap->textseg,swap->dataseg,total); if((c > 6) && !swap->pid) break; swap++; } seg = mem_maj[SWAPSEG].segs; printf("inuse Start End\n"); for(c=0; c<mem_maj[SWAPSEG].nsegs; c++){ printf("%d %x %x\n",seg->inuse, seg->start, seg->end); if((c > 16) && !seg->inuse) break; seg++; } getchar();}psize(p)struct procs *p;{ printf("Entry Tsize dsize bsize ebss ssize\n"); printf("%x %x %x %x %x %x\n",p->psize.entry,p->psize.tsize,p->psize.dsize,p->psize.bsize,p->psize.ebss,p->psize.stacks);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -