/* depend: */
/* cflags: */
--/* linker: */
++/* linker: -lpthread */
#include <assert.h>
+ //#include <math.h>
++#include <pthread.h>
++#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
++#include <unistd.h>
/* constants */
- #define MAXSIZE 16;
-
- /* types */
-
- typedef struct {
- int n;
- double *mat;
- } matrix_t;
++#define RT_PRIO 80
+ //#define N 32
+ #define N 10
++#define BREATH 50
/* static variables */
double val;
get_matrix (matrix, 0, l, &val);
det += sign * val * determinant (comatrix);
++ usleep (BREATH);
}
+ free_matrix (comatrix);
return det;
}
+ void generate_matrix (matrix_t *matrix)
+ {
+ double base = 10 * drand48 () - 5;
+ for (int i = 0; i < matrix->n; i++) {
+ for (int j = 0; j < matrix->n; j++) {
+ double val = base * (rand () - RAND_MAX / 2) / (double)RAND_MAX;
+ set_matrix (matrix, i, j, val);
+ }
++ usleep (BREATH);
+ }
+ }
+
++void *work (__attribute__((unused)) void *arg)
++{
++ while (1) {
++ matrix_t *matrix = alloc_matrix (N);
++ generate_matrix (matrix);
++ double det = determinant (matrix);
++ printf ("det: %g\n", det);
++ }
++ pthread_exit (NULL);
++}
++
/* main function */
int main (int argc, char *argv[])
}
}
++#if 1
++ /* real-time process */
++
++ struct sched_param param = {0};
++ if (sched_getparam (0, ¶m) != 0) {
++ fprintf (stderr, "error: sched_getparam\n");
++ return 1;
++ }
++ param.sched_priority = RT_PRIO;
++ int rc = sched_setscheduler (0, SCHED_FIFO, ¶m); /* non-preemptive */
++ // int rc = sched_setscheduler (0, SCHED_RR, ¶m); /* preemptive */
++ if (rc != 0) {
++ fprintf (stderr, "error: sched_setscheduler\n");
++ return 1;
++ }
++#endif
++
/* main process */
- while (1) {
- matrix_t *matrix = alloc_matrix (N);
- generate_matrix (matrix);
- double det = determinant (matrix);
- printf ("det: %g\n", det);
+
++ if (nb_threads == 0) {
++ while (1) {
++ sleep (60);
++ }
++ } else {
++ pthread_t *tid = (pthread_t *) calloc (nb_threads, sizeof (pthread_t));
++ assert (tid);
++ for (int i = 0; i < nb_threads; i++) {
++ if (pthread_create (&tid[i], NULL, work, NULL) != 0) {
++ fprintf (stderr, "error on pthread_create\n");
++ return 1;
++ }
++ }
++
++ for (int i = 0; i < nb_threads; i++) {
++ pthread_join (tid[i], NULL);
++ }
+ }
+
return 0;
}