Revision 982
Added shared memory setup code to the create_robot function. Need to
write the cleanup code still. Exec call is failing but wasn't
succeeding before code change either.
main.c | ||
---|---|---|
4 | 4 |
#include <stdlib.h> |
5 | 5 |
#include <signal.h> |
6 | 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> |
|
7 | 11 |
|
8 |
int create_robot(char *execname, int id) |
|
12 |
int create_robot(char *execname, int id, RobotShared** robot_memory, SimulatorRobot* sim_memory)
|
|
9 | 13 |
{ |
10 | 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 |
} |
|
11 | 24 |
|
12 |
int child; |
|
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; |
|
13 | 33 |
|
14 |
if((child = fork())<0){
|
|
15 |
printf("error with fork!\n"); |
|
34 |
if((pid = fork())<0){
|
|
35 |
printf("error with fork!\n");
|
|
16 | 36 |
return -1; |
17 |
} |
|
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}; |
|
18 | 43 |
|
19 |
char *envp[] = {"test=1", NULL}; |
|
20 |
|
|
21 |
if(!child){ |
|
44 |
if(!pid){ |
|
22 | 45 |
/* restore default sigchld handler */ |
23 | 46 |
signal(SIGCHLD, SIG_DFL); |
24 | 47 |
|
... | ... | |
26 | 49 |
printf ("error! exec failed\n"); |
27 | 50 |
exit(-1); |
28 | 51 |
} |
29 |
|
|
30 |
return child;
|
|
52 |
|
|
53 |
return pid;
|
|
31 | 54 |
} |
32 | 55 |
|
33 | 56 |
void *sig_chld_handler(int sig) |
... | ... | |
50 | 73 |
{ |
51 | 74 |
signal(SIGCHLD, sig_chld_handler); |
52 | 75 |
|
53 |
create_robot("robot_test/robot", 0); |
|
76 |
RobotShared* robot; |
|
77 |
SimulatorRobot* sim; |
|
54 | 78 |
|
79 |
create_robot("robot_test/robot", 0, &robot, sim); |
|
80 |
|
|
55 | 81 |
printf("returned to parent\n"); |
56 | 82 |
|
57 | 83 |
|
Also available in: Unified diff