Revision 984
Function to free memory added, not sure how to test if its working.
main.c | ||
---|---|---|
8 | 8 |
#include <sys/ipc.h> |
9 | 9 |
#include <sys/types.h> |
10 | 10 |
#include <sys/shm.h> |
11 |
#include <errno.h> |
|
11 | 12 |
|
12 |
int create_robot(char *execname, int id, RobotShared** robot_memory, SimulatorRobot* sim_memory)
|
|
13 |
int create_robot(char *execname, int id, SimulatorRobot* sim_memory) |
|
13 | 14 |
{ |
14 | 15 |
/* do shared memory stuff here */ |
15 | 16 |
key_t key = IPC_PRIVATE; |
... | ... | |
22 | 23 |
return -1; |
23 | 24 |
} |
24 | 25 |
|
25 |
*robot_memory = shmat(sim_memory->robotSharedMemoryID, NULL, 0); |
|
26 |
if(!(*robot_memory)) |
|
26 |
sim_memory->robotState = shmat(sim_memory->robotSharedMemoryID, NULL, 0); |
|
27 |
|
|
28 |
if(!(sim_memory->robotState)) |
|
27 | 29 |
{ |
28 | 30 |
printf("error attaching parent process\n"); |
29 | 31 |
return -1; |
30 | 32 |
} |
31 | 33 |
|
34 |
/*Init robot structure here*/ |
|
35 |
sim_memory->robotState->motor1 = 0; |
|
36 |
sim_memory->robotState->motor2 = 0; |
|
37 |
|
|
32 | 38 |
int pid; |
33 | 39 |
|
34 | 40 |
if((pid = fork())<0){ |
... | ... | |
45 | 51 |
signal(SIGCHLD, SIG_DFL); |
46 | 52 |
|
47 | 53 |
execve(execname, NULL, envp); //TODO: keep the other env. stuff around |
48 |
printf ("error! exec failed \n");
|
|
54 |
printf ("error! exec failed\n"); |
|
49 | 55 |
|
50 | 56 |
exit(-1); |
51 | 57 |
} |
... | ... | |
53 | 59 |
return pid; |
54 | 60 |
} |
55 | 61 |
|
62 |
int free_robot(SimulatorRobot sim) |
|
63 |
{ |
|
64 |
shmdt(sim.robotState); |
|
65 |
return shmctl(sim.robotSharedMemoryID, IPC_RMID, NULL); |
|
66 |
} |
|
67 |
|
|
56 | 68 |
void *sig_chld_handler(int sig) |
57 | 69 |
{ |
58 | 70 |
pid_t pid; |
... | ... | |
73 | 85 |
{ |
74 | 86 |
signal(SIGCHLD, sig_chld_handler); |
75 | 87 |
|
76 |
RobotShared* robot; |
|
77 | 88 |
SimulatorRobot sim; |
78 | 89 |
|
79 |
create_robot("robot_test/robot", 0, &robot, &sim);
|
|
90 |
create_robot("robot_test/robot", 0, &sim); |
|
80 | 91 |
|
81 | 92 |
printf("returned to parent\n"); |
82 | 93 |
|
94 |
int x; |
|
95 |
for(x = 0; x <10000; x++) |
|
96 |
printf("motor 1: %d\n", x); |
|
83 | 97 |
|
84 | 98 |
gtk_gui_run(argc, argv); |
85 | 99 |
|
... | ... | |
87 | 101 |
|
88 | 102 |
return 0; |
89 | 103 |
} |
90 |
|
|
104 |
|
Also available in: Unified diff