summaryrefslogtreecommitdiff
path: root/410user/progs/work.c
blob: 46c34f9ca6b4510a0dcbed6ca6ef046a3d49fbec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/** @file 410user/progs/work.c
 *  @author acrichto
 *  @brief Factors some numbers in some tasks
 *  @public yes
 *  @for p4-smp
 *  @status done
 */

#include <410_tests.h>
#include <stdio.h>
#include <stdlib.h>
#include <syscall.h>
#include <stdint.h>

DEF_TEST_NAME("work:");

#define AMT 50
#define MAX 8000000

int work(uint32_t num) {
  uint32_t i;
  uint32_t factors = 0;

  for (i = 1; i <= num; i++) {
    if (num % i == 0) factors ++;
  }

  return factors;
}

int main() {
  int ticks = get_ticks();
  int i;
  int last = 0;
  srand(0x1401);

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

    int next = rand() % MAX;
    int pid = fork();
    REPORT_ON_ERR(pid);
    if (pid == 0) {
      return work(next);
    }
    printf("factoring: %u\n", next);
  }
  print(1, ">");
  for (i = 0; i < AMT; i++) {
    wait(NULL);
    if (i * 79 / AMT > last) {
      print(3, "\b=>");
      last++;
    }
  }

  printf("\nticks taken: %d\n", get_ticks() - ticks);
  return 0;
}