Пример #1
0
/*
 * Print a polynomial p
 * - prefix =  variable prefix name ('x' or 'i')
 */
static void dsolver_print_poly(FILE *f, polynomial_t *p, char prefix) {
  uint32_t i, n;
  int32_t x;

  n = p->nterms;
  if (n == 0) {
    fprintf(f, "0");

  } else if (n == 1) {
    x = p->mono[0].var;
    if (x == const_idx) {
      q_print(f, &p->mono[0].coeff);
    } else {
      dsolver_print_monomial(f, &p->mono[0].coeff, p->mono[0].var, prefix);
    }
  } else {

    fprintf(f, "(+");
    i = 0;
    x = p->mono[i].var;
    if (x == const_idx) {
      fprintf(f, " ");
      q_print(f, &p->mono[i].coeff);
      i ++;
    }
    while (i < n) {
      fprintf(f, " ");
      dsolver_print_monomial(f, &p->mono[i].coeff, p->mono[i].var, prefix);
      i ++;
    }
    fprintf(f, ")");
  }
}
/*
 * Check whether cnstr => var[k] = period * integer + phase
 */
static void check_period_and_phase(int_constraint_t *cnstr, uint32_t k, rational_t *period, rational_t *phase) {
  rational_t test_val;
  int32_t x, z;  

  q_init(&test_val);

  x = int_constraint_get_var(cnstr, k);

  for (z = -10; z < 10; z++) {
    get_solution_for_var(cnstr, k, &test_val, z);
    q_sub(&test_val, phase);  // value - phase 
    if (q_divides(period, &test_val)) {
      printf("  passed test for %s = ", var[x].name);
      q_print(stdout, &test_val);
      printf("\n");
    } else {
      printf("*** BUG ***");
      printf("  failed test for %s = ", var[x].name);
      q_print(stdout, &test_val);
      printf("\n");
      fflush(stdout);
      exit(1);
    }
  }

  q_clear(&test_val);
}
Пример #3
0
/*
 * Test 3: a is small, b is large
 */
static void test_small_big(void) {
  rational_t a, b, c;
  uint32_t i;
  int32_t x;

  q_init(&a);
  q_init(&b);
  q_init(&c);

  for (i=0; i<NBIGS; i++) {
    q_set_from_string(&b, large_divisor[i]);
    for (x=-10; x<=10; x++) {
      q_set32(&a, x);
      q_set32(&c, x);
      q_integer_div(&a, &b);
      q_integer_rem(&c, &b);

      printf("div[%"PRId32", %s]: quotient = ", x, large_divisor[i]);
      q_print(stdout, &a);
      printf(", remainder = ");
      q_print(stdout, &c);
      printf("\n");
    }
    printf("\n");
  }

  q_clear(&a);
  q_clear(&b);
  q_clear(&c);
}
Пример #4
0
/*
 * Test 2: a is large, b is small
 */
static void test_big_small(void) {
  rational_t a, b, c;
  uint32_t i;
  int32_t y;

  q_init(&a);
  q_init(&b);
  q_init(&c);

  for (y=1; y<8; y++) {
    q_set32(&b, y);
    for (i=0; i<NBIGS; i++) {
      q_set_from_string(&a, large_signed[i]);
      q_set(&c, &a);
      q_integer_div(&a, &b);
      q_integer_rem(&c, &b);

      printf("div[%s, %"PRId32"]: quotient = ", large_signed[i], y);
      q_print(stdout, &a);
      printf(", remainder = ");
      q_print(stdout, &c);
      printf("\n");
    }
    printf("\n");
  }

  q_clear(&a);
  q_clear(&b);
  q_clear(&c);
}
Пример #5
0
/*
 * Test 1: a divided by b
 * - both a and b are small integers
 */
static void test_small_small(void) {
  rational_t a, b, c;
  int32_t x, y;

  q_init(&a);
  q_init(&b);
  q_init(&c);

  for (y=1; y<8; y++) {
    q_set32(&b, y);
    for (x=-20; x<=20; x++) {
      q_set32(&a, x);
      q_integer_div(&a, &b);
      q_set32(&c, x);
      q_integer_rem(&c, &b);

      printf("div[%3"PRId32", %"PRId32"]: quotient = ", x, y);
      q_print(stdout, &a);
      printf(", remainder = ");
      q_print(stdout, &c);
      printf("\n");
    }
    printf("\n");
  }

  q_clear(&a);
  q_clear(&b);
  q_clear(&c);
}
Пример #6
0
/*
 * Test 4: a is large, b is large
 */
