Exemplo n.º 1
0
void server_serve(struct client *client)
{
	/* unix connection is secure, other insecure */
	run_only_safe_commands = addr.sa.sa_family != AF_UNIX;
	read_commands(client);
	run_only_safe_commands = 0;
}
Exemplo n.º 2
0
int main(int argc, char *argv[])
{
  // Shell read-evaluate loop
  for (;;) {
    // Read user input and evaluate commands
    Command_vec *commands = read_commands(rl_gets());
    eval(commands);
    free_command_vec(commands);
  }
  printf("\n");
  return 0;
}
Exemplo n.º 3
0
int main_menu(SDL_Renderer *renderer) {
  int should_quit = 0;
  Code code = CODE_OK;
  Menu menu;
  CommandTable command_table;
  char title[MAXIMUM_STRING_SIZE];
  char *options[] = {"Play", "Top Scores", "Info", "Quit"};
  sprintf(title, "%s %s", "Walls of Doom", WALLS_OF_DOOM_VERSION);
  menu.title = title;
  menu.options = options;
  menu.option_count = 4;
  menu.selected_option = 0;
  initialize_command_table(&command_table);
  while (!should_quit) {
    write_menu(&menu, renderer);
    read_commands(&command_table);
    if (test_command_table(&command_table, COMMAND_UP, REPETITION_DELAY)) {
      if (menu.selected_option > 0) {
        menu.selected_option--;
      } else {
        menu.selected_option = menu.option_count - 1;
      }
    } else if (test_command_table(&command_table, COMMAND_DOWN, REPETITION_DELAY)) {
      if (menu.selected_option + 1 < menu.option_count) {
        menu.selected_option++;
      } else {
        menu.selected_option = 0;
      }
    } else if (test_command_table(&command_table, COMMAND_ENTER, REPETITION_DELAY) ||
               test_command_table(&command_table, COMMAND_CENTER, REPETITION_DELAY)) {
      if (menu.selected_option == 0) {
        code = game(renderer, &command_table);
      } else if (menu.selected_option == 1) {
        code = top_scores(renderer, &command_table);
      } else if (menu.selected_option == 2) {
        code = info(renderer, &command_table);
      } else if (menu.selected_option == 3) {
        should_quit = 1;
      }
      /* If it is not defined whether or not we should quit, check the code. */
      if (should_quit == 0) {
        should_quit = is_termination_code(code);
      }
    }
    /* Quit if the user selected the Quit option or closed the window. */
    should_quit = should_quit || test_command_table(&command_table, COMMAND_QUIT, REPETITION_DELAY);
  }
  return 0;
}
Exemplo n.º 4
0
int	main(int ac, char **av)
{
  t_platine	*platine_a;
  t_platine	*platine_b;
  t_command	*commands;

  if (ac < 3)
  {
    printf("Usage: ./%s platineA platineB < commandes\n", av[0]);
    return (1);
  }
  platine_a = create_platine(av[1]);
  platine_b = create_platine(av[2]);
  commands = read_commands();

  if (!platine_a || !platine_b || !commands)
  {
    printf("Error while getting platine or commands\n");
    return (1);
  }
  run_dj(platine_a, platine_b, commands);
  return (0);
}
Exemplo n.º 5
0
Arquivo: packet.c Projeto: russor/mtr
int main(
    int argc,
    char **argv)
{
    bool command_pipe_open;
    struct command_buffer_t command_buffer;
    struct net_state_t net_state;

    /*
       To minimize security risk, the only thing done prior to 
       dropping SUID should be opening the network state for
       raw sockets.
     */
    init_net_state_privileged(&net_state);
    if (drop_elevated_permissions()) {
        perror("Unable to drop elevated permissions");
        exit(EXIT_FAILURE);
    }
    init_net_state(&net_state);

    init_command_buffer(&command_buffer, fileno(stdin));

    command_pipe_open = true;

    /*
       Dispatch commands and respond to probe replies until the
       command stream is closed.
     */
    while (true) {
        /*  Ensure any responses are written before waiting  */
        fflush(stdout);
        wait_for_activity(&command_buffer, &net_state);

        /*
           Receive replies first so that the timestamps are as
           close to the response arrival time as possible.
         */
        receive_replies(&net_state);

        if (command_pipe_open) {
            if (read_commands(&command_buffer)) {
                if (errno == EPIPE) {
                    command_pipe_open = false;
                }
            }
        }

        check_probe_timeouts(&net_state);

        /*
           Dispatch commands late so that the window between probe
           departure and arriving replies is as small as possible.
         */
        dispatch_buffer_commands(&command_buffer, &net_state);

        /*
           If the command pipe has been closed, exit after all
           in-flight probes have reported their status.
         */
        if (!command_pipe_open) {
            if (net_state.outstanding_probe_count == 0) {
                break;
            }
        }
    }

    return 0;
}
// Main function
int main (int argc, char *argv[])
{
    char *input = "input.txt"; // File containing the commands

    // Initialize Process Table
    struct process process_table[25];

    // Initialize CPU 1
    struct hardware cpu1;
    cpu1.busy = 0;

    // Initialize CPU 2
    struct hardware cpu2;
    cpu2.busy = 0;

    // Initialize SSD
    struct hardware ssd;
    ssd.busy = 0;

    // Initialize IO priority queue
    struct queue io_q;
    io_q.nextEmpty = 0;
    io_q.busy = 0;

    // Setting NULL values
    int x;

    for(x = 0; x < 26; x++) {
        io_q.pid[x] = NULL_VALUE;
    }

    // Initialize Ready queue.
    struct queue ready_q;
    ready_q.nextEmpty = 0;
    ready_q.busy = 0;

    // Setting NULL values
    for(x = 0; x < 26; x++) {
        ready_q.pid[x] = NULL_VALUE;
    }

    // Initialize SSD queue.
    struct queue ssd_q;
    ssd_q.nextEmpty = 0;
    ssd_q.busy = 0;

    // Setting NULL values
    for(x = 0; x < 26; x++) {
        ssd_q.pid[x] = NULL_VALUE;
    }


    // Read the file with the commands.
    read_commands(input, process_table);

    // Testing code to check if the process were created correctly
    int i, j;

    for(i=0;i<process_ctr;i++) {

        printf("PID %d\n", process_table[i].pid);
        printf("STATE %d\n", process_table[i].state);
        printf("COMMAND_INDEX %d\n", process_table[i].command_index);
        printf("TOTAL_COMMANDS %d\n", process_table[i].total_commands);
        printf("SSD_ACCESS_COUNTER %d\n", process_table[i].ssd_access_counter);
        printf("SSD_USAGE_TIME %d\n", process_table[i].ssd_usage_time);
        printf("SSD_WAIT_TIME %d\n", process_table[i].ssd_wait_time);
        printf("SSD_ENTRY_TIME %d\n", process_table[i].ssd_entry_time);

        for(j=0;j<process_table[i].command_index;j++) {
            printf("\tNAME %d", process_table[i].commands[j].name);
            printf("\tTIME %d\n", process_table[i].commands[j].time);
        }

        printf("\n");
    }

    // Setting the total commands and reseting the command_index of each process to 0;
    for(i=0;i<process_ctr;i++) {
        process_table[i].total_commands = process_table[i].command_index;
        process_table[i].command_index = 0;
    }


    printf("Current Time: %d, Total Processes in Table: %d\n", global_time,process_ctr);
    for(i = 0; i < process_ctr; i++) {
        printf("PROCESS %d: ", process_table[i].pid);
        if (process_table[i].state == READY)
        {
           printf("Current State = READY");
        }
        else if (process_table[i].state == RUNNING)
        {
           printf("Current State = RUNNING");
        }
        else if (process_table[i].state == WAITING)
        {
           printf("Current State = WAITING");
        }
        else if (process_table[i].state == FINISHED)
        {
           printf("Current State = FINISHED");
        }
        printf(" Next Index = %d\n", process_table[i].command_index);
    }

    printf("\n\n\n");

    while(finished_processes < process_ctr){
        /////////////// Check minimum end time ///////////////
        int minFinishTime = 9999999;
        // Check cpu1
        if(cpu1.busy) {
            if(cpu1.finish_time < minFinishTime) {
                minFinishTime = cpu1.finish_time;
            }
        }

        // Check cpu2
        if(cpu2.busy) {
            if(cpu2.finish_time < minFinishTime) {
                minFinishTime = cpu2.finish_time;
            }
        }

        // Check ssd
        if(ssd.busy) {
            if(ssd.finish_time < minFinishTime) {
                minFinishTime = ssd.finish_time;
            }
        }

        // Check INP queue
        if(io_q.busy) {
            if(process_table[io_q.pid[0]].commands[process_table[io_q.pid[0]].command_index].time + process_table[io_q.pid[0]].io_entry_time < minFinishTime) {
                minFinishTime = process_table[io_q.pid[0]].commands[process_table[io_q.pid[0]].command_index].time + process_table[io_q.pid[0]].io_entry_time;
            }
        }

        // Check if create a new process
        if(next_new_process < process_ctr) {
            if(process_table[next_new_process].commands[0].time < minFinishTime) {
                minFinishTime = process_table[next_new_process].commands[0].time;
            }
        }

        // Updating global time
        global_time = minFinishTime;



        /////////////// Free up hardware, io queue, and new process ///////////////

        // Free cpu1
        if(cpu1.busy) {
            if(cpu1.finish_time == minFinishTime) {
                execute_next_command(get_process_from_hardware(&cpu1, process_table), process_table, &io_q, &ssd_q, &ready_q);
            }
        }

        // Free cpu2
        if(cpu2.busy) {
            if(cpu2.finish_time == minFinishTime) {
                execute_next_command(get_process_from_hardware(&cpu2, process_table), process_table, &io_q, &ssd_q, &ready_q);
            }
        }

        // Free ssd
        if(ssd.busy) {
            if(ssd.finish_time == minFinishTime) {
                execute_next_command(get_process_from_hardware(&ssd, process_table), process_table, &io_q, &ssd_q, &ready_q);
            }
        }

        // Dequeue from INP queue
        if(io_q.busy) {
            while(io_q.busy && (process_table[io_q.pid[0]].commands[process_table[io_q.pid[0]].command_index].time + process_table[io_q.pid[0]].io_entry_time == minFinishTime) ) {
                execute_next_command(dequeue(&io_q), process_table, &io_q, &ssd_q, &ready_q);
            }
        }

        // Creating a new process
        if(next_new_process < process_ctr) {
            while(process_table[next_new_process].commands[0].time == minFinishTime) {
                execute_next_command(next_new_process, process_table, &io_q, &ssd_q, &ready_q);
                next_new_process = next_new_process + 1;
            }
        }




        /////////////// Put process from queue to hardware ///////////////

        // Check if cpu1 is busy
        if(cpu1.busy == 0) {
            // Check if ready q has something
            if(ready_q.busy) {

                // Add to cpu1
                cpu1.pid = dequeue(&ready_q);
                cpu1.busy = 1;
                cpu1.finish_time = global_time + process_table[cpu1.pid].commands[process_table[cpu1.pid].command_index].time;
                process_table[cpu1.pid].state = RUNNING;
                process_table[cpu1.pid].cpu_usage_time = process_table[cpu1.pid].cpu_usage_time + process_table[cpu1.pid].commands[process_table[cpu1.pid].command_index].time;


                // Increment command index
            }

        }

        // Check if cpu2 is busy
        if(cpu2.busy == 0) {
            // Check if ready q has something
             if(ready_q.busy) {

                // Add to cpu2
                cpu2.pid = dequeue(&ready_q);
                cpu2.busy = 1;
                cpu2.finish_time = global_time + process_table[cpu2.pid].commands[process_table[cpu2.pid].command_index].time;
                process_table[cpu2.pid].state = RUNNING;
                process_table[cpu2.pid].cpu_usage_time = process_table[cpu2.pid].cpu_usage_time + process_table[cpu2.pid].commands[process_table[cpu2.pid].command_index].time;
                // Increment command index
            }

        }

        // Check if ssd is busy
        if(ssd.busy == 0) {
            // Check if ssd q has something
            if(ssd_q.busy) {

                // Add to cpu2
                ssd.pid = dequeue(&ssd_q);
                ssd.busy = 1;
                ssd.finish_time = global_time + process_table[ssd.pid].commands[process_table[ssd.pid].command_index].time;
                process_table[ssd.pid].state = WAITING;
                process_table[ssd.pid].ssd_wait_time = (process_table[ssd.pid].ssd_wait_time + global_time) - process_table[ssd.pid].ssd_entry_time;
                process_table[ssd.pid].ssd_access_counter++;
                process_table[ssd.pid].ssd_usage_time = process_table[ssd.pid].ssd_usage_time + process_table[ssd.pid].commands[process_table[ssd.pid].command_index].time;
                // Increment command index
            }

        }

        printf("Current Time: %d, Total Processes in Table: %d\n", global_time,process_ctr);
        for(i = 0; i < process_ctr; i++) {
                printf("PROCESS %d: ", process_table[i].pid);
        if (process_table[i].state == READY)
        {
           printf("Current State = READY");
        }
        else if (process_table[i].state == RUNNING)
        {
           printf("Current State = RUNNING");
        }
        else if (process_table[i].state == WAITING)
        {
           printf("Current State = WAITING");
        }
        else if (process_table[i].state == FINISHED)
        {
           printf("Current State = FINISHED");
        }

        if (process_table[i].state == FINISHED) {
            printf(" Next Index = %d\n", process_table[i].command_index);
        }
        else {
            printf(" Next Index = %d\n", process_table[i].command_index + 1);
        }
    }

        printf("\n\n\n");

    }
    printf("Summary: \n");
    printf("Total Processes Completed: %d \n", finished_processes);

    int ssdAccesses = 0;
    int ssdWaitTime = 0;
    int ssdUsageTime = 0;
    int cpuUsageTime = 0;

    for(i=0; i < finished_processes; i++)
    {
        ssdAccesses = ssdAccesses + process_table[i].ssd_access_counter;
        ssdWaitTime =  ssdWaitTime + process_table[i].ssd_usage_time + process_table[i].ssd_wait_time;
        ssdUsageTime =  ssdUsageTime + process_table[i].ssd_usage_time;
        cpuUsageTime =  cpuUsageTime + process_table[i].cpu_usage_time;

    }

    printf("Total Number of SSD Accesses: %d\n", ssdAccesses);

    if(ssdAccesses != 0) {
        printf("Average SSD Access Duration: %f ms\n", ((double)ssdWaitTime/(double)ssdAccesses));
    }
    else {
        printf("Average SSD Access Duration: 0 ms\n");
    }
    printf("Total Elapsed Time %d ms\n", global_time-process_table[0].commands[0].time);
    printf("CPU Utilization %f\n", ( (double) cpuUsageTime / ((double) global_time-process_table[0].commands[0].time)));
    printf("SSD Utilization %f\n", ((double)ssdUsageTime/((double)global_time-process_table[0].commands[0].time)));


    // Finish program
    return 0;
};
int main(int argc, char **argv)
{
  FILE *head_fp;
  Ilist just;
  Plist demodulators, p;
  Mindex idx;
  Term t;
  int rewritten = 0;
  BOOL verbose = string_member("verbose", argv, argc);;

  if (string_member("help", argv, argc) ||
      string_member("-help", argv, argc) ||
      argc < 2) {
    printf("\n%s, version %s, %s\n",PROGRAM_NAME,PROGRAM_VERSION,PROGRAM_DATE);
    printf("%s", Help_string);
    exit(1);
  }

  init_standard_ladr();

  head_fp = fopen(argv[1], "r");
  if (head_fp == NULL)
    fatal_error("demodulator file can't be opened for reading");

  t = read_commands(head_fp, stderr, verbose, KILL_UNKNOWN);

  if (!is_term(t, "clauses", 1) && !is_term(t, "formulas", 1))
    fatal_error("formulas(demodulators) not found");

  /* Read list of demodulators. */

  demodulators = read_clause_list(head_fp, stderr, TRUE);

  fclose(head_fp);

  /* AC-canonicalize and index the demodulators. */

  if (assoc_comm_symbols() || comm_symbols())
    idx = mindex_init(DISCRIM_WILD, BACKTRACK_UNIF, 0);
  else
    idx = mindex_init(DISCRIM_BIND, ORDINARY_UNIF, 0);

  for (p = demodulators; p != NULL; p = p->next) {
    /* assume positive equality unit */
    Topform d = p->v;
    Literals lit = d->literals;
    Term alpha = lit->atom->args[0];
    mark_oriented_eq(lit->atom);  /* don not check for termination */
    if (assoc_comm_symbols())
      ac_canonical(lit->atom, -1);
    mindex_update(idx, alpha, INSERT);
  }

  if (verbose)
    fwrite_clause_list(stdout, demodulators, "demodulators", CL_FORM_BARE);

  /* Read and demodulate terms. */

  t = read_term(stdin, stderr);

  while (t != NULL) {
    rewritten++;
    if (verbose) {
      fprintf(stdout, "\nBefore:   "); fwrite_term_nl(stdout, t);
    }

    if (assoc_comm_symbols())
      ac_canonical(t, -1);
    just = NULL;
    t = demodulate(t, idx, &just, FALSE);

    if (verbose)
      fprintf(stdout, "After:    ");

    fwrite_term_nl(stdout, t);
    fflush(stdout);

    zap_ilist(just);
    zap_term(t);
    t = read_term(stdin, stderr);
  }

  printf("%% %s %s: rewrote %d terms with %d rewrite steps in %.2f seconds.\n",
	 PROGRAM_NAME, argv[1], rewritten, demod_rewrites(), user_seconds());
    
  exit(0);

}  /* main */