Project

General

Profile

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)

View differences:

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