4 Replies - 1619 Views - Last Post: 27 February 2011 - 05:03 PM Rate Topic: -----

#1 monicojr84   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 11-February 11

Shared memory problem

Posted 26 February 2011 - 07:25 PM

I have been able to create and attach to a shared memory segment. But I need to be
able to run two instances of the same program and have them both be able to access
the segment. I have no idea what I'm doing wrong.

The program works fine with only one instance but when I execute another instance it
doesn't see the changes that I made to my structs. Can anyone see what I'm doing wrong here.

Here is some of the code:
typedef struct
{ char *name;
  int row;
  char col;
}Reserve;

void initialize(Reserve x[]);  //This initializes flight information
int displayMenu();  //This function displays the options that the user has and retrieves their choice
void allSeats(Reserve all[]);  //This function lists all the seats on the flight
void reserveSeat(Reserve res[]);  //This function reserves a seat for a customer
void openSeats(Reserve open[]);  //This function list all available seats on the flight
void list(Reserve x);  //This function outputs the seat information from the structs

#define SIZE 120
Reserve res[SIZE];

void main()
{ int choice;
  int shmid;
  int shmSize = sizeof(res);
  key_t key = 1234;
  Reserve *shared;
  Reserve *data = &res[0];

  //create the shared memory segment if the 1st process
  shmid = shmget(key, shmSize, 0644 | IPC_CREAT);
  if(shmid == -1)
    { perror("shmget failed");
      exit(1);
    }
  
  //Connect to the shared memory segment
  shared =(Reserve *)shmat(shmid, (void*)0, 0);
  if(shared == (void *)(-1))
    { perror("shmat failed");
      exit(1);
    }
  
  //make struct array point to shared memory segment
  data = shared;

  //initialize structs
   initialize(shared);
  
  //Display choices to user
  choice = displayMenu();

  //Determine which function to call for the users input
  while(1)
    {switch(choice)
	{ case 1:
	    allSeats(shared); break;
	  case 2:
	    reserveSeat(shared); break;
	  case 3:
	    openSeats(shared); break;
	default:
	  printf("Invalid choice, Please try again.\n"); break;
	}//End of switch

      //Ask the user for their next choice
      choice = displayMenu();
    }//End of while

  //Detach from shared memory segment
  if(shmdt(shared) == -1)
    { perror("shdmt failed");
      exit(1);
    }

   //remove shared memory segment
  if(shmctl(shmid, IPC_RMID, 0)== -1)
    { perror("shmctl failed");
      exit(1);
    }
  
}//End of main



Is This A Good Question/Topic? 0
  • +

Replies To: Shared memory problem

#2 Martyn.Rae   User is offline

  • The programming dinosaur
  • member icon

Reputation: 557
  • View blog
  • Posts: 1,438
  • Joined: 22-August 09

Re: Shared memory problem

Posted 27 February 2011 - 12:00 AM

Remember, only the instance that successfully creates the shared memory segment needs to initialize your arrays. If an instance fails to create the shared memory segment, then it should attempt to get it not fail as your code does. If it gets it, then it should not initialize the shared memory.
Was This Post Helpful? 0
  • +
  • -

#3 monicojr84   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 11-February 11

Re: Shared memory problem

Posted 27 February 2011 - 03:23 PM

But How can I do that if I need to attach to the segment before I can use it?
Don't I need the pointer return from shmat() to initialize the struct.

Sorry I'm just very confused. I was basically given the definitions of shared memory, semaphores
and signals and told to write a program using them.
Was This Post Helpful? 0
  • +
  • -

#4 Martyn.Rae   User is offline

  • The programming dinosaur
  • member icon

Reputation: 557
  • View blog
  • Posts: 1,438
  • Joined: 22-August 09

Re: Shared memory problem

Posted 27 February 2011 - 03:30 PM

Try something like this:

  // attempt to create the shared memory segment if the 1st process
  shmid = shmget(key, shmSize, 0644 | IPC_CREAT);
  if ( shmid == -1 ) {   //Connect to the shared memory segment if already created
      data =(Reserve *)shmat(shmid, (void*)0, 0);
      if ( shared == (void *)(-1) ) { 
           perror("shmat failed");
           exit(1);
      }
  }
  else { // Connect and initialize data area if the first time
      data =(Reserve *)shmat(shmid, (void*)0, 0);
      initialize(shared);
  }


Was This Post Helpful? 0
  • +
  • -

#5 Aphex19   User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 619
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: Shared memory problem

Posted 27 February 2011 - 05:03 PM

void main()


This is wrong, the main is an integer function and should return 0 upon success.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1