-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
88 lines (79 loc) · 1.83 KB
/
main.c
File metadata and controls
88 lines (79 loc) · 1.83 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
CS 533 Class Project
Naomi Dickerson
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include "scheduler.h"
// Prime function courtesy of Kendall Stewart
void print_nth_prime(void * pn) {
int n = *(int *) pn;
int c = 1, i = 1;
while(c <= n) {
++i;
int j, isprime = 1;
for(j = 2; j < i; ++j) {
if(i % j == 0) {
isprime = 0;
break;
}
}
if(isprime) {
++c;
}
yield();
}
printf("%dth prime: %d\n", n, i);
}
void input_wait(void * chars) {
int n = *(int *) chars;
char * buffer;
buffer = (char *) malloc(n * sizeof(char));
printf("Type %d chars: \n", n);
read_wrap(STDIN_FILENO, buffer, n);
int i;
printf("Buffer contents: ");
for (i = 0; i < n; i++) {
printf("%c ", buffer[i]);
}
free(buffer);
}
void printBuffer(char * buffer, int n) {
int i;
for (i = 0; i < n; i++) {
printf("%c", buffer[i]);
}
}
void open_file(void * junk) {
int fd, ret;
char buff[10];
fd = open("test.txt", O_RDONLY);
printf("Reading 1st 10 chars from test.txt...\n");
ret = read_wrap(fd, buff, 10);
printf("Buffer contents: ");
printBuffer(buff, 10);
printf("\n");
printf("return value: %d\n", ret);
printf("Next 10...\n");
ret = read_wrap(fd, buff, 10);
printf("Buffer contents: ");
printBuffer(buff, 10);
printf("\n");
printf("return value: %d\n", ret);
close(fd);
}
void go1(void) {
int n1 = 20000, n2 = 10000, n3 = 30000, n4 = 2;
thread_fork(print_nth_prime, &n1);
thread_fork(print_nth_prime, &n2);
thread_fork(print_nth_prime, &n3);
thread_fork(open_file, &n4);
}
int main(void) {
scheduler_begin();
go1();
scheduler_end();
return 0;
}