Revision 984
Function to free memory added, not sure how to test if its working.
branches/simulator/projects/simulator/simulator/robot_test/robot.c | ||
---|---|---|
3 | 3 |
#include <signal.h> |
4 | 4 |
#include <unistd.h> |
5 | 5 |
#include <stdlib.h> |
6 |
#include <sys/shm.h> |
|
7 |
#include <sys/ipc.h> |
|
8 |
#include <robot_shared.h> |
|
9 |
#include <string.h> |
|
6 | 10 |
|
7 | 11 |
volatile int i; |
8 | 12 |
|
13 |
RobotShared* shared_state; |
|
14 |
|
|
9 | 15 |
void *tick(int sig) |
10 | 16 |
{ |
11 | 17 |
printf("robot process paused. suspending\n"); |
12 | 18 |
|
19 |
shared_state->motor1++; |
|
20 |
|
|
13 | 21 |
if(raise(SIGTSTP)<0) |
14 | 22 |
printf("could not kill self!\n"); |
15 | 23 |
|
... | ... | |
23 | 31 |
int ret; |
24 | 32 |
struct itimerval iv; |
25 | 33 |
|
34 |
shared_state = shmat(atoi(envp[0]), NULL, 0); |
|
35 |
if(shared_state < 0) |
|
36 |
{ |
|
37 |
printf("unable to get shared memory region\n"); |
|
38 |
return 1; |
|
39 |
} |
|
40 |
|
|
41 |
|
|
26 | 42 |
printf("hello. I am a robot w/ env[0] %s\n", envp[0]); |
27 | 43 |
|
28 | 44 |
iv.it_interval.tv_sec = 1; |
... | ... | |
42 | 58 |
while(1) |
43 | 59 |
i++; |
44 | 60 |
|
61 |
shmdt(shared_state); |
|
45 | 62 |
return 0; |
46 | 63 |
} |
branches/simulator/projects/simulator/simulator/core/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