static void test_big_big(void) {
  rational_t a, b, c;
  uint32_t i, j;

  q_init(&a);
  q_init(&b);
  q_init(&c);

  for (i=0; i<NBIGS; i++) {
    q_set_from_string(&b, large_divisor[i]);
    for (j=0; j<NBIGS; j++) {
      q_set_from_string(&a, large_signed[j]);
      q_set(&c, &a);
      q_integer_div(&a, &b);
      q_integer_rem(&c, &b);

      printf("div[%s, %s]: quotient = ", large_signed[j], large_divisor[i]);
      q_print(stdout, &a);
      printf(", remainder = ");
      q_print(stdout, &c);
      printf("\n");
    }
    printf("\n");
  }

  q_clear(&a);
  q_clear(&b);
  q_clear(&c);
}
Пример #7
0
int test_div ( void )
{
    Q a,inv,x,y;

    /* Set x and y to k */
    q_set_k(x);
    q_print(x);
    printf("\n");

    q_set_k(y);
    q_print(y);
    printf("\n\n");

    /* Divide x by y */
    q_div(a,x,y);

    /* Print the outcome */
    q_print(x);
    printf(" / ");
    q_print(y);
    printf(" = ");
    q_print(a);
    printf("\n");

    return 0;
}
Пример #8
0
/*
 * Difference logic triple (x - y + d)
 * - x and y are vertices
 */
void print_idl_triple(FILE *f, dl_triple_t *triple) {
  bool space;

  space = false;
  if (triple->target >= 0) {
    print_idl_vertex(f, triple->target); // x
    space = true;
  }
  if (triple->source >= 0) {
    if (space) fputc(' ', f);
    fputs("- ", f);
    print_idl_vertex(f, triple->source); // y
    space = true;
  }

  if (! space) {
    q_print(f, &triple->constant);
  } else if (q_is_pos(&triple->constant)) {
    fprintf(f, " + ");
    q_print(f, &triple->constant);
  } else if (q_is_neg(&triple->constant)) {
    fprintf(f, " - ");
    q_print_abs(f, &triple->constant);
  }
}
Пример #9
0
static void convert_int64_test(rational_t *r1) {
  int64_t a;
  uint64_t b;
  rational_t check;

  if (q_get_int64(r1, &a, &b)) {
    printf("Rational: ");
    q_print(stdout, r1);
    printf(" decomposed to %"PRId64"/%"PRIu64" (64 bits)\n", a, b);
    q_init(&check);
    q_set_int64(&check, a, b);
    if (! q_eq(r1, &check)) {
      printf("---> BUG\n");
      fflush(stdout);
      exit(1);
    }
    if (! q_fits_int64(r1)) {
      printf("---> BUG\n");
      fflush(stdout);
      exit(1);
    }
    q_clear(&check);
  } else {
    printf("Rational: ");
    q_print(stdout, r1);
    printf(" not convertible to 64bit integers\n");
    if (q_fits_int64(r1)) {
      printf("---> BUG\n");
      fflush(stdout);
      exit(1);
    }
  }
}
Пример #10
0
/*
 * Conversion to an integer
 */
static void convert32_test(rational_t *r1) {
  int32_t a;
  rational_t check;

  if (q_get32(r1, &a)) {
    printf("Rational: ");
    q_print(stdout, r1);
    printf(" equal to %"PRId32" (32 bits)\n", a);
    q_init(&check);
    q_set32(&check, a);
    if (! q_eq(r1, &check)) {
      printf("---> BUG\n");
      fflush(stdout);
      exit(1);
    }
    if (! q_is_int32(r1)) {
      printf("---> BUG\n");
      fflush(stdout);
      exit(1);
    }
    q_clear(&check);
  } else {
    printf("Rational: ");
    q_print(stdout, r1);
    printf(" not convertible to a 32bit integer\n");
    if (q_is_int32(r1)) {
      printf("---> BUG\n");
      fflush(stdout);
      exit(1);
    }
  }
}
/*
 * Test the period/phase computation
 */
