root / branches / simulator / projects / simulator / simulator / core / main.c @ 986
History | View | Annotate | Download (2.23 KB)
1 |
#include "gtk_gui.h" |
---|---|
2 |
#include <stdio.h> |
3 |
#include <unistd.h> |
4 |
#include <stdlib.h> |
5 |
#include <signal.h> |
6 |
#include <sys/wait.h> |
7 |
#include <robot_shared.h> |
8 |
#include <sys/ipc.h> |
9 |
#include <sys/types.h> |
10 |
#include <sys/shm.h> |
11 |
#include <errno.h> |
12 |
|
13 |
int create_robot(char *execname, int id, SimulatorRobot* sim_memory) |
14 |
{ |
15 |
/* do shared memory stuff here */
|
16 |
// key_t key = IPC_PRIVATE;
|
17 |
/*Memory accessible only to children and gives them read/write priveledge*/
|
18 |
// sim_memory->robotSharedMemoryID = shmget(key, sizeof(RobotShared), IPC_CREAT | 0666);
|
19 |
|
20 |
/* if(sim_memory->robotSharedMemoryID < 0)
|
21 |
{
|
22 |
printf("error getting shared memory\n");
|
23 |
return -1;
|
24 |
}
|
25 |
|
26 |
sim_memory->robotState = shmat(sim_memory->robotSharedMemoryID, NULL, 0);
|
27 |
|
28 |
if(!(sim_memory->robotState))
|
29 |
{
|
30 |
printf("error attaching parent process\n");
|
31 |
return -1;
|
32 |
}
|
33 |
*/
|
34 |
/*Init robot structure here*/
|
35 |
/* sim_memory->robotState->motor1 = 0;
|
36 |
sim_memory->robotState->motor2 = 0;*/
|
37 |
int pid;
|
38 |
|
39 |
if((pid = fork())<0){ |
40 |
printf("error with fork!\n");
|
41 |
return -1; |
42 |
} |
43 |
|
44 |
char var[21]; |
45 |
char *envp[] = {var, NULL}; |
46 |
|
47 |
if(!pid){
|
48 |
/* restore default sigchld handler */
|
49 |
signal(SIGCHLD, SIG_DFL); |
50 |
|
51 |
sprintf(var, "memory_id=%d", 1);//sim_memory->robotSharedMemoryID); |
52 |
|
53 |
execve(execname, NULL, envp); //TODO: keep the other env. stuff around |
54 |
printf ("error! exec failed\n");
|
55 |
|
56 |
exit(-1);
|
57 |
} |
58 |
|
59 |
return pid;
|
60 |
} |
61 |
|
62 |
int free_robot(SimulatorRobot sim)
|
63 |
{ |
64 |
return 0;/* |
65 |
shmdt(sim.robotState);
|
66 |
return shmctl(sim.robotSharedMemoryID, IPC_RMID, NULL); */
|
67 |
} |
68 |
|
69 |
void *sig_chld_handler(int sig) |
70 |
{ |
71 |
pid_t pid; |
72 |
int cstat;
|
73 |
|
74 |
printf("SIGCHLD: parent waiting\n");
|
75 |
|
76 |
pid = waitpid(0, &cstat, WUNTRACED);
|
77 |
|
78 |
printf("got %d from %d\n", cstat, pid);
|
79 |
|
80 |
kill(pid, SIGCONT); |
81 |
|
82 |
return NULL; |
83 |
} |
84 |
|
85 |
int main(int argc, char** argv) |
86 |
{ |
87 |
signal(SIGCHLD, sig_chld_handler); |
88 |
|
89 |
//SimulatorRobot sim;
|
90 |
|
91 |
if(argc<=1){ |
92 |
printf("Usage: simulator <robot execetuable>\n");
|
93 |
exit(-1);
|
94 |
} |
95 |
|
96 |
create_robot(argv[1], 0, NULL/*&sim*/); |
97 |
|
98 |
printf("returned to parent\n");
|
99 |
|
100 |
int x;
|
101 |
/* for(x = 0; x <10000; x++)
|
102 |
printf("motor 1: %d\n", x);*/
|
103 |
|
104 |
gtk_gui_run(argc, argv); |
105 |
|
106 |
printf("parent exiting.\n");
|
107 |
|
108 |
return 0; |
109 |
} |
110 |
|