Revision 997
Started on drawing robots in the right place.
robot.c | ||
---|---|---|
31 | 31 |
Robot* robots = NULL; |
32 | 32 |
int robots_size = 0; |
33 | 33 |
|
34 |
int iterator_pos = 0; |
|
35 |
|
|
34 | 36 |
int robots_initialize(void) |
35 | 37 |
{ |
38 |
int i; |
|
36 | 39 |
if (signal(SIGCHLD, sig_chld_handler) == SIG_ERR) |
37 | 40 |
return -1; |
38 | 41 |
|
... | ... | |
41 | 44 |
if (!robots) |
42 | 45 |
return -1; |
43 | 46 |
memset(robots, 0, sizeof(Robot) * robots_size); |
47 |
for (i = 0; i < robots_size; i++) |
|
48 |
robots[i].id = -1; |
|
44 | 49 |
|
45 | 50 |
return 0; |
46 | 51 |
} |
... | ... | |
84 | 89 |
|
85 | 90 |
if((pid = fork()) < 0) |
86 | 91 |
{ |
87 |
|
|
88 | 92 |
//Free Shared Memory Region |
89 |
if (!shmdt(r->shared))
|
|
93 |
if (!shmdt(r->shared)) |
|
90 | 94 |
fprintf(stderr, "Failed to free shared memory.\n"); |
91 | 95 |
|
92 |
if (!shmctl(r->sharedMemID, IPC_RMID, NULL))
|
|
93 |
fprintf(stderr, "Failed to free shared memory.\n");
|
|
96 |
if (!shmctl(r->sharedMemID, IPC_RMID, NULL)) |
|
97 |
fprintf(stderr, "Failed to free shared memory.\n"); |
|
94 | 98 |
|
95 |
r->id = 0;
|
|
99 |
r->id = -1;
|
|
96 | 100 |
fprintf(stderr, "Failed to fork robot process.\n"); |
97 | 101 |
return -1; |
98 |
}
|
|
102 |
} |
|
99 | 103 |
|
100 | 104 |
|
101 | 105 |
if(!pid) |
... | ... | |
161 | 165 |
} |
162 | 166 |
|
163 | 167 |
memset(r, 0, sizeof(Robot)); |
168 |
r->id = -1; |
|
164 | 169 |
|
165 | 170 |
if (id < first_available_id) |
166 | 171 |
first_available_id = 0; |
... | ... | |
168 | 173 |
return 0; |
169 | 174 |
} |
170 | 175 |
|
176 |
void robot_iterator_reset(void) |
|
177 |
{ |
|
178 |
iterator_pos = 0; |
|
179 |
} |
|
180 |
|
|
181 |
// note: addresses may change, replace with |
|
182 |
// allocated memory |
|
183 |
Robot* robot_iterator_next(void) |
|
184 |
{ |
|
185 |
while (iterator_pos < robots_size && |
|
186 |
robots[iterator_pos].id == -1) |
|
187 |
iterator_pos++; |
|
188 |
|
|
189 |
if (iterator_pos >= robots_size) |
|
190 |
return NULL; |
|
191 |
|
|
192 |
return &(robots[iterator_pos]); |
|
193 |
} |
|
194 |
|
|
171 | 195 |
void sig_chld_handler(int sig) |
172 | 196 |
{ |
173 | 197 |
pid_t pid; |
Also available in: Unified diff