static void test_periods_and_phases(int_constraint_t *cnstr) {
  ivector_t v;
  rational_t *p, *q;
  uint32_t i, n;
  int32_t x;

  init_ivector(&v, 10);

  n = int_constraint_num_terms(cnstr);
  for (i=0; i<n; i++) {
    ivector_reset(&v);
    int_constraint_period_of_var(cnstr, i, &v);
    x = int_constraint_get_var(cnstr, i);
    p = int_constraint_period(cnstr);
    q = int_constraint_phase(cnstr);
    printf("Variable %s: period = ", var[x].name);
    q_print(stdout, p);
    printf(", phase = ");
    q_print(stdout, q);
    printf("\n");
    printf("  antecedents: ");
    show_varnames(stdout, v.data, v.size);
    check_period_and_phase(cnstr, i, p, q);
  }

  delete_ivector(&v);
}
static void show_constraint_details(FILE *f, int_constraint_t *cnstr) {
  fprintf(f, "Details\n");
  fprintf(f, "  num_gcd = ");
  q_print(f, &cnstr->num_gcd);
  fprintf(f, "\n");
  fprintf(f, "  den_lcm = ");
  q_print(f, &cnstr->den_lcm);
  fprintf(f, "\n");
  fprintf(f, "  gcd     = ");
  q_print(f, &cnstr->gcd);
  fprintf(f, "\n");
  fprintf(f, "  fixed   = ");
  q_print(f, &cnstr->fixed_constant);
  fprintf(f, "\n");
}
Пример #13
0
void print_arith_vartable(FILE *f, arith_vartable_t *table) {
  uint32_t i, n;

  n = table->nvars;
  for (i=0; i<n; i++) {
    print_avar(f, table, i);
    switch (arith_var_kind(table, i)) {
    case AVAR_FREE:
      break;

    case AVAR_POLY:
      fputs(" := ", f);
      print_avar_poly(f, table, arith_var_poly_def(table, i));
      break;

    case AVAR_PPROD:
      fputs(" := ", f);
      print_avar_product(f, table, arith_var_product_def(table, i));
      break;

    case AVAR_CONST:
      fputs(" := ", f);
      q_print(f, arith_var_rational_def(table, i));
      break;
    }

    if (arith_var_has_eterm(table, i)) {
      fputs(" --> ", f);
      print_eterm_id(f, arith_var_eterm(table, i));
    }
    fputc('\n', f);
  }
}
Пример #14
0
int main(int argc, const char * argv[]) {
    QUEUE* queue = q_init();
    
    for (int i = 0; i < 5; i++) {
        q_enter(queue, i);
    }
    q_print(queue);
    
    q_quit(queue);
    q_quit(queue);
    q_print(queue);
    
    q_clear(queue);
    q_print(queue);
    
    return 0;
}
Пример #15
0
int main()
{
	int q[MAX_Q];
	int front;
	int rear;

	q_init(q, MAX_Q, &front, &rear); q_print(q, MAX_Q, &front, &rear);

	q_insert(q, MAX_Q, &front, &rear, 10); q_print(q, MAX_Q, &front, &rear);
	q_insert(q, MAX_Q, &front, &rear, 20); q_print(q, MAX_Q, &front, &rear);
	q_insert(q, MAX_Q, &front, &rear, 30); q_print(q, MAX_Q, &front, &rear);

	printf("Val = %d\n", q_delete(q, MAX_Q, &front, &rear));
	printf("Val = %d\n", q_delete(q, MAX_Q, &front, &rear));
	printf("Val = %d\n", q_delete(q, MAX_Q, &front, &rear));

	q_insert(q, MAX_Q, &front, &rear, 40); q_print(q, MAX_Q, &front, &rear);
	q_insert(q, MAX_Q, &front, &rear, 50); q_print(q, MAX_Q, &front, &rear);
	printf("Val = %d\n", q_delete(q, MAX_Q, &front, &rear));
	q_insert(q, MAX_Q, &front, &rear, 60); q_print(q, MAX_Q, &front, &rear);
	printf("Val = %d\n", q_delete(q, MAX_Q, &front, &rear));
	printf("Val = %d\n", q_delete(q, MAX_Q, &front, &rear));

	return 0;
}
Пример #16
0
void queue_test_empty(void) {
	queue_t* q = queue_init(q_compare, q_print, q_clone, q_destroy);
	CU_ASSERT (q != NULL);

	q_print(q, stderr);
	CU_ASSERT (queue_peek(q) == NULL);
	CU_ASSERT (queue_pop(q) == NULL);

	CU_ASSERT (queue_destroy(q) == 0);
}
Пример #17
0
/*
 * Print a solution row:
 * - x = variable defined by that row
 * - r = the row index
 * - v = column indices (as an int_bag)
 */
