-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathprocess_packages.c
135 lines (124 loc) · 3.49 KB
/
process_packages.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
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/* ----------------------------------------------------------------------- *
*
* Author: Leandro Augusto Lacerda Campos <[email protected]>
*
* Data Structures and Algorithms Specialization,
* by University of California, San Diego,
* and National Research University Higher School of Economics
*
* Course 2: Data Structures
*
* Solution for Network Packet Processing Simulation Problem
*
* ----------------------------------------------------------------------- */
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef size_t
#undef size_t
#endif
#define size_t unsigned int
#define time unsigned long
typedef struct Packet Packet;
struct Packet {
time arrival; /* time of arrival, in milliseconds */
time processing; /* processing time, in milliseconds */
bool isdropped; /* packet is dropped because network buffer is full */
time start; /* start time of processing, in milliseconds */
};
/*
* emalloc: allocates memory or print a message if an error occurs
*/
void *emalloc(size_t n)
{
void *p;
p = malloc(n);
if (p == NULL) {
printf("malloc of %u bytes failed\n", n);
exit(1);
}
return p;
}
/*
* efree: deallocates the memory previously allocated or print a message if
* an error occurs
*/
void *efree(void *p)
{
if (p == NULL) {
printf("ptr points to NULL\n");
exit(1);
}
free(p);
return (p = NULL);
}
/*
* processor: simulates the processing of network packets following the
* instruction of the problem (see the corresponding programming assignment
* file)
*/
time processor(Packet *base, size_t nel, size_t bufsiz)
{
size_t clock; /* processor clock */
size_t cnt; /* number of packets in the buffer */
size_t bufidx; /* last packet in the buffer */
Packet *pkt; /* current packet in the processor */
size_t i;
if (nel == 0)
return 0;
if (bufsiz == 0) {
printf("the size of the buffer should be equal to or greater than 1\n");
exit(1);
}
clock = 0;
cnt = 0;
bufidx = 0;
for (i = 0; i < nel; i++) {
pkt = &base[i];
if (!pkt->isdropped) {
if (cnt == 0) { /* if the packet was not in the buffer */
cnt++;
clock = pkt->arrival;
bufidx = i;
}
pkt->start = clock;
clock += pkt->processing;
/* update the buffer */
while (++bufidx < nel && base[bufidx].arrival < clock) {
if (cnt < bufsiz) /* put base[bufidx] in the buffer */
cnt++;
else /* buffer is full */
base[bufidx].isdropped = true;
}
bufidx--;
/* release a place in the buffer */
cnt--;
}
}
return clock;
}
int main()
{
size_t s, n, i;
Packet *packets;
scanf("%u", &s); /* size of the buffer */
scanf("%u", &n); /* number of incoming network packets */
if (n == 0)
exit(0);
packets = emalloc(n * sizeof(*packets));
for (i = 0; i < n; i++) {
scanf("%lu", &packets[i].arrival);
scanf("%lu", &packets[i].processing);
packets[i].isdropped = false;
packets[i].start = 0;
}
processor(packets, n, s);
for (i = 0; i < n; i++) {
if (packets[i].isdropped)
printf("%d\n", -1);
else
printf("%lu\n", packets[i].start);
}
packets = efree(packets);
return 0;
}