Revision 1039
works with 20 robots! but its incredibly slow and destroys computers
also fixed a bug in the robot creation code (it didn't init robots array to -1 after a resize)
branches/simulator/projects/simulator/libsim/libsim.c | ||
---|---|---|
8 | 8 |
#include <sys/ipc.h> |
9 | 9 |
#include <robot_shared.h> |
10 | 10 |
#include <string.h> |
11 |
|
|
11 | 12 |
RobotShared* shared_state; |
12 | 13 |
|
13 | 14 |
void *tick(int sig) |
14 | 15 |
{ |
15 |
printf("robot process paused. suspending\n");
|
|
16 |
printf("robot paused. suspending\n"); |
|
16 | 17 |
|
17 | 18 |
if(raise(SIGTSTP)<0) |
18 | 19 |
printf("could not kill self!\n"); |
branches/simulator/projects/simulator/simulator/core/main.c | ||
---|---|---|
26 | 26 |
exit(-1); |
27 | 27 |
} |
28 | 28 |
|
29 |
|
|
29 | 30 |
robot_create(argv[1]); |
31 |
robot_create(argv[1]); |
|
32 |
robot_create(argv[1]); |
|
33 |
robot_create(argv[1]); |
|
34 |
robot_create(argv[1]); |
|
35 |
robot_create(argv[1]); |
|
36 |
robot_create(argv[1]); |
|
37 |
robot_create(argv[1]); |
|
38 |
robot_create(argv[1]); |
|
39 |
robot_create(argv[1]); |
|
40 |
robot_create(argv[1]); |
|
41 |
robot_create(argv[1]); |
|
42 |
robot_create(argv[1]); |
|
43 |
robot_create(argv[1]); |
|
44 |
robot_create(argv[1]); |
|
45 |
robot_create(argv[1]); |
|
46 |
robot_create(argv[1]); |
|
47 |
robot_create(argv[1]); |
|
48 |
robot_create(argv[1]); |
|
49 |
robot_create(argv[1]); |
|
30 | 50 |
|
51 |
|
|
31 | 52 |
sigset_t set; |
32 | 53 |
//TODO: errors |
33 | 54 |
sigemptyset(&set); |
branches/simulator/projects/simulator/simulator/core/robot.c | ||
---|---|---|
21 | 21 |
#include <sys/shm.h> |
22 | 22 |
#include <errno.h> |
23 | 23 |
#include <pthread.h> |
24 |
#include <err.h> |
|
24 | 25 |
|
25 | 26 |
#include "robot.h" |
26 | 27 |
#include "motion.h" |
... | ... | |
70 | 71 |
**/ |
71 | 72 |
int robot_create(char *execname) |
72 | 73 |
{ |
73 |
int pid;
|
|
74 |
int pid,i;
|
|
74 | 75 |
int id = first_available_id; |
75 | 76 |
Robot* r = &robots[id]; |
76 | 77 |
printf("ID: %d\n", id); |
... | ... | |
79 | 80 |
//Memory accessible only to children with r/w privileges |
80 | 81 |
r->sharedMemID = shmget(key, sizeof(RobotShared), IPC_CREAT | 0666); |
81 | 82 |
|
83 |
|
|
84 |
//hack!! TODO: remove!! |
|
85 |
r->pose.x=first_available_id*100; |
|
86 |
r->pose.y=first_available_id*50; |
|
87 |
|
|
88 |
|
|
82 | 89 |
if(r->sharedMemID < 0) |
83 | 90 |
{ |
84 | 91 |
fprintf(stderr, "Failed to get shared memory.\n"); |
... | ... | |
126 | 133 |
putenv(var); |
127 | 134 |
//TODO: keep the other env. stuff around |
128 | 135 |
execv(execname, NULL); |
129 |
fprintf(stderr, "exec failed to run child process.\n"); |
|
130 |
exit(-1); |
|
136 |
err(errno, "exec failed to run child process.\n"); |
|
131 | 137 |
} |
132 | 138 |
else |
133 | 139 |
{ |
... | ... | |
146 | 152 |
fprintf(stderr, "Out of memory.\n"); |
147 | 153 |
return -1; |
148 | 154 |
} |
155 |
for (i = robots_size=first_available_id; i < robots_size; i++) |
|
156 |
robots[i].id = -1; |
|
157 |
|
|
149 | 158 |
} |
150 | 159 |
} |
151 | 160 |
|
... | ... | |
213 | 222 |
|
214 | 223 |
void sig_chld_handler(int sig) |
215 | 224 |
{ |
216 |
pthread_mutex_lock(&all_finished_mutex); |
|
217 |
finished++; |
|
218 |
if (finished >= num_robots) |
|
219 |
{ |
|
220 |
pthread_cond_signal(&all_finished_cond); |
|
221 |
} |
|
222 |
pthread_mutex_unlock(&all_finished_mutex); |
|
225 |
int ret,stat; |
|
226 |
|
|
227 |
pthread_mutex_lock(&all_finished_mutex); |
|
228 |
|
|
229 |
while((ret = waitpid(-1, &stat, WUNTRACED | WNOHANG))) |
|
230 |
{ |
|
231 |
if(ret==-1) |
|
232 |
err(errno,"error waiting on robot"); |
|
233 |
|
|
234 |
if(WIFSTOPPED(stat)) |
|
235 |
finished++; |
|
236 |
} |
|
237 |
|
|
238 |
if (finished >= num_robots) |
|
239 |
{ |
|
240 |
pthread_cond_signal(&all_finished_cond); |
|
241 |
} |
|
242 |
pthread_mutex_unlock(&all_finished_mutex); |
|
223 | 243 |
} |
224 | 244 |
|
225 | 245 |
void* robot_event_loop(void* arg) |
Also available in: Unified diff