static void dsolver_print_sol_row_core(FILE *f, dsolver_t *solver, int32_t x, int32_t r, int32_t *v) {
  dcolumn_t *c, *b;
  uint32_t i, n, m;
  int32_t k, j;

  // j = constant index for row r, j == -1 means constant = 0
  b = solver->constant_column;
  j = find_row(b, r);

  if (v == NULL) {
    n = 0;
    m = 0;
  } else {
    m = ibag_nelems(v);
    n = ibag_size(v);
  }

  if (m == 0 && j < 0) {
    fprintf(f, "(= x_%"PRId32" 0)", x);
  } else {

    if (m > 1 || j >= 0) {
      fprintf(f, "(= x_%"PRId32" (+", x);
    } else {
      fprintf(f, "(= x_%"PRId32, x);
    }

    // print the constant first
    if (j >= 0) {
      fprintf(f, " ");
      q_print(f, &b->data[j].coeff);
    }

    // print the matrix element
    for (i=0; i<n; i++) {
      if (v[i] >= 0) {
        assert(v[i] < solver->ncolumns);
        c = solver->column[v[i]];
        assert(c != NULL);
        k = find_row(c, r);
        assert(k >= 0);

        fprintf(f, " ");
        dsolver_print_monomial(f, &c->data[k].coeff, c->var, 'i');
      }
    }

    if (m > 1 || j >= 0) {
      fprintf(f, "))");
    } else {
      fprintf(f, ")");
    }
  }

}
Пример #18
0
/*
 * Print monomial (* a <prefix>_<x>)
 */
static void dsolver_print_monomial(FILE *f, rational_t *a, int32_t x, char prefix) {
  if (q_is_one(a)) {
    fprintf(f, "%c_%"PRId32, prefix, x);
  } else if (q_is_minus_one(a)) {
    fprintf(f, "(- %c_%"PRId32")", prefix, x);
  } else {
    fprintf(f, "(* ");
    q_print(f, a);
    fprintf(f, " %c_%"PRId32")", prefix, x);
  }
}
Пример #19
0
int main(int argc, char *argv[])
{
  /* Set the matrix values directly.  Ugly but true. */
  q_matrix_type col_matrix = { {-0.999848,  0.002700, -0.017242,  0.001715},
			     {        0, -0.987960, -0.154710, -0.207295},
			     {-0.017452, -0.154687,  0.987809, -0.098239},
			     {        0,         0,         0,         1} };
/*
  q_matrix_type col_matrix = { { 0.999848, -0.002700,  0.017242, -0.001715},
			     {        0,  0.987960,  0.154710,  0.207295},
			     {-0.017452, -0.154687,  0.987809, -0.098239},
			     {        0,         0,         0,         1} };
*/
  q_matrix_type	  row_matrix;
  q_xyz_quat_type     q;
  q_xyz_quat_type     q_inverse;

  /* Transpose the column matrix into a row matrix */
  unsigned i,j;
  for (i = 0; i < 4; i++) {
    for (j = 0; j < 4; j++) {
	row_matrix[i][j] = col_matrix[j][i];
    }
  }

  printf("Row Matrix directly:\n");
  q_print_matrix(row_matrix);

  /* Convert to pos/quat and print. */
  q_row_matrix_to_xyz_quat(&q, row_matrix);
  printf("XYZ Quat:\n");
  q_vec_print(q.xyz);
  q_print(q.quat);

  /* Invert the result and print that. */
  printf("XYZ Quat inverse:\n");
  q_xyz_quat_invert(&q_inverse, &q);
  q_vec_print(q_inverse.xyz);
  q_print(q_inverse.quat);

}	/* main */
Пример #20
0
void print_fixed_var_vector(FILE *f, arith_vartable_t *vtbl, fvar_vector_t *fvars) {
  uint32_t i, n;

  n = fvars->nvars;
  for (i=0; i<n; i++) {
    fprintf(f, "  fixed[%"PRIu32"]:   ", i);
    print_space(f, i, n);
    print_avar(f, vtbl, fvars->fvar[i].var);
    fputs(" == ", f);
    q_print(f, &fvars->fvar[i].value);
    fputc('\n', f);
  }
  fputc('\n', f);
}
Пример #21
0
/*
 * Print a triple
 */
