示例#1
0
/**
 * Process the buffer
 * Return TRUE to exit program
 */
int process_buf(char **bufargs) {
    struct Command *cmd;
    int argc = 0;

    /*  cd command*/
    if (!strcmp(bufargs[0], "cd") && bufargs[0][2] == '\0') {
        set_cwd(bufargs[1]);
        return FALSE;
    }

    /* exit command */
    if (!strcmp(bufargs[0], "exit") && bufargs[0][4] == '\0') {
        return TRUE;
    }

    /* Get argc */
    while (bufargs[argc++] != ARR_END);

    /* Set up command and first process */
    cmd = init_command();
    cmd->procHead = init_process(argc * sizeof(char *));
    cmd->runningProcs++;

    /* Allocate arguments to their processes within the command */
    if (allocate_args(argc, bufargs, cmd) == TRUE) {
        /* Invalid command */
        free_command(NULL, cmd);
        return FALSE;
    }

    /* Add the command to the global commands */
    add_global(cmd);

    /* For each process in the command, fork it and set it up as running */
    fork_command(cmd);

    /* If foreground command, then wait on processes */
    if (cmd->type == FG) {
        wait_running(cmd);

        /* set the foreground command to null and clean it */
        globalCmd[FG] = NULL;
        free_command(NULL, cmd);
    }

    return FALSE;
}
示例#2
0
文件: ex8.c 项目: ThCP/see
int
main (int argc, char *argv[])
{
    int N;
    int i;
    pthread_t *tids;
    struct args *args_array;

    if (argc != 3)
    {
        fprintf (stderr, "Wrong number of arguments\n");
        printf ("Correct usage: %s <base> <final exponent>\n", argv[0]);
        exit(1);
    }

    N = atoi(argv[2]); // Read the upper limit of the sum

    N = N+1; // so that the sum actually runs from 0 to N instead of from 0 to N-1

    if (N <=0)
    {
        fprintf (stderr, "The upper limit must be positive\n");
        exit(1);
    }

    sscanf(argv[1], "%f", &base);

    results = allocate_results(N);
    args_array = allocate_args(N);
    tids = allocate_tids(N);

    // Assign values to the args of the runner function
    for (i = 0; i < N; i += 1)
    {
        args_array[i].tid = i;
    }

    for (i = 0; i < N; i += 1)
    {
        if (pthread_create(&tids[i], NULL, runner, (void *) &args_array[i]))
        {
            fprintf (stderr, "Error during creation of a thread\n");
            exit(1);
        }
    }

    for (i = 0; i < N; i += 1)
    {
        pthread_join(tids[i], NULL);
    }

    // This is not required
    for (i = 0; i < N; i += 1)
    {
        printf("The result of %g to the %d is %g\n", base, i, results[i]);
        sum+=results[i];
    }

    // Final result
    printf("The sum is %g\n", sum);

    return 0;
}