tac0S
Template Affectional Command Operating System
Functions
sysdep.cc File Reference

Implementation of system-dependent interface. More...

#include "copyright.h"
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <sys/file.h>
#include <sys/un.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include "interrupt.h"
#include "system.h"

Functions

int creat (const char *name, unsigned short mode)
 
int open (const char *name, int flags,...)
 
int select (int numBits, void *readFds, void *writeFds, void *exceptFds, struct timeval *timeout)
 
int unlink (char *name)
 
int read (int filedes, char *buf, int numBytes)
 
int write (int filedes, char *buf, int numBytes)
 
int lseek (int filedes, int offset, int whence)
 
int tell (int filedes)
 
int close (int filedes)
 
void srand (unsigned seed)
 
int rand (void)
 
unsigned sleep (unsigned)
 
void abort ()
 
void exit ()
 
int mprotect (char *addr, int len, int prot)
 
int socket (int, int, int)
 
int bind (int, const void *, int)
 
int recvfrom (int, void *, int, int, void *, int *)
 
int sendto (int, const void *, int, int, void *, int)
 
bool PollFile (int fd)
 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. More...
 
int OpenForWrite (const char *name)
 OpenForWrite Open a file for writing. Create it if it doesn't exist; truncate it if it does already exist. More...
 
int OpenForReadWrite (const char *name, bool crashOnError)
 OpenForReadWrite Open a file for reading or writing. More...
 
void Read (int fd, char *buffer, int nBytes)
 Read Read characters from an open file. Abort if read fails.
 
int ReadPartial (int fd, char *buffer, int nBytes)
 ReadPartial Read characters from an open file, returning as many as are available.
 
void WriteFile (int fd, const char *buffer, int nBytes)
 WriteFile Write characters to an open file. Abort if write fails.
 
void Lseek (int fd, int offset, int whence)
 Lseek Change the location within an open file. Abort on error.
 
int Tell (int fd)
 Tell Report the current location within an open file.
 
void Close (int fd)
 Close Close a file. Abort on error.
 
bool Unlink (const char *name)
 Unlink Delete a file.
 
int OpenSocket ()
 OpenSocket Open an interprocess communication (IPC) connection. For now, just open a datagram port where other Nachos (simulating workstations on a network) can send messages to this Nachos.
 
void CloseSocket (int sockID)
 CloseSocket Close the IPC connection.
 
void AssignNameToSocket (const char *socketName, int sockID)
 AssignNameToSocket Give a UNIX file name to the IPC port, so other instances of Nachos can locate the port.
 
void DeAssignNameToSocket (const char *socketName)
 DeAssignNameToSocket Delete the UNIX file name we assigned to our IPC port, on cleanup.
 
bool PollSocket (int sockID)
 PollSocket. More...
 
void ReadFromSocket (int sockID, char *buffer, int packetSize)
 ReadFromSocket Read a fixed size packet off the IPC port. Abort on error.
 
void SendToSocket (int sockID, const char *buffer, int packetSize, const char *toName)
 SendToSocket Transmit a fixed size packet to another Nachos' IPC port. Abort on error.
 
void CallOnUserAbort (VoidNoArgFunctionPtr func)
 CallOnUserAbort Arrange that "func" will be called when the user aborts (e.g., by hitting ctl-C. More...
 
void Delay (int seconds)
 Sleep 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 Abort ()
 Abort Quit and drop core. More...
 
void Exit (int exitCode)
 Exit Quit without dropping core.
 
void RandomInit (unsigned seed)
 RandomInit Initialize the pseudo-random number generator. We use the now obsolete "srand" and "rand" because they are more portable! More...
 
int Random ()
 Random. More...
 
char * AllocBoundedArray (int size)
 AllocBoundedArray. More...
 
void DeallocBoundedArray (char *ptr, int size)
 DeallocBoundedArray Deallocate an array of integers, unprotecting its two boundary pages. More...
 

Detailed Description

Implementation of system-dependent interface.

Author
Olivier Hureau, Hugo Feydel , Julien ALaimo 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-1993 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.

Function Documentation

void Abort ( )

Abort Quit and drop core.

Process control: abort, exit, and sleep.

char* AllocBoundedArray ( int  size)

AllocBoundedArray.

Allocate, de-allocate an array, such that de-referencing just beyond either end of the array will cause an error.

Returns
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!

Parameters
size– amount of useful space needed (in bytes)
void CallOnUserAbort ( VoidNoArgFunctionPtr  func)

CallOnUserAbort Arrange that "func" will be called when the user aborts (e.g., by hitting ctl-C.

Initialize system so that cleanUp routine is called when user hits ctl-C.

void DeallocBoundedArray ( char *  ptr,
int  size 
)

DeallocBoundedArray Deallocate an array of integers, unprotecting its two boundary pages.

Parameters
ptr– the array to be deallocated
size– amount of useful space in the array (in bytes)
int OpenForReadWrite ( const char *  name,
bool  crashOnError 
)

OpenForReadWrite Open a file for reading or writing.

Returns
Return the file descriptor, or error if it doesn't exist.
Parameters
name– file name
int OpenForWrite ( const char *  name)

OpenForWrite Open a file for writing. Create it if it doesn't exist; truncate it if it does already exist.

Returns
Return the file descriptor.
Parameters
name– file name
bool PollFile ( int  fd)

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.

In the network case, if there are no threads for us to run, and no characters to be read, we need to give the other side a chance to get our host's CPU (otherwise, we'll go really slowly, since UNIX time-slices infrequently, and this would be like busy-waiting). So we delay for a short fixed time, before allowing ourselves to be re-scheduled (sort of like a Yield, but cast in terms of UNIX).

Parameters
fd– the file descriptor of the file to be polled
bool PollSocket ( int  sockID)

PollSocket.

Returns
Return TRUE if there are any messages waiting to arrive on the IPC port.
int Random ( )

Random.

Returns
Return a pseudo-random number.
void RandomInit ( unsigned  seed)

RandomInit Initialize the pseudo-random number generator. We use the now obsolete "srand" and "rand" because they are more portable!

Initialize the pseudo random number generator.