static void print_dl_triple(dl_triple_t *t) {
  bool space;

  space = false;
  if (t->target >= 0) {
    printf("x!%"PRId32, t->target);
    space = true;
  }
  if (t->source >= 0) {
    if (space) printf(" ");
    printf("- x!%"PRId32, t->source);
  }


  if (! space) {
    q_print(stdout, &t->constant);
  } else if (q_is_pos(&t->constant)) {
    printf(" + ");
    q_print(stdout, &t->constant);
  } else if (q_is_neg(&t->constant)) {
    printf(" - ");
    q_print_abs(stdout, &t->constant);
  }
}
static void show_var(FILE *f, uint32_t i) {
  const char *type;
  const char *fixed;

  assert(i < NVARS);

  type = var[i].is_int ? "int" : "rational";
  fixed = var[i].is_fixed ? "fixed" : "not fixed";

  fprintf(f, "var[%"PRIu32"]: name = %s, type = %s, %s", i, var[i].name, type, fixed);
  if (var[i].is_fixed) {
    fprintf(f, ", value = ");
    q_print(f, &var[i].fixed_value);
  }
  fprintf(f, "\n");
}
Пример #23
0
/*
 * Print a row:
 * - r = row index
 * - v = array of columns where i occurs (as an integer vector)
 */
static void dsolver_print_row_vector(FILE *f, dsolver_t *solver, int32_t r, ivector_t *v) {
  dcolumn_t *c, *b;
  uint32_t i, n;
  int32_t k, j;

  // j = constant index for row r, j == -1 means constant = 0
  b = solver->constant_column;
  j = find_row(b, r);
  n = v->size;

  if (n == 0 && j < 0) {
    fprintf(f, "(= 0 0)");
  } else {

    if (n > 1 || j >= 0) {
      fprintf(f, "(= (+");
    } else {
      fprintf(f, "(=");
    }

    // print the matrix element
    for (i=0; i<n; i++) {
      assert(0 <= v->data[i] && v->data[i] < solver->ncolumns);
      c = solver->column[v->data[i]];
      assert(c != NULL);
      k = find_row(c, r);
      assert(k >= 0);

      fprintf(f, " ");
      dsolver_print_monomial(f, &c->data[k].coeff, c->var, 'x');
    }

    // print the constant
    if (j >= 0) {
      fprintf(f, " ");
      q_print(f, &b->data[j].coeff);
    }

    // close parentheses
    if (n > 1 || j >= 0) {
      fprintf(f, ") 0)");
    } else {
      fprintf(f, " 0)");
    }
  }
}
Пример #24
0
/*
 * Print the basic solution
 */
