/*-----------------------------------------------------------------*/
int main(int argc, char* argv[]) {
    int  n;
    char g_i;
    int* a;

    Get_args(argc, argv, &n, &g_i);
    a = (int*) malloc(n*sizeof(int));
    if (g_i == 'g') {
        Generate_list(a, n);
        Print_list(a, n, "Before sort");
    } else {
        Read_list(a, n);
    }

    Odd_even_sort(a, n);

    Print_list(a, n, "After sort");

    free(a);
    return 0;
}  /* main */
示例#2
0
int main (int argc, char *argv[])
{
    int i, j, rc;
    pthread_attr_t attr;

    assert(argc == 3);
    student_num = atoi(argv[1]);
    sheet_num = atoi(argv[2]);
    assert(student_num > 0);
    assert(sheet_num > 0);

    srand(time(NULL));

    cqueue_init(&cqueue, sizeof(sheet_array), sheet_num);

    // Initialize and set thread detached attribute
    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
    pthread_rwlock_init(&rwlock, NULL);

    // prepare sheet_array
    sheet_array = malloc(sizeof(sheet_array[0])*sheet_num);
    assert(sheet_array);
    for(i = 0; i < sheet_num; i++)
    {
        sheet_array[i].array = malloc(sizeof(sheet_array[0].array[0])*student_num);
        assert(sheet_array[i].array);
        memset(sheet_array[i].array, 0, sizeof(sheet_array[0].array[0])*student_num);
        pthread_mutex_init(&(sheet_array[i].mutex), NULL);
        signupsheet_t* psheet = sheet_array + i;
        cqueue_push_back(&cqueue, &psheet);
    }

    // prepare final_sheet
    final_sheet.array = malloc(sizeof(int)*student_num);
    memset(final_sheet.array, 0, sizeof(final_sheet.array[0])*student_num);
    pthread_mutex_init(&(final_sheet.mutex), NULL);

    // prepare the golden data for check()
    golden = malloc(sizeof(int)*student_num);
    assert(golden);

    pthread_barrier_init(&bar, NULL, student_num);

    // initial semaphore attribute
    sem_init(&sem, 0, sheet_num);

    // initial thread creation
    thread_array = (pthread_t*) malloc(sizeof(pthread_t) * student_num);
    assert(thread_array);
    for(i = 0; i < student_num; i++)
    {
        int student_id = 0;
        while(student_id == 0)
            student_id = rand();
        assert(student_id != 0);

        int rc = pthread_create(thread_array+i, &attr, student, (void *)student_id);
        assert(rc == 0);
        golden[i] = student_id;
    }

    for(i = 0; i < student_num; i++)
        pthread_join(thread_array[i], NULL);

    // sort all student autograph
    int signup_count = 0;
    for(i = 0; i < sheet_num; i++)
        for(j = 0; j < student_num; j++)
            if(sheet_array[i].array[j])
                final_sheet.array[signup_count++] = sheet_array[i].array[j];

    printf("signup_count:%d\n", signup_count);
    assert(signup_count == student_num);

    Odd_even_sort(final_sheet.array, student_num);
    //Print_list(final_sheet.array, student_num,
    //"Student autograph Booki (Sorted)");

    check_result();

    return 0;
}