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

New Topic/Question
Reply



MultiQuote


|