-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocesses.c
49 lines (38 loc) · 976 Bytes
/
processes.c
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
// pid arrival_time service_time priority
int main(int argc, char *argv[]) {
if (argc != 3) {
fprintf(stderr, "Usage: ./processes <num_processes> <output_file>\n");
exit(1);
}
int num_p = atoi(argv[1]);
int array[num_p];
int temp, randomIndex;
int pid, arrival_time, service_time, priority;
int i;
time_t t;
FILE *fp;
srand((unsigned) time(&t));
fp = fopen(argv[2], "w");
for (i = 0; i < num_p; i++) { // fill array
array[i] = i;
}
for (i = 0; i < num_p; i++) { // shuffle array
temp = array[i];
randomIndex = rand() % num_p;
array[i] = array[randomIndex];
array[randomIndex] = temp;
}
for (i=0; i < num_p; i++) {
pid = array[i];
arrival_time = rand() % 50;
service_time = (rand() % 20) + 1;
priority = (rand() % 40) - 20;
fprintf(fp, "%d %d %d %d\n", pid, arrival_time, service_time, priority);
}
fclose(fp);
return 0;
}