void dsolver_print_solution(FILE *f, dsolver_t *solver) {
  uint32_t i, n;

  if (solver->base_sol == NULL) {
    fprintf(f, "No solution computed\n");
  } else {
    fprintf(f, "Solution\n");
    n = solver->nvars;
    for (i=0; i<n; i++) {
      if (solver->sol_row[i] >= 0) {
        fprintf(f, "  x_%"PRIu32" = ", i);
        q_print(f, dsolver_get_value(solver, i));
        fprintf(f, "\n");
      }
    }
  }
}
static void show_fixed_vars(FILE *f, int_constraint_t *cnstr) {
  uint32_t i, n;
  int32_t x;

  n = cnstr->fixed_nterms;
  if (n == 0) {
    fprintf(f, "No fixed variable\n");
  } else {
    fprintf(f, "Fixed variables:\n");
    for (i=0; i<n; i++) {
      x = cnstr->fixed_sum[i].var;
      fprintf(f, " val[%s] = ", var[x].name);
      q_print(f, &cnstr->fixed_val[i]);
      printf("\n");
    }
  }  
}
Пример #26
0
int
main(int argc, char *argv[])
{

    q_type	multQuat;
    q_type 	xformedPoint;
    q_type  	point;
    
    double  	matrix[4][4];




/*
 * read in, echo, and normalize 2 quaternions
 */
printf("\nEnter xform quaternion: (vec, s) ");
scanf("%lf %lf %lf %lf", 
    	&multQuat[0], &multQuat[1], &multQuat[2], &multQuat[3]);

printf("\nEnter point: (x y z) ");
scanf("%lf %lf %lf", 
    	&point[Q_X], &point[Q_Y], &point[Q_Z]);
point[Q_W] = 0.0;

/*
 * matrix of product quat
 */
q_to_col_matrix(matrix, multQuat);
printf("Transformation (column) matrix:\n");
q_print_matrix(matrix);

/* 
 * xformedPoint = (multQuat * candQuat) * invertedQuat	
 */
q_xform(xformedPoint, multQuat, point);

printf("Xform Result:\n");
q_print(xformedPoint);

return(0);

}	/* main */
Пример #27
0
static void test_buffer(rba_buffer_t *b) {
  int32_t x;
  mono_t *m;

  printf("Buffer %p: ", b);
  print_rba_buffer(stdout, b);
  printf("\n");

  test_buffer_pred("is_zero", b, rba_buffer_is_zero);
  test_buffer_pred("is_constant", b, rba_buffer_is_constant);
  test_buffer_pred("is_nonzero", b, rba_buffer_is_nonzero);
  test_buffer_pred("is_pos", b, rba_buffer_is_pos);
  test_buffer_pred("is_neg", b, rba_buffer_is_neg);
  test_buffer_pred("is_nonneg", b, rba_buffer_is_nonneg);
  test_buffer_pred("is_nonpos", b, rba_buffer_is_nonpos);

  printf("  size: %"PRIu32"\n", rba_buffer_num_terms(b));
  printf("  degree: %"PRIu32"\n", rba_buffer_degree(b));
  if (! rba_buffer_is_zero(b)) {
    printf("  main term: ");
    print_pprod0(stdout, rba_buffer_main_term(b));
    printf("\n");
    m = rba_buffer_main_mono(b);
    printf("  main monomial: ");
    q_print(stdout, &m->coeff);
    printf(" * ");
    print_pprod0(stdout, m->prod);
    printf("\n");
  }

  for (x=0; x<5; x++) {
    printf("  degree in x_%"PRId32": %"PRIu32"\n",
	   x, rba_buffer_var_degree(b, x));
  }
  printf("---\n");
}
Пример #28
0
/*
 * Print a row in a matrix or tableau
 * - a variable k of index 1 ... m-1 is printed as x_<k>
 * - a variable k of index m ... is printed as i_<k>
 */
