Esempio n. 1
0
static void
syscall_handler (struct intr_frame *f) 
{
  uint32_t *esp = f->esp;
  if (not_valid(esp))
    exit (-1);
  switch (*esp)
    {
      case SYS_HALT:
        halt ();
        break;
      case SYS_EXIT:
        if (not_valid(esp+1)) exit(-1);
        exit ((int) ARG0);
        break;
      case SYS_EXEC:
        f->eax = exec ((const char *) ARG0);
        break;
      case SYS_WAIT:
        f->eax = wait ((pid_t) ARG0);
        break;
      case SYS_CREATE:
        f->eax = create ((const char *) ARG0, (unsigned) ARG1);
        break;
      case SYS_REMOVE:
        f->eax = remove ((const char *) ARG0);
        break;
      case SYS_OPEN:
        f->eax = open ((const char *) ARG0);
        break;
      case SYS_FILESIZE:
        f->eax = filesize ((int) ARG0);
        break;
      case SYS_READ:
        f->eax = read ((int) ARG0, (void *) ARG1, (unsigned) ARG2);
        break;
      case SYS_WRITE:
        f->eax = write ((int) ARG0, (void *) ARG1, (unsigned) ARG2);
        break;
      case SYS_SEEK:
        seek ((int) ARG0, (unsigned) ARG1);
        break;
      case SYS_TELL:
        f->eax = tell ((int) ARG0);
        break;
      case SYS_CLOSE:
        close ((int) ARG0);
        break;
      default:
        printf ("Invalid syscall!\n");
        thread_exit();
    }
}
Esempio n. 2
0
int main()
{
	char inp[35];
	fgets(inp,33,stdin);
	int l;
	int i;
	if((l=strlen(inp)) != strlen(f)) {
		return not_valid();
	}

	for(i=0;i<l;i++) {
		if(inp[i] != f[l-1-i])
			return not_valid();
	}
	return valid();
}
Esempio n. 3
0
bool
create(const char *file, unsigned initial_size)
{
  if (not_valid(file))
    exit (-1);

  lock_acquire(&file_lock);
  bool result = filesys_create (file, initial_size);
  lock_release(&file_lock);
  return result;
}
Esempio n. 4
0
int 
write (int fd, const void *buffer, unsigned size)
{
  if (not_valid(buffer) || not_valid(buffer+size) || fd == STDIN_FILENO)
    exit (-1);
  lock_acquire(&file_lock);
  int result = 0;
  if (fd == STDOUT_FILENO)
    {
      putbuf (buffer, size);
      result = size;
    }
  else 
    {
      struct file *file = get_file(fd);
      result = file? file_write(file, buffer, size) : -1;
    }
  lock_release(&file_lock); 
  return result;
}
Esempio n. 5
0
int g3_fastMove(const struct connect4 *game, int *movesToExplore) {
    const char me = game->whoseTurn;

    struct connect4 tempGame;

    // Clear space for wins
    int wins[NUM_COLS];
    memset(wins, 0, sizeof(wins));

    int i, count;
    for (i = 0; i < NUM_COLS; i++) {
        if (movesToExplore[i] == 0)
            continue;

        // Run a bunch of sims for this column
        for (count = 0; count < g3_FAST_SIMULATIONS; count++) {
            memcpy(&tempGame, game, sizeof(struct connect4));

            // Play a move in the ith column
            tempGame.board[get_row(&tempGame, i)][i] = tempGame.whoseTurn;
            tempGame.whoseTurn = other(tempGame.whoseTurn);

            char winner = 0;
            while (winner == 0 && g3_movesAvailable(&tempGame)) {
                int nextMove = g3_has3Wins(&tempGame);
                // Try again if move is invalid
                while (not_valid(&tempGame, nextMove))
                    nextMove = rand() % NUM_COLS; // Try again

                // If the move would result in a winner or the game is CATS, break the loop
                int validRow = get_row(&tempGame, nextMove);
                if (g3_is3Win(&tempGame, validRow, nextMove, tempGame.whoseTurn)) {
                    winner = tempGame.whoseTurn;
                } else {
                    // Otherwise, play the move and keep playing
                    move(&tempGame, nextMove, tempGame.whoseTurn);
                    tempGame.whoseTurn = other(tempGame.whoseTurn);
                }
            }

            wins[i] += (winner == me);
        }
    }

    int greatest = -1, greatestMove = -1;
    for (i = 0; i < NUM_COLS; i++) {
        if (wins[i] > greatest) {
            greatest = wins[i];
            greatestMove = i;
        }
    }

    return greatestMove;
}
Esempio n. 6
0
int 
read (int fd, void *buffer, unsigned size)
{
  if (not_valid(buffer) || not_valid(buffer+size) || fd == STDOUT_FILENO)
    exit (-1);
  lock_acquire(&file_lock);
  int count = 0, result = 0;
  if (fd == STDIN_FILENO)
    {
      while (count < size)
        {
          *((uint8_t *) (buffer + count)) = input_getc ();
          count++;
        }
      result = size;
    }
  else 
    {
      struct file *file = get_file(fd);
      result = file ? file_read(file, buffer, size) : -1;
    }
  lock_release(&file_lock);
  return result;
}
Esempio n. 7
0
bool
remove (const char *file)
{
  if (not_valid(file))
    exit (-1);

  lock_acquire(&file_lock);
  /* In case the file is opened. First check its existence. */
  struct file *f = filesys_open (file);
  bool result;
  if (f == NULL)
    result = false;
  else
    {
      file_close (f);
      result = filesys_remove (file);
    }
  lock_release(&file_lock);
  return result;
}
Esempio n. 8
0
// Convert dheluks data package to encoded string
// has_msg = false: package contains bare minimum (header + pubkey)
// has_msg = true: package contains a message (header + pubkey + nonce + cphtxt)
int pkg_to_str(dheluks_pkg_t *pkg, unsigned char *str, bool has_msg) {

	size_t datalen; 	//length of package data
	size_t max_datalen; //max length of pkg data
	uint8_t *data;		//stores data
	size_t codelen;		//length of encoded data
	unsigned char *code;//stores code

	datalen = KEY_SIZE; //min length
	max_datalen = KEY_SIZE + NONCE_SIZE + DIGEST_SIZE + pkg->csize; //max length
	data = malloc(max_datalen); //allocate max length

	memcpy(data, pkg->pubkey, KEY_SIZE); //put the public key in first

	if (has_msg == true) { //if there is supposed to be a message

		if(not_valid(pkg->cphtxt)) { //but there actually isn't one
			err(ERR_NUL, "has_msg is true, but there is no ciphertext.");
			return -1;	//raise an error
		} //else,

		datalen += (NONCE_SIZE + DIGEST_SIZE + pkg->csize); //increase datalen for nonce/cphtxt
		memcpy(data + KEY_SIZE, pkg->nonce, NONCE_SIZE); //add nonce to data
		memcpy(data + KEY_SIZE + NONCE_SIZE, pkg->digest, DIGEST_SIZE);
		memcpy(data + KEY_SIZE + NONCE_SIZE + DIGEST_SIZE, pkg->cphtxt, pkg->csize); //add ctxt

	}

	codelen = B64_ENCODE_LEN(datalen); //size of encoded data
	code = malloc(codelen);	//allocate memory for code
	uint8_to_str(data, code, datalen); //convert data to code

	memcpy(str, HEADER, HEADER_LEN); //place the header
	memcpy(str+HEADER_LEN, code, codelen); //place the encoded data

	free(data); //clean up

	return 0; //there were no errors

}
Esempio n. 9
0
int 
open (const char *file)
{
  if (not_valid(file))
    exit (-1);

  lock_acquire(&file_lock);
  struct file_descriptor *file_d = malloc(sizeof(struct file_descriptor));
  struct file *f = filesys_open(file);
  struct thread *cur = thread_current();
  if (f == NULL)
    {
      lock_release(&file_lock);
      return -1;
    }
  file_d->file = f;
  file_d->fd = cur->fd;
  cur->fd = cur->fd + 1;
  list_push_back(&thread_current()->files,&file_d->elem);
  lock_release(&file_lock);
  return file_d->fd;
}
Esempio n. 10
0
// Frees a pointer whose value is not null
void free_mem(void *ptr) {

	if(not_valid(ptr) == false) //if ptr is not null,
		free(ptr);				//free it

}
Esempio n. 11
0
pid_t exec (const char *cmd_line)
{
  if (not_valid(cmd_line))
    exit (-1);
  return process_execute(cmd_line); 
}
Esempio n. 12
0
int g3_handleSpecialCase(const struct connect4 *game) {
    // Play the center if there are no other moves
    if (!not_valid(game, g3_CENTER)) {
        //isCrazy checks if there are moves in other columns;
        if (!g3_isCrazy(game)) return g3_CENTER;
    }

    //Continue to checking for 3wins and 2wins if you shouldn't play the center

    int col;
    int didTheyWin = -1;

    //determines who is who for each of the pieces based on whose turn it is
    char us = game->whoseTurn;

    //they have to be the opposite of our piece, so set them to the opposite of us
    char them = other(us);

    //Check all of the columns for if we win by playing in the column or if we block their win
    for (col = 0; col < NUM_COLS; col++) {
        //finds the row you will be playing in if you select that column
        int validRow = get_row(game, col);
        //If you can win there, play there!
        if (g3_is3Win(game, validRow, col, us)) {
            return col;
        }
        //Otherwise, keep track of where to block the opponent if they have a 3win
        if (g3_is3Win(game, validRow, col, them)) didTheyWin = col;
    }

    //If you found a column you have to block, block it.
    if (didTheyWin != -1) return didTheyWin;

    //If there are no 3wins, check for 2wins

    int enemy2Wins[NUM_COLS];
    memset(enemy2Wins, 0, sizeof(enemy2Wins));

    //Check in each col if there is a 2win
    for (col = 0; col < NUM_COLS; col++) {
        int validRow = get_row(game, col);

        //if you have a 2win and you don't cause a 3win for the opponent, you should play there
        if (g3_is2Win(game, validRow, col, us)) {
            if (g3_isSafe(game, validRow, col, us)) {
                return col;
            }
        }
/*
		//If your opponent has a 2win and its safe to block them, do it
		if (g3_is2Win(game, validRow, col, them) && g3_isSafe(game, validRow, col, us)) {
			enemy2Wins[col] = 1;
		}
*/
    }

/*
    for (col = 0; col < NUM_COLS; col++) {
        if (enemy2Wins[col] ==  1)
            return g3_fastMove(game, enemy2Wins);
    }
*/
    return -1;
}
Esempio n. 13
0
// Perform a Monte-Carlo Tree Search
void g3_mcts(const struct connect4 *game, g3_MCnode *root) {
    // For concision
    const char me = game->whoseTurn;
    //const int winCondition = me == PLAYERONE ? X_WINS : O_WINS;

    // Memory to do scratch-work in
    struct connect4 tempGame;

    g3_MCnode *nodeStack[g3_NUM_MOVES];
    int moveStack[g3_NUM_MOVES];
    int s = 0; // Stack pointer

    static int runs = 0;
    int numSims = g3_max(g3_SIMULATIONS / 26 * (26 - runs), g3_FAST_SIMULATIONS);
    runs++;

    //puts("g");
    //printf("Running %d simulations\n", numSims);

    int t; // current number of simulations
    for (t = 0; t < numSims; t++) {
        // Make a fresh copy of the game
        memcpy(&tempGame, game, sizeof(struct connect4));

        // Temp pointer to the current node in the tree
        g3_MCnode *current = root;

        // Reset stack pointer
        s = 0;

        // Play moves until the hypothetical game ends
        char winner = 0;
        while (winner == 0 && g3_movesAvailable(&tempGame)) {
            if (s >= 26)
                puts("Something's gone horribly wrong.");

            double probabilities[NUM_COLS];
            //const char currPlayer = tempGame.whoseTurn;

            // Assume both players will go for special cases
            int nextMove = g3_handleSpecialCase(&tempGame);
            if (nextMove == -1) {
                // Weight current player's path based on past success
                if (tempGame.whoseTurn == me)
                    g3_computeWeightedProbs(probabilities, current->scores, t);
                    // If there was no immediate win, give all opponent paths an equal chance
                else
                    g3_computeUniformProbs(probabilities);

                // Use probabilities to select the next node
                while (not_valid(&tempGame, nextMove))
                    nextMove = g3_chooseMove(probabilities);
            }

            // Push the move onto the stack if this is our move
            if (tempGame.whoseTurn == me) {
                //printf("Stack pointer at %d\n", s);
                //print_board(&tempGame);
                moveStack[s] = nextMove;
                nodeStack[s++] = current;
            }

            // If the move would result in a winner or the game is CATS, break the loop
            int validRow = get_row(&tempGame, nextMove);
            if (g3_is3Win(&tempGame, validRow, nextMove, tempGame.whoseTurn)) {
                winner = tempGame.whoseTurn;
            } else {
                // Otherwise, play the move and keep playing
                tempGame.board[get_row(&tempGame, nextMove)][nextMove] = tempGame.whoseTurn;
                tempGame.whoseTurn = other(tempGame.whoseTurn);
                current = g3_getNextNode(current, nextMove);
            }
        }

        // Determine if we won
        int w = (winner == me);
        // Backprop the result of the game
        g3_backpropogate(nodeStack, moveStack, s - 1, w);
    }

    return;
}