Project

General

Profile

Revision 993

Free's shared memory in the core file upon error in forking or error in attaching to shared memory

View differences:

branches/simulator/projects/simulator/libsim/libsim.c
6 6
#include <stdlib.h>
7 7
#include <sys/shm.h>
8 8
#include <sys/ipc.h>
9
//#include <robot_shared.h>
9
#include <robot_shared.h>
10 10
#include <string.h>
11 11

  
12 12

  
13
//RobotShared* shared_state;
13
RobotShared* shared_state;
14 14

  
15 15
void *tick(int sig)
16 16
{
17
  printf("robot process paused. suspending\n");
17
  printf("robot process paused. suspending\n"); 
18 18

  
19
  //shared_state->motor1++; 
20

  
21 19
  if(raise(SIGTSTP)<0)
22 20
    printf("could not kill self!\n");
23 21

  
......
32 30
  struct itimerval iv;
33 31
  int ret;
34 32

  
35
  /*shared_state = shmat(atoi(envp[0]), NULL, 0);
33
  shared_state = shmat(atoi(envp[0]), NULL, 0);
36 34
  if(shared_state < 0)
37 35
  {
38 36
	  printf("unable to get shared memory region\n");
39 37
	  return 1;
40
	  }*/
38
  }
41 39

  
42 40
                                             
43 41
  printf("hello. I am a robot w/ memory_id %s\n", getenv("memory_id"));
branches/simulator/projects/simulator/simulator/core/robot.c
68 68
	r->shared = (RobotShared*)shmat(r->sharedMemID, NULL, 0);
69 69
	
70 70
	if(!(r->shared))
71
	{
72
		// TODO: do we have to free the shared memory?
71
	{	
72
		//Free shared memory region
73
		if (!shmctl(r->sharedMemID, IPC_RMID, NULL))
74
			fprintf(stderr, "Failed to free shared memory.\n");
75
		       
73 76
		fprintf(stderr, "Error attaching memory to parent.\n");
74 77
		return -1;
75 78
	}
......
81 84

  
82 85
	if((pid = fork()) < 0)
83 86
	{
84
		// TODO: do we have to free the shared memory?
87
		
88
		//Free Shared Memory Region
89
		if (!shmdt(r->shared))                    
90
			fprintf(stderr, "Failed to free shared memory.\n");
91
		
92
		if (!shmctl(r->sharedMemID, IPC_RMID, NULL))	
93
			fprintf(stderr, "Failed to free shared memory.\n");      
94

  
85 95
		r->id = 0;
86 96
		fprintf(stderr, "Failed to fork robot process.\n");
87 97
		return -1;
......
139 149
	if (r->id == 0)
140 150
		return -1;
141 151

  
142
	if (!shmdt(r->shared))
152
    if (!shmdt(r->shared))
143 153
	{
144 154
		fprintf(stderr, "Failed to free shared memory.\n");
145 155
		return -1;

Also available in: Unified diff