static void dsolver_print_matrix_row(FILE *f, row_t *row, int32_t m) {
  uint32_t i, n;
  int32_t x;
  char prefix;

  n = row->size;
  if (n == 0) {
    fputs("(= 0 0)", f); // empty row (should not happen)
  } else {

    if (n == 1) {
      fputs("(=", f);
    } else {
      fputs("(= (+", f);
    }

    for (i=0; i<n; i++) {
      x = row->data[i].c_idx;
      if (x >= 0) {
        fputc(' ', f);
        if (x == const_idx) {
          q_print(f, &row->data[i].coeff);
        } else {
          prefix = x < m ? 'x' : 'i';
          dsolver_print_monomial(f, &row->data[i].coeff, x, prefix);
        }
      }
    }

    if (n == 1) {
      fputs("0)", f);
    } else {
      fputs(") 0)", f);
    }
  }
}
Пример #29
0
int main(int argc, char **argv)
{
  pthread_t nthread;
  pthread_t sthread;
  pthread_t ethread;
  pthread_t wthread;
  char *dirs;
  int i;

  setvbuf(stdout, NULL, _IOLBF,0);

  if (argc != 2)
    {
      printf("usage: %s <directions>\n",argv[0]);
      return 1;
    }

  dirs = argv[1];

  /*initialize four queues; one for each direction*/
  q_init();
  /*add carts to queues from command line args*/
  for (i=0; dirs[i] != '\0'; ++i)
    {
      if (dirs[i] == 'n')
	q_putCart('n');
      else if (dirs[i] == 's') 
	q_putCart('s');
      else if (dirs[i] == 'e')
	q_putCart('e');
      else if (dirs[i] == 'w')
	q_putCart('w');
      else
	printf("Invalid character %c supplied, ignoring...\n",dirs[i]);
    }
  
  printf("\nStarting queues:\n");
  q_print('n');
  q_print('w');
  q_print('s');
  q_print('e');
  printf("\n");

  /*init monitor*/
  monitor_init();

  /*start threads*/
  if (pthread_create(&nthread,NULL,&north,NULL))
    {
      printf("North thread creation failed.\n");
      return -1;
    }
  if (pthread_create(&sthread,NULL,&south,NULL))
    {
      printf("South thread creation failed.\n");
      return -1;
    }
  if (pthread_create(&ethread,NULL,&east,NULL))
    {
      printf("East thread creation failed.\n");
      return -1;
    }
  if (pthread_create(&wthread,NULL,&west,NULL))
    {
      printf("West thread creation failed.\n");
      return -1;
    }

  /*wait until all threads exit*/
  pthread_join(nthread,NULL);
  pthread_join(sthread,NULL);
  pthread_join(ethread,NULL);
  pthread_join(wthread,NULL);

  printf("\nEnding queues (should all be NULL):\n");
  q_print('n');
  q_print('w');
  q_print('s');
  q_print('e');
  printf("\n");

  /*free up memory and exit program*/
  q_shutdown();
  monitor_shutdown();

  return 0;
}
Пример #30
0
int main(int argc, char *argv[]) {
  char *directions;
  int i;
  pthread_t tid[4];
  char *arg;
  int rc;

/* get command line */
  if (argc != 2) {
    fprintf(stderr, "usage: %s <string: any length/combination of n,e,w,s>\n", argv[0]);
    exit(1);
  }
  directions = argv[1];

/* init other modules */
  q_init();
  monitor_init();
  pthread_barrier_init(&endBarrier, NULL, 4);

/* 1. place all carts on appropriate queue */
  for (i=0; i<strlen(directions); i++)
    q_putCart(directions[i]);
#if 1
  q_print(Q_NORTH);
  q_print(Q_SOUTH);
  q_print(Q_EAST);
  q_print(Q_WEST);
#endif

/* 2. create 4 threads, one for each direction */
  arg = malloc(sizeof(char));
  *arg = Q_NORTH;
  if ((rc = pthread_create(&tid[0], NULL, moveCart, (void *)arg)) != 0)
    fprintf(stderr, "thread create %c failed (%s)\n", *arg, strerror(rc)), exit(1);
  arg = malloc(sizeof(char));
  *arg = Q_SOUTH;
  if ((rc = pthread_create(&tid[1], NULL, moveCart, (void *)arg)) != 0)
    fprintf(stderr, "thread create %c failed (%s)\n", *arg, strerror(rc)), exit(1);
  arg = malloc(sizeof(char));
  *arg = Q_EAST;
  if ((rc = pthread_create(&tid[2], NULL, moveCart, (void *)arg)) != 0)
    fprintf(stderr, "thread create %c failed (%s)\n", *arg, strerror(rc)), exit(1);
  arg = malloc(sizeof(char));
  *arg = Q_WEST;
  if ((rc = pthread_create(&tid[3], NULL, moveCart, (void *)arg)) != 0)
    fprintf(stderr, "thread create %c failed (%s)\n", *arg, strerror(rc)), exit(1);

/* 3. join threads as they complete */
  if ((rc = pthread_join(tid[0], NULL)) != 0)
    fprintf(stderr, "thread join 0 failed (%s)\n", strerror(rc)), exit(1);
  if ((rc = pthread_join(tid[1], NULL)) != 0)
    fprintf(stderr, "thread join 1 failed (%s)\n", strerror(rc)), exit(1);
  if ((rc = pthread_join(tid[2], NULL)) != 0)
    fprintf(stderr, "thread join 2 failed (%s)\n", strerror(rc)), exit(1);
  if ((rc = pthread_join(tid[3], NULL)) != 0)
    fprintf(stderr, "thread join 3 failed (%s)\n", strerror(rc)), exit(1);

/* release resources */
  q_shutdown();
  monitor_shutdown();
  pthread_barrier_destroy(&endBarrier);

  return 0;
}