root / branches / simulator / projects / simulator / simulator / core / main.c @ 982
History | View | Annotate | Download (1.83 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 |
|
12 |
int create_robot(char *execname, int id, RobotShared** robot_memory, SimulatorRobot* sim_memory) |
13 |
{ |
14 |
/* do shared memory stuff here */
|
15 |
key_t key = IPC_PRIVATE; |
16 |
/*Memory accessible only to children and gives them read/write priveledge*/
|
17 |
sim_memory->robotSharedMemoryID = shmget(key, sizeof(RobotShared), IPC_CREAT | 0666); |
18 |
|
19 |
if(sim_memory->robotSharedMemoryID < 0) |
20 |
{ |
21 |
printf("error getting shared memory\n");
|
22 |
return -1; |
23 |
} |
24 |
|
25 |
*robot_memory = shmat(sim_memory->robotSharedMemoryID, NULL, 0); |
26 |
if(!(*robot_memory))
|
27 |
{ |
28 |
printf("error attaching parent process\n");
|
29 |
return -1; |
30 |
} |
31 |
|
32 |
int pid;
|
33 |
|
34 |
if((pid = fork())<0){ |
35 |
printf("error with fork!\n");
|
36 |
return -1; |
37 |
} |
38 |
|
39 |
//Need to consider if this actually works...
|
40 |
char var[21]; |
41 |
sprintf(var, "memory_id=%d", sim_memory->robotSharedMemoryID);
|
42 |
char *envp[] = {var, NULL}; |
43 |
|
44 |
if(!pid){
|
45 |
/* restore default sigchld handler */
|
46 |
signal(SIGCHLD, SIG_DFL); |
47 |
|
48 |
execve(execname, NULL, envp); //TODO: keep the other env. stuff around |
49 |
printf ("error! exec failed\n");
|
50 |
exit(-1);
|
51 |
} |
52 |
|
53 |
return pid;
|
54 |
} |
55 |
|
56 |
void *sig_chld_handler(int sig) |
57 |
{ |
58 |
pid_t pid; |
59 |
int cstat;
|
60 |
|
61 |
printf("SIGCHLD: parent waiting\n");
|
62 |
|
63 |
pid = waitpid(0, &cstat, WUNTRACED);
|
64 |
|
65 |
printf("got %d from %d\n", cstat, pid);
|
66 |
|
67 |
kill(pid, SIGCONT); |
68 |
|
69 |
return NULL; |
70 |
} |
71 |
|
72 |
int main(int argc, char** argv) |
73 |
{ |
74 |
signal(SIGCHLD, sig_chld_handler); |
75 |
|
76 |
RobotShared* robot; |
77 |
SimulatorRobot* sim; |
78 |
|
79 |
create_robot("robot_test/robot", 0, &robot, sim); |
80 |
|
81 |
printf("returned to parent\n");
|
82 |
|
83 |
|
84 |
gtk_gui_run(argc, argv); |
85 |
|
86 |
printf("parent exiting.\n");
|
87 |
|
88 |
return 0; |
89 |
} |
90 |
|