⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 swap.c

📁 用于motorala 68K系列处理器的小实时多任务操作系统 The OMU Kernel was written to provide a cut-down Unix-like O/S for a
💻 C
📖 第 1 页 / 共 2 页
字号:
/****************************************************************************** *	Swap.c		Process swaper bits ****************************************************************************** */# include	"../include/param.h"# include	<a.out.h># include	"../include/inode.h"# include	"../include/signal.h"# include	"../include/procs.h"# include	"../include/memory.h"# include	"../include/swap.h"# include	<errno.h># include	"../include/stat.h"# include	<sys/dir.h># include	"../include/file.h"# include	"../include/state.h"/* *	Swapspace definitions */int	swapupseg();struct	Swapspace swapspace[NSWAPS];/****************************************************************************** *	Swapout		Swaps given proccess out of core ****************************************************************************** */swapout(proc)struct procs *proc;{#ifdef TSWAPprintf("SWAPOUT swaping %s\n\r",proc->name);#endif	return swappout(proc, SPROC);}/****************************************************************************** *	Stickout	Swaps given proccess out of core ****************************************************************************** */stickout(proc)struct procs *proc;{#ifdef TSWAPprintf("SWAPOUT swaping sticky %s\n\r",proc->name);#endif	return swappout(proc, SSTICK);}/****************************************************************************** *	Swappout		Swaps given proccess out of core ****************************************************************************** */swappout(proc, type)struct procs *proc;int	type;{	unsigned long	datastart;	unsigned long	tsize, dsize, ssize;	int	swapno, tswapno, segno;	struct	Swapspace *swap, *tswap;		#ifdef TSWAPprintf("Swap out %s %d\n",proc->name,proc->pid);psize(proc);pswap();#endif	/*	 *	Gets a swap-space table entry	 */	swap = swapspace;	for(swapno = 0; swapno < NSWAPS; swapno++){		if(!swap->pid) break;		swap++;	}	if(swapno == NSWAPS){		printf("SWAPOUT: no swapspace table entries left\n\r");		pswap();		return -1;	}	/* Indicate swap table entry being created */	swap->pid = SWAPCREATE;	swap->textseg = SWAPDISK;	swap->dataseg = SWAPDISK;	/* Initialy no sizes for text or data */	swap->psize.tsize = 0;	swap->psize.dsize = 0;	swap->psize.bsize = 0;	swap->psize.stacks = 0;	/* Work out program segment sizes */	tsize = proc->psize.tsize;	if(proc->psize.fmagic == NMAGIC){		datastart = ((proc->psize.entry + proc->psize.tsize) & MMUMASK) + MMUBOUND;	}	else{		datastart = proc->psize.entry + proc->psize.tsize;	}	/* Get data area and stack size only if normal process */	dsize = proc->psize.ebss - datastart;	if(type != SSTICK) ssize = proc->psize.ustack - getusp();	else ssize = 0;#ifdef TSWAPprintf("usr sp %x\n",getusp());printf("Swap out tsize %x, dsize %x ssize %x\n",tsize,dsize,ssize);#endif	/* Checks if text area exists in swap space */	if((segno = swapcheck(&proc->object, STEXT)) == -1){		/* Get a free area if none return -1 */		if((segno = getsarea(tsize, (type | STEXT))) == -1){			swap->pid = 0;			return -1;		}#ifdef TSWAPprintf("SWAPOUT: copying text segment %x\n",tsize);printf("From %x to %x\n",proc->psize.entry,segarea[segno].start);#endif		/* Checks if getsarea returned a flag indicating that		 * The area is on disk, if not area is in swap space		 * So copy text segment there.		 */		if(segno != SWAPDISK){			bytecp(proc->psize.entry,memstart(SWAPSEG,segno),tsize);			/* Checks if any processes exist with the same text area		 	* with the text area on disk, if so set its text pointer		 	* to this new swap space segment		 	*/			tswap = &swapspace[0];			for(tswapno = 0; tswapno < NSWAPS; tswapno++){				/* If process with the same ID exists and using			 	* disk for text segment set text segment to new			 	* one created.			 	*/				if((tswap != swap) && (tswap->pid) &&					(tswap->textseg == SWAPDISK) &&					(tswap->object.majdev == proc->object.majdev) &&					(tswap->object.mindev == proc->object.mindev) &&					(tswap->object.inode == proc->object.inode)){					tswap->textseg = segno;					relockmem(SWAPSEG, segno);				}				tswap++;			}		}	}	else {		relockmem(SWAPSEG, segno);	}	/* Set up text segment number and size */	swap->textseg = segno;	swap->psize.tsize = tsize;	/* At this stage the context of the process will be saved so no	 * User action can now take place and so the process can be assumed	 * to be out of core. (All gone ...)	 */	if(type != SSTICK) proc->flag &= ~SLOAD;	/* Does data segment */	/* Get a free area if none return -1 */	if((segno = getsarea((dsize + ssize), (type | SDATA))) == -1){		if(swap->textseg != SWAPDISK) endmem(SWAPSEG,swap->textseg);		swap->pid = 0;		return -1;	}#ifdef TSWAPprintf("SWAPOUT: copying data segment %x, %x, %x\n",dsize,ssize,dsize+ssize);printf("From %x to %x\n",datastart,segarea[segno].start);printf("From %x to %x\n",getusp(),segarea[segno].start+dsize);#endif	bytecp(datastart, memstart(SWAPSEG,segno), dsize);	bytecp(getusp(), (memstart(SWAPSEG,segno) + dsize), ssize);	/* Set up data segment number */	swap->dataseg = segno;	/* Copies object description and process size info to swap table */	bytecp(&proc->object, &swap->object, sizeof(struct Object));	bytecp(&proc->psize, &swap->psize, sizeof(struct Psize));	swap->psize.stacks = ssize;	strcpy(swap->name, proc->name);	/* Set up swap table entry */	if(type == SPROC) swap->pid = proc->pid;	else swap->pid = SWAPSTICK;#ifdef TSWAPtidyseg();pswap();printf("SWAPOUT: returning\n");#endif	return 0;}/****************************************************************************** *	Inswap		Checks if named proccess is in swap space ****************************************************************************** */inswap(object, psize)struct Object *object;struct Psize *psize;{	short	swapno;	struct	Swapspace *swap;#ifdef	TSWAPprintf("INSWAP: Checking if in swap\n");#endif	swap = &swapspace[0];	for(swapno=0; swapno <NSWAPS; swapno++){		/* If sticky process with the same ID exists break out */		if((swap->pid == SWAPSTICK) &&			(swap->object.majdev == object->majdev) &&			(swap->object.mindev == object->mindev) &&			(swap->object.inode == object->inode)) break;					swap++;	}	if(swapno >= NSWAPS) return -1;	bytecp(&swap->psize, psize, sizeof(struct Psize));	return swapno;}/****************************************************************************** *	Swapin		Swaps given proccess back into core ****************************************************************************** */swapin(proc)struct procs *proc;{	short	swapno;	struct	Swapspace *swap;#ifdef TSWAPprintf("Swapin process %d, %s\n",proc->pid,proc->name);psize(proc);pswap();#endif	swap = &swapspace[0];	for(swapno=0; swapno <NSWAPS; swapno++){		if(swap->pid == proc->pid) break;		swap++;	}	if(swapno >= NSWAPS){		printf("Cannot swapin proccess not in swap table %d\n",proc->pid);		pswap();		return -1;	}	swapino(swapno);	/* Process is now in core */	proc->flag |= SLOAD;	return 0;}/****************************************************************************** *	Stickin		Swaps given sticky entry back into core ****************************************************************************** */stickin(object)struct	Object *object;{	short	swapno;	struct	Swapspace *swap;#ifdef TSWAPprintf("Stickin Object\n");pswap();#endif	swap = &swapspace[0];	for(swapno=0; swapno <NSWAPS; swapno++){		/* If sticky process with the same ID exists break out */		if((swap->pid == SWAPSTICK) &&			(swap->object.majdev == object->majdev) &&			(swap->object.mindev == object->mindev) &&			(swap->object.inode == object->inode)) break;					swap++;	}	if(swapno >= NSWAPS) return -1;	return swapino(swapno);}/****************************************************************************** *	Swapino		Swaps given swap entry back into core ****************************************************************************** */swapino(swapno)short swapno;{	unsigned long	tsize, datastart, dsize;	struct	Swapspace *swap;	swap = &swapspace[swapno];#ifdef TSWAPprintf("Swap in %s\n",swap->name);pswap();#endif	if(swap->psize.fmagic == NMAGIC){		datastart = ((swap->psize.entry + swap->psize.tsize) & MMUMASK) + MMUBOUND;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -