📄 sysdep.cc
字号:
// sysdep.cc// Implementation of system-dependent interface. Nachos uses the // routines defined here, rather than directly calling the UNIX library,// to simplify porting between versions of UNIX, and even to// other systems, such as MSDOS.//// On UNIX, almost all of these routines are simple wrappers// for the underlying UNIX system calls.//// NOTE: all of these routines refer to operations on the underlying// host machine (e.g., the DECstation, SPARC, etc.), supporting the // Nachos simulation code. Nachos implements similar operations,// (such as opening a file), but those are implemented in terms// of hardware devices, which are simulated by calls to the underlying// routines in the host workstation OS.//// This file includes lots of calls to C routines. C++ requires// us to wrap all C definitions with a "extern "C" block".// This prevents the internal forms of the names from being// changed by the C++ compiler.//// Copyright (c) 1992-1996 The Regents of the University of California.// All rights reserved. See copyright.h for copyright notice and limitation // of liability and disclaimer of warranty provisions.#include "copyright.h"#include "debug.h"#include "sysdep.h"#include "stdlib.h"#include "unistd.h"#include "sys/time.h"#include "sys/file.h"#include <sys/socket.h>#include <sys/un.h>#ifdef SOLARIS// KMS// for open()#include <fcntl.h>#endif#ifdef LINUX // at this point, linux doesn't support mprotect #define NO_MPROT #endif#ifdef DOS // neither does DOS#define NO_MPROT#endifextern "C" {#include <signal.h>#include <sys/types.h>#ifndef NO_MPROT #include <sys/mman.h>#endif// UNIX routines called by procedures in this file #if defined CYGWIN size_t getpagesize(void);#else int getpagesize(void);#endifunsigned sleep(unsigned);//#ifdef SOLARIS//int usleep(useconds_t);//#else//void usleep(unsigned int); // rcgood - to avoid spinning processes.//#endif#ifndef NO_MPROT #ifdef OSF#define OSF_OR_AIX#endif#ifdef AIX#define OSF_OR_AIX#endif#ifdef OSF_OR_AIXint mprotect(const void *, long unsigned int, int);#elseint mprotect(char *, unsigned int, int);#endif#endif#if defined(BSD) || defined(SOLARIS) || defined(LINUX)//KMS// added Solaris and LINUXint select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);#elseint select(int numBits, void *readFds, void *writeFds, void *exceptFds, struct timeval *timeout);#endifint socket(int, int, int);#if defined(SUNOS) || defined(ULTRIX)long tell(int);int bind (int, const void*, int);int recvfrom (int, void*, int, int, void*, int *);int sendto (int, const void*, int, int, void*, int);#endif}//----------------------------------------------------------------------// CallOnUserAbort// Arrange that "func" will be called when the user aborts (e.g., by// hitting ctl-C.//----------------------------------------------------------------------void CallOnUserAbort(void (*func)(int)){ (void)signal(SIGINT, func);}//----------------------------------------------------------------------// RegisterSignal// Arrange that "func" will be called when NachOS get a signal from// the host operating system.//----------------------------------------------------------------------void RegisterSignalHandler(void (*func)(int), int sig){ (void)signal(sig, func);}//----------------------------------------------------------------------// Delay// Put the UNIX process running Nachos to sleep for x seconds,// to give the user time to start up another invocation of Nachos// in a different UNIX shell.//----------------------------------------------------------------------void Delay(int seconds){ (void) sleep((unsigned) seconds);}//----------------------------------------------------------------------// UDelay// Put the UNIX process running Nachos to sleep for x microseconds,// to prevent an idle Nachos process from spinning...//----------------------------------------------------------------------void UDelay(unsigned int useconds){//#ifdef SOLARIS// usleep(useconds_t useconds);//#else// usleep(useconds);//#endif /* SOLARIS */}//----------------------------------------------------------------------// Abort// Quit and drop core.//----------------------------------------------------------------------void Abort(){ abort();}//----------------------------------------------------------------------// Exit// Quit without dropping core.//----------------------------------------------------------------------void Exit(int exitCode){ exit(exitCode);}//----------------------------------------------------------------------// RandomInit// Initialize the pseudo-random number generator. We use the// now obsolete "srand" and "rand" because they are more portable!//----------------------------------------------------------------------void RandomInit(unsigned seed){ srand(seed);}//----------------------------------------------------------------------// RandomNumber// Return a pseudo-random number.//----------------------------------------------------------------------unsigned int RandomNumber(){ return rand();}//----------------------------------------------------------------------// AllocBoundedArray// Return an array, with the two pages just before // and after the array unmapped, to catch illegal references off// the end of the array. Particularly useful for catching overflow// beyond fixed-size thread execution stacks.//// Note: Just return the useful part!//// "size" -- amount of useful space needed (in bytes)//----------------------------------------------------------------------char * AllocBoundedArray(int size){#ifdef NO_MPROT return new char[size];#else int pgSize = getpagesize(); char *ptr = new char[pgSize * 2 + size]; mprotect(ptr, pgSize, 0); mprotect(ptr + pgSize + size, pgSize, 0); return ptr + pgSize;#endif}//----------------------------------------------------------------------// DeallocBoundedArray// Deallocate an array of integers, unprotecting its two boundary pages.//// "ptr" -- the array to be deallocated// "size" -- amount of useful space in the array (in bytes)//----------------------------------------------------------------------#ifdef NO_MPROTvoid DeallocBoundedArray(char *ptr, int /* size */){ delete [] ptr;}#elsevoid DeallocBoundedArray(char *ptr, int size){ int pgSize = getpagesize(); mprotect(ptr - pgSize, pgSize, PROT_READ | PROT_WRITE | PROT_EXEC); mprotect(ptr + size, pgSize, PROT_READ | PROT_WRITE | PROT_EXEC); delete [] (ptr - pgSize);}#endif//----------------------------------------------------------------------// PollFile// Check open file or open socket to see if there are any // characters that can be read immediately. If so, read them// in, and return TRUE.//// "fd" -- the file descriptor of the file to be polled//----------------------------------------------------------------------boolPollFile(int fd){#if defined(SOLARIS) || defined(LINUX)// KMS fd_set rfd,wfd,xfd;#else int rfd = (1 << fd), wfd = 0, xfd = 0;#endif int retVal; struct timeval pollTime;#if defined(SOLARIS) || defined(LINUX)// KMS FD_ZERO(&rfd); FD_ZERO(&wfd); FD_ZERO(&xfd); FD_SET(fd,&rfd);#endif// don't wait if there are no characters on the file pollTime.tv_sec = 0; pollTime.tv_usec = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -